From b70e3494973120c586e1beceb213ac9803e8e545 Mon Sep 17 00:00:00 2001 From: Philip Chimento Date: Fri, 19 Jan 2024 11:44:31 -0800 Subject: [PATCH] Normative: Fix Duration rounding relative to ZonedDateTime 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 --- polyfill/lib/duration.mjs | 15 ++++++++++++--- spec/duration.html | 3 ++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/polyfill/lib/duration.mjs b/polyfill/lib/duration.mjs index 4b8856ce06..6bd5d3f13f 100644 --- a/polyfill/lib/duration.mjs +++ b/polyfill/lib/duration.mjs @@ -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( diff --git a/spec/duration.html b/spec/duration.html index 3d02bfa378..1944a3aba7 100644 --- a/spec/duration.html +++ b/spec/duration.html @@ -474,7 +474,8 @@

Temporal.Duration.prototype.round ( _roundTo_ )

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_).