Change NudgeToCalendarUnit to use relative date when comparing durations - #3172
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3172 +/- ##
==========================================
- Coverage 96.84% 96.49% -0.35%
==========================================
Files 22 22
Lines 10235 10305 +70
Branches 1841 1846 +5
==========================================
+ Hits 9912 9944 +32
- Misses 273 311 +38
Partials 50 50 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
So I haven't sat down yet to think through the code but I did run this on my in-progress snapshot tests. It definitely fixes the assertion failures, and the results seem correct: Temporal.Duration.from({ months: 1, hours: 10 }).round({ smallestUnit: 'months', roundingMode: 'expand', relativeTo: '2020-01-31' })
// old: assertion failure
// new: P2M (seems correct)
Temporal.Duration.from({ years: 2345, hours: 12 }).round({ smallestUnit: 'years', roundingMode: 'expand', relativeTo: '2020-02-29' })
// old: assertion failure
// new: P2346Y (seems correct)
Temporal.Duration.from({ months: 1, hours: 10 }).total({ unit: 'months', relativeTo: '2020-01-31' })
// old: assertion failure
// new: 1.0134408602150538
// (seems correct: the bounding box is from 2020-02-29T00:00 (relativeTo + 1 month)
// to 2020-03-31T00:00 (relativeTo + 2 months), which is 744 hours, and the result is
// the floating point approximation of 1+10/744)(Additionally, these cases produced wrong answers in a build of the existing code with assertions disabled) But I also found one case that seems to produce a wrong result where the old code was correct: Temporal.Duration.from({ years: 1 }).round({ smallestUnit: 'months', relativeTo: '2020-02-29' })
// old: P1Y
// new: P12M (seems wrong, should balance back up to the implicit largestUnit in the original duration)(I'd have liked to push an in-progress branch with the snapshot tests but currently the testing space is too large, and the snapshots run afoul of GitHub's file size limit. So here's what I've currently got, without snapshots: bd34a53 To recreate the snapshots, run |
a3f6b1f to
47ad4c4
Compare
This should be fixed now. |
60b2640 to
d910018
Compare
ptomato
left a comment
There was a problem hiding this comment.
Thanks, I went through the spec text and that made me revisit some things that I didn't catch before — sorry about that!
b241de7 to
d4d36bc
Compare
ptomato
left a comment
There was a problem hiding this comment.
Just nitpicks at this point. Ready to present to TC39 in the November meeting.
|
Draft until presented to TC39 |
|
I finally got a chance to look at this, sorry for the delay. I think it's really good and the same general approach I would have taken, which is to shift the r1->r2 window by one increment if an overflow situation occurs. I feel there might be room for some minor code DRY-ing and optimization however. I see that an out-of-bounds situation is preemptively detected via Also, I see that IF an overflow is detected, the logic for shifting the window by an increment is rather repetitive across the unit:year and unit:month cases. It'd be nice if we could unify that. So, instead of a new I'm interested to hear of @catamorphism and @ptomato think this refactor would be worthwhile. Unfortunately I don't have time to try this myself, and who knows, maybe this new abstraction makes things look overly confusing after all is said and done, but could be worth a shot. |
|
@catamorphism the new code looks great to me! I'll leave the nitpicking & bike shedding to @ptomato :) |
ptomato
left a comment
There was a problem hiding this comment.
This PR reached consensus in the 2025-11-18 TC39 plenary.
I was making one last pass through the spec text and I noticed some spec notation nitpicks. Otherwise good to go.
ptomato
left a comment
There was a problem hiding this comment.
A couple of stragglers from the last round of comments.
…ring durations See tc39#3168
3ea831b to
5dd0b0d
Compare
… when comparing durations. r=spidermonkey-reviewers,mgaudet Implements the changes from: <tc39/proposal-temporal#3172> Differential Revision: https://phabricator.services.mozilla.com/D274333
… when comparing durations. r=spidermonkey-reviewers,mgaudet Implements the changes from: <tc39/proposal-temporal#3172> Differential Revision: https://phabricator.services.mozilla.com/D274333 UltraBlame original commit: 0821da0dc725fc8a49adacd6c207316293dd5dc8
… when comparing durations. r=spidermonkey-reviewers,mgaudet Implements the changes from: <tc39/proposal-temporal#3172> Differential Revision: https://phabricator.services.mozilla.com/D274333 UltraBlame original commit: 0821da0dc725fc8a49adacd6c207316293dd5dc8
… when comparing durations. r=spidermonkey-reviewers,mgaudet Implements the changes from: <tc39/proposal-temporal#3172> Differential Revision: https://phabricator.services.mozilla.com/D274333 UltraBlame original commit: 0821da0dc725fc8a49adacd6c207316293dd5dc8
… when comparing durations. r=spidermonkey-reviewers,mgaudet Implements the changes from: <tc39/proposal-temporal#3172> Differential Revision: https://phabricator.services.mozilla.com/D274333
Fixes #635 Updates to tc39/proposal-temporal#3172 <s>This passes the test, but it fails other tests. The problem is probably that IncrementRounder isn't written in terms of r1 and r2. @nekevss, I don't understand the full purpose of IncrementRounder being written the way it is: mind having a look? The new spec text changes how r1 and r2 are computed, and they're passed down to IncrementRounder in the spec but not the code, so I assume that IncrementRounder is recomputing them somehow from the inputs.</s> I wrote this to not use IncrementRounder. I'm not 100% sure if the implementation is correct when it comes to how it handles mathematical values. --------- Co-authored-by: Kevin Ness <nekevss@gmail.com> Co-authored-by: Kevin Ness <46825870+nekevss@users.noreply.github.com>
See #3168