Skip to content

Commit

Permalink
Normative: Fix Duration rounding relative to ZonedDateTime
Browse files Browse the repository at this point in the history
When the time portion of a duration, rounded relative to a ZonedDateTime,
would land on a non-24-hour day, we'd get incorrect results. This is a
regression from #2508 where although switching the order of
BalanceDateDurationRelative and BalanceTimeDurationRelative was correct,
we should not have removed the MoveRelativeZonedDateTime call.

Closes: #2742
  • Loading branch information
ptomato committed Apr 27, 2024
1 parent 3ae28f4 commit 5cbf708
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 12 additions & 3 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,22 @@ export class Duration {
timeZoneRec,
precalculatedPlainDateTime
));
const intermediate = ES.MoveRelativeZonedDateTime(
zonedRelativeTo,
calendarRec,
timeZoneRec,
years,
months,
weeks,
0,
precalculatedPlainDateTime
);
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDurationRelative(
days,
norm,
largestUnit,
zonedRelativeTo,
timeZoneRec,
precalculatedPlainDateTime
intermediate,
timeZoneRec
));
} else {
({ days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = ES.BalanceTimeDuration(
Expand Down
3 changes: 2 additions & 1 deletion spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ <h1>Temporal.Duration.prototype.round ( _roundTo_ )</h1>
1. Let _roundResult_ be _roundRecord_.[[NormalizedDuration]].
1. If _zonedRelativeTo_ is not *undefined*, then
1. Set _roundResult_ to ? AdjustRoundedDurationDays(_roundResult_.[[Years]], _roundResult_.[[Months]], _roundResult_.[[Weeks]], _roundResult_.[[Days]], _roundResult_.[[NormalizedTime]], _roundingIncrement_, _smallestUnit_, _roundingMode_, _zonedRelativeTo_, _calendarRec_, _timeZoneRec_, _precalculatedPlainDateTime_).
1. Let _balanceResult_ be ? BalanceTimeDurationRelative(_roundResult_.[[Days]], _roundResult_.[[NormalizedTime]], _largestUnit_, _zonedRelativeTo_, _timeZoneRec_, _precalculatedPlainDateTime_).
1. Let _intermediate_ be ? MoveRelativeZonedDateTime(_zonedRelativeTo_, _calendarRec_, _timeZoneRec_, _roundResult_.[[Years]], _roundResult_.[[Months]], _roundResult_.[[Weeks]], 0, _precalculatedPlainDateTime_).
1. Let _balanceResult_ be ? BalanceTimeDurationRelative(_roundResult_.[[Days]], _roundResult_.[[NormalizedTime]], _largestUnit_, _intermediate_, _timeZoneRec_, _precalculatedPlainDateTime_).
1. Else,
1. Let _normWithDays_ be ? Add24HourDaysToNormalizedTimeDuration(_roundResult_.[[NormalizedTime]], _roundResult_.[[Days]]).
1. Let _balanceResult_ be BalanceTimeDuration(_normWithDays_, _largestUnit_).
Expand Down

0 comments on commit 5cbf708

Please sign in to comment.