Fix calendar-unit rounding expanding an exact duration#364
Closed
spokodev wants to merge 1 commit into
Closed
Conversation
NudgeToCalendarUnit chose the start bound of the rounding window with 'if (!r1)', looking only at the rounded smallest-unit field. When that field is zero but the rest of startDuration is not (e.g. rounding P1Y to months), it used the origin instant as the start bound instead of the real endpoint of startDuration, so the numerator was non-zero and a directional rounding mode expanded an already-exact result (P1Y became P1Y1M for until/since/round with ceil or floor). Guard on DateDurationSign(startDuration) === 0, matching the reference algorithm.
Contributor
|
Thanks for taking the time to contribute! This fix and its corresponding tests will already be pulled in on our next rebase from https://github.com/tc39/proposal-temporal, so I'll close this. Just watch for #361 and its follow-ups to be merged. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Calendar-unit rounding with a directional rounding mode (
ceil,floor,expand) expands a duration that already lands exactly on the smallest-unit boundary, giving an off-by-one-unit result.Reachable from
Duration.roundand{PlainDate,PlainDateTime,ZonedDateTime}.{until,since}.Duration.totalis unaffected.Cause
In
NudgeToCalendarUnit, the start bound of the rounding window is chosen withif (!r1), wherer1is only the rounded smallest-unit field. Whenr1is zero but the rest ofstartDurationis non-zero (for example roundingP1Yto months, wherestartDurationkeepsyears: 1), it treats the origin instant (relativeTo) as the start bound, even thoughstartDurationactually ends one year later. That makes the bounding numerator non-zero, so a directional rounding mode rounds up tor2.Fix
Guard on
DateDurationSign(startDuration) === 0instead of!r1, so the origin shortcut is used only when the whole start duration is zero. This matches the current reference algorithm (ComputeNudgeWindowin tc39/proposal-temporal).DateDurationSignalready exists in the same file.Testing
Added
test/round-nudge-startbound.mjscovering theceil,floor, andDuration.roundcases above. They returnP1Y1M/-P1Y1Mon the current code and the correctP1Y/-P1Ywith the fix. The full test suite stays green.