Skip to content

Commit

Permalink
Normative: Avoid extra Zoned→Plain conversion in AddDuration
Browse files Browse the repository at this point in the history
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
  • Loading branch information
ptomato committed Nov 6, 2023
1 parent bfc9290 commit b7eea83
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
5 changes: 4 additions & 1 deletion polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4877,7 +4877,10 @@ export function AddDuration(
const TemporalInstant = GetIntrinsic('%Temporal.Instant%');
const calendar = GetSlot(zonedRelativeTo, CALENDAR);
const startInstant = GetSlot(zonedRelativeTo, INSTANT);
const startDateTime = precalculatedPlainDateTime ?? GetPlainDateTimeFor(timeZoneRec, startInstant, calendar);
let startDateTime = precalculatedPlainDateTime;
if (largestUnit === 'year' || largestUnit === 'month' || largestUnit === 'week' || largestUnit === 'day') {
startDateTime ??= GetPlainDateTimeFor(timeZoneRec, startInstant, calendar);
}
const intermediateNs = AddZonedDateTime(
startInstant,
timeZoneRec,
Expand Down
3 changes: 2 additions & 1 deletion spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -1643,7 +1643,8 @@ <h1>
1. Return ! CreateDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _result_.[[Days]], _result_.[[Hours]], _result_.[[Minutes]], _result_.[[Seconds]], _result_.[[Milliseconds]], _result_.[[Microseconds]], _result_.[[Nanoseconds]]).
1. Assert: _zonedRelativeTo_ is not *undefined*.
1. If _precalculatedPlainDateTime_ is not present, let _precalculatedPlainDateTime_ be *undefined*.
1. If _precalculatedPlainDateTime_ is *undefined*, then
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*.
1. If _precalculatedPlainDateTime_ is *undefined* and _startDateTimeNeeded_ is *true*, then
1. Let _startDateTime_ be ? GetPlainDateTimeFor(_timeZoneRec_, _zonedRelativeTo_.[[Nanoseconds]], _calendarRec_.[[Receiver]]).
1. Else,
1. Let _startDateTime_ be _precalculatedPlainDateTime_.
Expand Down

0 comments on commit b7eea83

Please sign in to comment.