Skip to content

Commit

Permalink
fixup! Normative: Precalculate PlainDateTime from ZonedDateTime in mo…
Browse files Browse the repository at this point in the history
…re places
  • Loading branch information
ptomato committed Sep 28, 2023
1 parent 817a3d7 commit a2239a8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 12 additions & 4 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3535,19 +3535,27 @@ export function BalancePossiblyInfiniteTimeDurationRelative(
precalculatedPlainDateTime
) {
const startNs = GetSlot(zonedRelativeTo, EPOCHNANOSECONDS);
const startInstant = GetSlot(zonedRelativeTo, INSTANT);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);

let intermediateNs = startNs;
if (days !== 0) {
const startInstant = GetSlot(zonedRelativeTo, INSTANT);
const timeZone = GetSlot(zonedRelativeTo, TIME_ZONE);
const startDt = precalculatedPlainDateTime ?? GetPlainDateTimeFor(timeZone, startInstant, 'iso8601');
intermediateNs = AddDaysToZonedDateTime(startInstant, startDt, timeZone, 'iso8601', days, 'constrain').epochNs;
precalculatedPlainDateTime ??= GetPlainDateTimeFor(timeZone, startInstant, 'iso8601');
intermediateNs = AddDaysToZonedDateTime(
startInstant,
precalculatedPlainDateTime,
timeZone,
'iso8601',
days,
'constrain'
).epochNs;
}

const endNs = AddInstant(intermediateNs, hours, minutes, seconds, milliseconds, microseconds, nanoseconds);
nanoseconds = endNs.subtract(startNs);

if (largestUnit === 'year' || largestUnit === 'month' || largestUnit === 'week' || largestUnit === 'day') {
if (!nanoseconds.isZero()) precalculatedPlainDateTime ??= GetPlainDateTimeFor(timeZone, startInstant, 'iso8601');
({ days, nanoseconds } = NanosecondsToDays(nanoseconds, zonedRelativeTo, precalculatedPlainDateTime));
largestUnit = 'hour';
} else {
Expand Down
7 changes: 4 additions & 3 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -1344,14 +1344,15 @@ <h1>
<emu-alg>
1. If _precalculatedPlainDateTime_ is not present, let _precalculatedPlainDateTime_ be *undefined*.
1. Let _intermediateNs_ be _zonedRelativeTo_.[[Nanoseconds]].
1. Let _startInstant_ be ! CreateTemporalInstant(_zonedRelativeTo_.[[Nanoseconds]]).
1. If _days_ &ne; 0, then
1. Let _startInstant_ be ! CreateTemporalInstant(_zonedRelativeTo_.[[Nanoseconds]]).
1. If _precalculatedPlainDateTime_ is not *undefined*, let _startDateTime_ be _precalculatedPlainDateTime_; else let _startDateTime_ be ? GetPlainDateTimeFor(_zonedRelativeTo_.[[TimeZone]], _startInstant_, *"iso8601"*).
1. Let _intermediateResult_ be ? AddDaysToZonedDateTime(_startInstant_, _startDateTime_, _zonedRelativeTo_.[[TimeZone]], *"iso8601"*, _days_, *"constrain"*).
1. If _precalculatedPlainDateTime_ is *undefined*, set _precalculatedPlainDateTime_ to ? GetPlainDateTimeFor(_zonedRelativeTo_.[[TimeZone]], _startInstant_, *"iso8601"*).
1. Let _intermediateResult_ be ? AddDaysToZonedDateTime(_startInstant_, _precalculatedPlainDateTime_, _zonedRelativeTo_.[[TimeZone]], *"iso8601"*, _days_, *"constrain"*).
1. Set _intermediateNs_ to _intermediateResult_.[[EpochNanoseconds]].
1. Let _endNs_ be AddInstant(_intermediateNs_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_).
1. Set _nanoseconds_ to ℝ(_endNs_ - _zonedRelativeTo_.[[Nanoseconds]]).
1. If _largestUnit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
1. If _nanoseconds_ &ne; 0 and _precalculatedPlainDateTime_ is *undefined*, set _precalculatedPlainDateTime_ to ? GetPlainDateTimeFor(_zonedRelativeTo_.[[TimeZone]], _startInstant_, *"iso8601"*).
1. Let _result_ be ? NanosecondsToDays(_nanoseconds_, _zonedRelativeTo_, _precalculatedPlainDateTime_).
1. Set _days_ to _result_.[[Days]].
1. Set _largestUnit_ to *"hour"*.
Expand Down

0 comments on commit a2239a8

Please sign in to comment.