-
Notifications
You must be signed in to change notification settings - Fork 537
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create add_months()
method on NaiveDate
#731
Conversation
@esheppa I believe you referenced some external crates that add stuff like this? I think I tend to agree that it probably make sense to bring this functionality into chrono, what do you think? |
I don't usually do |
The |
I think it would be great to have an One of the crates I mentioned is The other way this kind of thing can be achieved is by using a TLDR: |
Thanks! I swapped out to |
Hi @avantgardnerio - I'm still keen to merge this if @djc is also on board. There may be some changes required by code review but I think this functionality would be great to have within |
@esheppa , I'd like to push back against the idea of a If we wanted something more more type-safe than the i32-based solution, I'd propose something like JodaTime's Period. I do think this approach is overly complicated however, since all other durations are true fixed intervals: hours, minutes, seconds, days, and weeks are a defined as number of relative milliseconds (ignoring leap seconds, which even Google does). Unfortunately the outliers are months and years (which are trivial for users to treat as 12 months). With this being the single edge case, I'd posit that adding a new enum is probably not be worth the complexity: would we need a new Looking for precedent, it appears both JodaTime and Java 8 support the straightforward solution like the one proposed in this PR:
I'm happy to help do the work to finally get month addition into chrono, but it seems like the first step might be selecting a commonly agreed upon design. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding this method. The code looks good - if possible I'd just like a few changes like removing use of as
(I recognize this is used heavily around the code base but we are trying to avoid introducing new usage, and gradually removing the old usage)
Hi @avantgardnerio - thanks for these notes, they raise some valuable points in the In terms of having an |
It looks like the build is failing because |
Thanks for the changes, they look great! Unfortunately it looks like
|
If this is not fallible I'm inclined not to have this interface in favor of a |
I don't entirely understand... does "fallible" have a domain-specific definition here? |
@djc - The reason I'm relatively keen on this API is that I think it will take a while to figure out how a potentially more ergonomic Potentially this being behind an Alternatively, as this doesn't rely on any private APIs, this could be moved to documentation or an example as an interim solution. |
I guess my argument is that we could just do a minimal My argument against |
I'd be happy to make this change if it's generally agreed upon. It seems like a good compromise. |
You got a thumbs up from @esheppa, I think that means we have consensus? (Just calling it out to generate a notification.) |
Sorry, started typing this earlier and got distracted. I think it makes sense to go ahead with a On a seperate note - @djc - what is your preference with regards to preferring |
I don't think |
My apologies @avantgardnerio as I know I'd requested earlier to switch usage of |
Cleanup Cleanup no_std No magic micromath trick PR feedback
No worries. I wasn't sure where to put the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thanks for your patience @avantgardnerio - will be great to have this in 0.4.20
!
Thanks @avantgardnerio |
Thanks you :) Very happy to have this in chrono. |
The Apache arrow-datafusion project is a query engine that supports doing date math with arrow primitives like IntervalYearMonth, which despite being a common part of SQL standards, adding months to a NaiveDate was surprisingly non-trivial to perform correctly.
Support is being added to
arrow-datafusion
in this PR, but it seemed like logic like this might best make it's home in thechrono
crate, so that others don't have to re-implement this logic. A quick search of issues reveals some demand:Datelike
should have methods to add/sub a number of calendar day/month/year #290Unfortunately, I realize there is no
Duration::Month
because it doesn't represent a fixed duration in time, so hopefully this is an acceptable compromise. If not, I defer to the maintainers on the best way to implement such functionality.