Skip to content

Commit

Permalink
Normative: Fix intermediate value in ZonedDateTime difference
Browse files Browse the repository at this point in the history
To find the difference between two ZonedDateTimes, we first find the
calendar difference between their date parts. Then we find the time
difference between an intermediate ZonedDateTime value (calculated by
adding the calendar parts to the first ZonedDateTime) and the second
ZonedDateTime.

Previously we would calculate the intermediate value by adding years,
months, and weeks from the date difference, because the days were
calculated as part of the time difference.

This was incorrect, because the intermediate value can shift if it falls
in the middle of a DST transition. However, that could be on a completely
different date than would be expected, leading to surprising results like
this:

const duration = Temporal.Duration.from({ months: 1, days: 15, hours: 12 });
const past = Temporal.ZonedDateTime.from('2024-02-10T02:00[America/New_York]');
const future = past.add(duration);

duration  // => 1 month, 15 days, 12 hours
past.until(future, { largestUnit: 'months' })  // => 1 month, 15 days, 11 hours

This result would occur because of a DST change on 2024-03-10T02:00,
unrelated to either the start date of 2024-02-10 or the end date of
2024-03-25. With this change, the intermediate value occurs on
2024-03-25T02:00 and would only shift if that was when the DST change
occurred.
  • Loading branch information
ptomato committed Jan 27, 2024
1 parent ae85b3c commit 50724e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 16 deletions.
18 changes: 5 additions & 13 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4086,7 +4086,7 @@ export function DifferenceZonedDateTime(
years,
months,
weeks,
0,
days,
0,
0,
0,
Expand All @@ -4098,20 +4098,12 @@ export function DifferenceZonedDateTime(
// may disambiguate
let timeRemainderNs = ns2.subtract(intermediateNs);
const intermediate = CreateTemporalZonedDateTime(intermediateNs, timeZoneRec.receiver, calendarRec.receiver);
({ nanoseconds: timeRemainderNs, days } = NanosecondsToDays(timeRemainderNs, intermediate, timeZoneRec));
let deltaDays;
({ nanoseconds: timeRemainderNs, days: deltaDays } = NanosecondsToDays(timeRemainderNs, intermediate, timeZoneRec));

// Finally, merge the date and time durations and return the merged result.
let { hours, minutes, seconds, milliseconds, microseconds, nanoseconds } = BalanceTimeDuration(
0,
0,
0,
0,
0,
0,
timeRemainderNs,
'hour'
);
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds };
let balanceResult = BalanceTimeDuration(days + deltaDays, 0, 0, 0, 0, 0, timeRemainderNs, 'day');
return { years, months, weeks, ...balanceResult };
}

export function GetDifferenceSettings(op, options, group, disallowed, fallbackSmallest, smallestLargestDefaultUnit) {
Expand Down
6 changes: 3 additions & 3 deletions spec/zoneddatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1405,12 +1405,12 @@ <h1>
1. Let _endInstant_ be ! CreateTemporalInstant(_ns2_).
1. Let _endDateTime_ be ? GetPlainDateTimeFor(_timeZoneRec_, _endInstant_, _calendarRec_.[[Receiver]]).
1. Let _dateDifference_ be ? DifferenceISODateTime(_startDateTime_.[[ISOYear]], _startDateTime_.[[ISOMonth]], _startDateTime_.[[ISODay]], _startDateTime_.[[ISOHour]], _startDateTime_.[[ISOMinute]], _startDateTime_.[[ISOSecond]], _startDateTime_.[[ISOMillisecond]], _startDateTime_.[[ISOMicrosecond]], _startDateTime_.[[ISONanosecond]], _endDateTime_.[[ISOYear]], _endDateTime_.[[ISOMonth]], _endDateTime_.[[ISODay]], _endDateTime_.[[ISOHour]], _endDateTime_.[[ISOMinute]], _endDateTime_.[[ISOSecond]], _endDateTime_.[[ISOMillisecond]], _endDateTime_.[[ISOMicrosecond]], _endDateTime_.[[ISONanosecond]], _calendarRec_, _largestUnit_, _options_).
1. Let _intermediateNs_ be ? AddZonedDateTime(_ns1_, _timeZoneRec_, _calendarRec_, _dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], 0, 0, 0, 0, 0, 0, 0, _startDateTime_).
1. Let _intermediateNs_ be ? AddZonedDateTime(_ns1_, _timeZoneRec_, _calendarRec_, _dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _dateDifference_.[[Days]], 0, 0, 0, 0, 0, 0, _startDateTime_).
1. Let _timeRemainderNs_ be _ns2_ - _intermediateNs_.
1. Let _intermediate_ be ! CreateTemporalZonedDateTime(_intermediateNs_, _timeZoneRec_.[[Receiver]], _calendarRec_.[[Receiver]]).
1. Let _result_ be ? NanosecondsToDays(ℝ(_timeRemainderNs_), _intermediate_, _timeZoneRec_).
1. Let _timeDifference_ be ! BalanceTimeDuration(0, 0, 0, 0, 0, 0, _result_.[[Nanoseconds]], *"hour"*).
1. Return ! CreateDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _result_.[[Days]], _timeDifference_.[[Hours]], _timeDifference_.[[Minutes]], _timeDifference_.[[Seconds]], _timeDifference_.[[Milliseconds]], _timeDifference_.[[Microseconds]], _timeDifference_.[[Nanoseconds]]).
1. Let _timeDifference_ be ! BalanceTimeDuration(_dateDifference_.[[Days]] + _result_.[[Days]], 0, 0, 0, 0, 0, _result_.[[Nanoseconds]], *"day"*).
1. Return ! CreateDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], _timeDifference_.[[Days]], _timeDifference_.[[Hours]], _timeDifference_.[[Minutes]], _timeDifference_.[[Seconds]], _timeDifference_.[[Milliseconds]], _timeDifference_.[[Microseconds]], _timeDifference_.[[Nanoseconds]]).
</emu-alg>
</emu-clause>

Expand Down

0 comments on commit 50724e9

Please sign in to comment.