-
Notifications
You must be signed in to change notification settings - Fork 156
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
How does plus
work?
#58
Comments
This is a good question. I think options 1 and 2 make the most sense to me. Any preferences after up or down? I think we have usually chosen up due to the fact that time only moves forward. |
I think the choice is mostly arbitrary; rounding up is fine. But do note that options 3 and 4 are more general; option 1 or 2 can be built on top of them (e.g., |
That's a decent argument in favor for 3/4, imo |
Just a small thought, can I add a |
@MrRhodes, do you mean using the .plus function? The CivilTime and CivilDate objects have functions that return CivilDateTime objects (the .withDate and .withTime functions, respectively). |
The way I read the original spec (and wrongly so) was to add in increasing order of magnitude: 2016-02-29 + 2m => 2016-04-29 which would eliminate rounding entirely. Are we sure we want to add in descending order? |
@pipobscure Adding in increasing order would eliminate rounding for that particular example, but introduce it for other cases: new CivilDate(2015, 1, 29).plus({years: 1, months: 1}); In decreasing order: In increasing order: Regardless of whether the parts are added in increasing or decreasing order, there's going to be a case that hits an intermediate rounding problem. My vote is for option 3/4 where the rounding only happens after all parts are added. |
FWIW, my vote also goes for 3/4. About rounding up or down, picking the "correct" form seems challenging. Follow an observation from the following use cases: Rounding down:
Rounding up:
My observation so far (from the above use cases)... It seems like it's desirable to always round up the units with lower precision that the one requested (e.g., month when requested is day) and round down the units with higher precision than the one requested (e.g., month when requested is year). Is there any other use case that breaks this observation? |
JSR-310 does the following:
This is essentially option 3 above, although expressed in a different way. I still think it is the best choice Options 1 and 2 have nasty effects with the varying month length. I don't believe rounding up is helpful either when doing date time math - if I add 3 months to a date in January I expect to get a date in April, not a date in May.. |
TL;DR: I don't care what we do, let's just decide and be explicit. The naive way I did this so far is to round after each step.
2015-01-29 + (1y 1m) => 2016-02-29 (invalid) => 2016-03-01 I'm not saying this is correct, but it's what I did in the polyfill for now. I'm not even sure there is a right way™️ to do this. I just had some conversations with folks over the last few weeks and found that depending on where they are, they have entirely different expectations of the result.
So I think we just need to be absolutely explicit on the rules. This becomes especially relevant when mixing date & time math as they are entirely different animals. Which happens before/after which. |
I’ve finally worked this out this weekend in the polyfill. I’ve also created a test (or 8070583 tests really) for adding and subtracting all differences for all dates between 1999-01-01 and 2009-12-31. the properties are additional property is that .difference() always produces durations that in the above operations do not need disambiguation. |
From the current README:
Can you add an explanation of this result? Assuming parts are added one-by-one (as opposed to e.g. translating them into nanosecond-counted durations up front, which would be weird because real years and months vary in length), I see at least three possible algorithms:
Option 1: Correct after each part, rounding up
not recommended, can be recovered by chaining calls that don't mix fixed- and variable-length units
Option 2: Correct after each part, rounding down
not recommended, can be recovered by chaining calls that don't mix fixed- and variable-length units
Option 3 (4): Correct at the end, rounding up (down)
rounding TBD, see #58 (comment)
The text was updated successfully, but these errors were encountered: