Skip to content

Commit f77ca4e

Browse files
committed
Normative: Avoid extra Zoned→Plain conversion in AddDuration
This is an addendum to part 3 (#2671) that removes a ZonedDateTime to PlainDateTime conversion in one place where a fast path doesn't need the PlainDateTime. This is observable in the case where you add or subtract two Temporal.Durations that don't have any units higher than hours. Credit to Anba for spotting this. Closes: #2696
1 parent 09c7938 commit f77ca4e

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

polyfill/lib/ecmascript.mjs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4877,7 +4877,10 @@ export function AddDuration(
48774877
const TemporalInstant = GetIntrinsic('%Temporal.Instant%');
48784878
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
48794879
const startInstant = GetSlot(zonedRelativeTo, INSTANT);
4880-
const startDateTime = precalculatedPlainDateTime ?? GetPlainDateTimeFor(timeZoneRec, startInstant, calendar);
4880+
let startDateTime = precalculatedPlainDateTime;
4881+
if (largestUnit === 'year' || largestUnit === 'month' || largestUnit === 'week' || largestUnit === 'day') {
4882+
startDateTime ??= GetPlainDateTimeFor(timeZoneRec, startInstant, calendar);
4883+
}
48814884
const intermediateNs = AddZonedDateTime(
48824885
startInstant,
48834886
timeZoneRec,

spec/duration.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,8 @@ <h1>
16431643
1. Return ! CreateDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _result_.[[Days]], _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
16441644
1. Assert: _zonedRelativeTo_ is not *undefined*.
16451645
1. If _precalculatedPlainDateTime_ is not present, let _precalculatedPlainDateTime_ be *undefined*.
1646-
1. If _precalculatedPlainDateTime_ is *undefined*, then
1646+
1. If _largestUnit_ is *"year"*, or _largestUnit_ is *"month"*, or _largestUnit_ is *"week"*, or _largestUnit_ is *"day"*, let _startDateTimeNeeded_ be *true*; else let _startDateTimeNeeded_ be *false*.
1647+
1. If _precalculatedPlainDateTime_ is *undefined* and _startDateTimeNeeded_ is *true*, then
16471648
1. Let _startDateTime_ be ? GetPlainDateTimeFor(_timeZoneRec_, _zonedRelativeTo_.[[Nanoseconds]], _calendarRec_.[[Receiver]]).
16481649
1. Else,
16491650
1. Let _startDateTime_ be _precalculatedPlainDateTime_.

0 commit comments

Comments
 (0)