Skip to content

Conversation

@catamorphism
Copy link
Contributor

See #3168

@codecov
Copy link

codecov bot commented Oct 28, 2025

Codecov Report

❌ Patch coverage is 85.36585% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 96.85%. Comparing base (c8571ae) to head (88de6b4).
⚠️ Report is 16 commits behind head on main.

Files with missing lines Patch % Lines
polyfill/lib/ecmascript.mjs 81.25% 11 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3172      +/-   ##
==========================================
- Coverage   96.91%   96.85%   -0.06%     
==========================================
  Files          22       22              
  Lines       10209    10270      +61     
  Branches     1839     1848       +9     
==========================================
+ Hits         9894     9947      +53     
- Misses        266      273       +7     
- Partials       49       50       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@ptomato
Copy link
Collaborator

ptomato commented Oct 28, 2025

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 node polyfill/test/thorough/durationrounding.mjs -u and node polyfill/test/thorough/durationtotal.mjs -u, then rebase your fixes on top of it and run them again without -u to compare)

@catamorphism
Copy link
Contributor Author

But I also found one case that seems to produce a wrong result where the old code was correct:

This should be fixed now.

Copy link
Collaborator

@ptomato ptomato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@arshaw If you have a moment to weigh in on this, I'd really appreciate it — you probably have a deeper understanding of the NudgeCalendarUnit algorithm than I do.

Copy link
Collaborator

@ptomato ptomato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I went through the spec text and that made me revisit some things that I didn't catch before — sorry about that!

catamorphism and others added 10 commits November 4, 2025 12:13
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
Co-authored-by: Philip Chimento <philip.chimento@gmail.com>
@catamorphism catamorphism marked this pull request as ready for review November 4, 2025 21:40
@catamorphism catamorphism changed the title DRAFT: Change NudgeToCalendarUnit to use relative date when comparing durations Change NudgeToCalendarUnit to use relative date when comparing durations Nov 4, 2025
Copy link
Collaborator

@ptomato ptomato left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

// Create a separate duration that incorporates roundingIncrement
let r1, r2, startDuration, endDuration;
var didExpandCalendarUnit = false;
const compare = (d1, d2) => CompareDurations(d1, d2, undefined, undefined, calendar, isoDateTime.isoDate, unit, unit);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not need to pass originEpochNs and timeZone into this function? (Or is it only supposed to use the PlainRelativeTo comparison by definition?)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't thought of a test case where the time component matters. But there might be one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants