Skip to content

Commit

Permalink
Editorial: Use Time Record in AddTime
Browse files Browse the repository at this point in the history
Instead of passing each component individually.

See: #2949
  • Loading branch information
ptomato committed Oct 7, 2024
1 parent bee282b commit a6ec4bb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 32 deletions.
10 changes: 8 additions & 2 deletions polyfill/lib/duration.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,10 @@ export class Duration {

if (plainRelativeTo) {
let duration = ES.NormalizeDurationWith24HourDays(this);
const targetTime = ES.AddTime(0, 0, 0, 0, 0, 0, duration.norm);
const targetTime = ES.AddTime(
{ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 },
duration.norm
);

// Delegate the date part addition to the calendar
const isoRelativeToDate = ES.TemporalObjectToISODateRecord(plainRelativeTo);
Expand Down Expand Up @@ -324,7 +327,10 @@ export class Duration {

if (plainRelativeTo) {
const duration = ES.NormalizeDurationWith24HourDays(this);
let targetTime = ES.AddTime(0, 0, 0, 0, 0, 0, duration.norm);
let targetTime = ES.AddTime(
{ hour: 0, minute: 0, second: 0, millisecond: 0, microsecond: 0, nanosecond: 0 },
duration.norm
);

// Delegate the date part addition to the calendar
const isoRelativeToDate = ES.TemporalObjectToISODateRecord(plainRelativeTo);
Expand Down
34 changes: 19 additions & 15 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,15 +2053,15 @@ export function DisambiguatePossibleEpochNanoseconds(possibleEpochNs, timeZone,
switch (disambiguation) {
case 'earlier': {
const norm = TimeDuration.normalize(0, 0, 0, 0, 0, -nanoseconds);
const earlierTime = AddTime(hour, minute, second, millisecond, microsecond, nanosecond, norm);
const earlierTime = AddTime(isoDateTime, norm);
const earlierDate = BalanceISODate(year, month, day + earlierTime.deltaDays);
return GetPossibleEpochNanoseconds(timeZone, { ...earlierTime, ...earlierDate })[0];
}
case 'compatible':
// fall through because 'compatible' means 'later' for "spring forward" transitions
case 'later': {
const norm = TimeDuration.normalize(0, 0, 0, 0, 0, nanoseconds);
const laterTime = AddTime(hour, minute, second, millisecond, microsecond, nanosecond, norm);
const laterTime = AddTime(isoDateTime, norm);
const laterDate = BalanceISODate(year, month, day + laterTime.deltaDays);
const possible = GetPossibleEpochNanoseconds(timeZone, { ...laterTime, ...laterDate });
return possible[possible.length - 1];
Expand Down Expand Up @@ -4208,7 +4208,7 @@ export function DifferenceTemporalZonedDateTime(operation, zonedDateTime, other,
return result;
}

export function AddTime(hour, minute, second, millisecond, microsecond, nanosecond, norm) {
export function AddTime({ hour, minute, second, millisecond, microsecond, nanosecond }, norm) {
second += norm.sec;
nanosecond += norm.subsec;
return BalanceTime(hour, minute, second, millisecond, microsecond, nanosecond);
Expand Down Expand Up @@ -4305,12 +4305,14 @@ export function AddDurationToDateTime(operation, dateTime, durationLike, options

// Add the time part
const timeResult = AddTime(
GetSlot(dateTime, ISO_HOUR),
GetSlot(dateTime, ISO_MINUTE),
GetSlot(dateTime, ISO_SECOND),
GetSlot(dateTime, ISO_MILLISECOND),
GetSlot(dateTime, ISO_MICROSECOND),
GetSlot(dateTime, ISO_NANOSECOND),
{
hour: GetSlot(dateTime, ISO_HOUR),
minute: GetSlot(dateTime, ISO_MINUTE),
second: GetSlot(dateTime, ISO_SECOND),
millisecond: GetSlot(dateTime, ISO_MILLISECOND),
microsecond: GetSlot(dateTime, ISO_MICROSECOND),
nanosecond: GetSlot(dateTime, ISO_NANOSECOND)
},
normalizedDuration.norm
);
const dateDuration = AdjustDateDurationRecord(normalizedDuration.date, timeResult.deltaDays);
Expand Down Expand Up @@ -4345,12 +4347,14 @@ export function AddDurationToTime(operation, temporalTime, durationLike) {
if (operation === 'subtract') duration = CreateNegatedTemporalDuration(duration);
const normalizedDuration = NormalizeDurationWith24HourDays(duration);
let { hour, minute, second, millisecond, microsecond, nanosecond } = AddTime(
GetSlot(temporalTime, ISO_HOUR),
GetSlot(temporalTime, ISO_MINUTE),
GetSlot(temporalTime, ISO_SECOND),
GetSlot(temporalTime, ISO_MILLISECOND),
GetSlot(temporalTime, ISO_MICROSECOND),
GetSlot(temporalTime, ISO_NANOSECOND),
{
hour: GetSlot(temporalTime, ISO_HOUR),
minute: GetSlot(temporalTime, ISO_MINUTE),
second: GetSlot(temporalTime, ISO_SECOND),
millisecond: GetSlot(temporalTime, ISO_MILLISECOND),
microsecond: GetSlot(temporalTime, ISO_MICROSECOND),
nanosecond: GetSlot(temporalTime, ISO_NANOSECOND)
},
normalizedDuration.norm
);
({ hour, minute, second, millisecond, microsecond, nanosecond } = RegulateTime(
Expand Down
6 changes: 4 additions & 2 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,8 @@ <h1>Temporal.Duration.prototype.round ( _roundTo_ )</h1>
1. Return ? UnnormalizeDuration(_normalizedDuration_, _largestUnit_).
1. If _plainRelativeTo_ is not *undefined*, then
1. Let _normalizedDuration_ be NormalizeDurationWith24HourDays(_duration_).
1. Let _targetTime_ be AddTime(0, 0, 0, 0, 0, 0, _normalizedDuration_.[[NormalizedTime]]).
1. Let _midnight_ be Time Record { [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
1. Let _targetTime_ be AddTime(_midnight_, _normalizedDuration_.[[NormalizedTime]]).
1. Let _isoRelativeToDate_ be TemporalObjectToISODateRecord(_plainRelativeTo_).
1. Let _calendar_ be _plainRelativeTo_.[[Calendar]].
1. Let _dateDuration_ be ! AdjustDateDurationRecord(_normalizedDuration_.[[Date]], _targetTime_.[[Days]]).
Expand Down Expand Up @@ -479,7 +480,8 @@ <h1>Temporal.Duration.prototype.total ( _totalOf_ )</h1>
1. Let _total_ be ? DifferenceZonedDateTimeWithTotal(_relativeEpochNs_, _targetEpochNs_, _timeZone_, _calendar_, _unit_).
1. Else if _plainRelativeTo_ is not *undefined*, then
1. Let _normalizedDuration_ be NormalizeDurationWith24HourDays(_duration_).
1. Let _targetTime_ be AddTime(0, 0, 0, 0, 0, 0, _normalizedDuration_.[[NormalizedTime]]).
1. Let _midnight_ be Time Record { [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }.
1. Let _targetTime_ be AddTime(_midnight_, _normalizedDuration_.[[NormalizedTime]]).
1. Let _isoRelativeToDate_ be TemporalObjectToISODateRecord(_plainRelativeTo_).
1. Let _calendar_ be _plainRelativeTo_.[[Calendar]].
1. Let _dateDuration_ be ! AdjustDateDurationRecord(_normalizedDuration_.[[Date]], _targetTime_.[[Days]]).
Expand Down
3 changes: 2 additions & 1 deletion spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,8 @@ <h1>
1. Let _resolvedOptions_ be ? GetOptionsObject(_options_).
1. Let _overflow_ be ? GetTemporalOverflowOption(_resolvedOptions_).
1. Let _normalizedDuration_ be NormalizeDurationWith24HourDays(_duration_).
1. Let _timeResult_ be AddTime(_dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _normalizedDuration_.[[NormalizedTime]]).
1. Let _time_ be Time Record { [[Hour]]: _dateTime_.[[ISOHour]], [[Minute]]: _dateTime_.[[ISOMinute]], [[Second]]: _dateTime_.[[ISOSecond]], [[Millisecond]]: _dateTime_.[[ISOMillisecond]], [[Microsecond]]: _dateTime_.[[ISOMicrosecond]], [[Nanosecond]]: _dateTime_.[[ISONanosecond]] }.
1. Let _timeResult_ be AddTime(_time_, _normalizedDuration_.[[NormalizedTime]]).
1. Let _datePart_ be TemporalObjectToISODateRecord(_dateTime_).
1. Let _dateDuration_ be ? AdjustDateDurationRecord(_normalizedDuration_.[[Date]], _timeResult_.[[Days]]).
1. Let _addedDate_ be ? CalendarDateAdd(_dateTime_.[[Calendar]], _datePart_, _dateDuration_, _overflow_).
Expand Down
16 changes: 6 additions & 10 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -882,12 +882,7 @@ <h1>
<emu-clause id="sec-temporal-addtime" type="abstract operation">
<h1>
AddTime (
_hour_: an integer in the inclusive interval from 0 to 23,
_minute_: an integer in the inclusive interval from 0 to 59,
_second_: an integer in the inclusive interval from 0 to 59,
_millisecond_: an integer in the inclusive interval from 0 to 999,
_microsecond_: an integer in the inclusive interval from 0 to 999,
_nanosecond_: an integer in the inclusive interval from 0 to 999,
_time_: a Time Record
_norm_: a Normalized Time Duration Record,
): a Time Record
</h1>
Expand All @@ -896,9 +891,9 @@ <h1>
<dd></dd>
</dl>
<emu-alg>
1. Set _second_ to _second_ + NormalizedTimeDurationSeconds(_norm_).
1. Set _nanosecond_ to _nanosecond_ + NormalizedTimeDurationSubseconds(_norm_).
1. Return BalanceTime(_hour_, _minute_, _second_, _millisecond_, _microsecond_, _nanosecond_).
1. Let _second_ be _time_.[[Second]] + NormalizedTimeDurationSeconds(_norm_).
1. Let _nanosecond_ be _time_.[[Nanosecond]] + NormalizedTimeDurationSubseconds(_norm_).
1. Return BalanceTime(_time_.[[Hour]], _time_.[[Minute]], _second_, _time_.[[Millisecond]], _time_.[[Microsecond]], _nanosecond_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -1006,7 +1001,8 @@ <h1>
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Let _normalizedDuration_ be NormalizeDuration(_duration_).
1. Let _result_ be AddTime(_temporalTime_.[[ISOHour]], _temporalTime_.[[ISOMinute]], _temporalTime_.[[ISOSecond]], _temporalTime_.[[ISOMillisecond]], _temporalTime_.[[ISOMicrosecond]], _temporalTime_.[[ISONanosecond]], _normalizedDuration_.[[NormalizedTime]]).
1. Let _time_ be Time Record { [[Hour]]: _temporalTime_.[[ISOHour]], [[Minute]]: _temporalTime_.[[ISOMinute]], [[Second]]: _temporalTime_.[[ISOSecond]], [[Millisecond]]: _temporalTime_.[[ISOMillisecond]], [[Microsecond]]: _temporalTime_.[[ISOMicrosecond]], [[Nanosecond]]: _temporalTime_.[[ISONanosecond]] }.
1. Let _result_ be AddTime(_time_, _normalizedDuration_.[[NormalizedTime]]).
1. Return ! CreateTemporalTime(_result_.[[Hour]], _result_.[[Minute]], _result_.[[Second]], _result_.[[Millisecond]], _result_.[[Microsecond]], _result_.[[Nanosecond]]).
</emu-alg>
</emu-clause>
Expand Down
6 changes: 4 additions & 2 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,17 @@ <h1>
1. Assert: abs(_nanoseconds_) ≤ nsPerDay.
1. If _disambiguation_ is ~earlier~, then
1. Let _norm_ be NormalizeTimeDuration(0, 0, 0, 0, 0, -_nanoseconds_).
1. Let _earlierTime_ be AddTime(_isoDateTime_.[[Hour]], _isoDateTime_.[[Minute]], _isoDateTime_.[[Second]], _isoDateTime_.[[Millisecond]], _isoDateTime_.[[Microsecond]], _isoDateTime_.[[Nanosecond]], _norm_).
1. Let _time_ be Time Record { [[Hour]]: _isoDateTime_.[[Hour]], [[Minute]]: _isoDateTime_.[[Minute]], [[Second]]: _isoDateTime_.[[Second]], [[Millisecond]]: _isoDateTime_.[[Millisecond]], [[Microsecond]]: _isoDateTime_.[[Microsecond]], [[Nanosecond]]: _isoDateTime_.[[Nanosecond]] }.
1. Let _earlierTime_ be AddTime(_time_, _norm_).
1. Let _earlierDate_ be BalanceISODate(_isoDateTime_.[[Year]], _isoDateTime_.[[Month]], _isoDateTime_.[[Day]] + _earlierTime_.[[Days]]).
1. Let _earlierDateTime_ be CombineISODateAndTimeRecord(_earlierDate_, _earlierTime_).
1. Set _possibleEpochNs_ to ? GetPossibleEpochNanoseconds(_timeZone_, _earlierDateTime_).
1. Assert: _possibleEpochNs_ is not empty.
1. Return _possibleEpochNs_[0].
1. Assert: _disambiguation_ is ~compatible~ or ~later~.
1. Let _norm_ be NormalizeTimeDuration(0, 0, 0, 0, 0, _nanoseconds_).
1. Let _laterTime_ be AddTime(_isoDateTime_.[[Hour]], _isoDateTime_.[[Minute]], _isoDateTime_.[[Second]], _isoDateTime_.[[Millisecond]], _isoDateTime_.[[Microsecond]], _isoDateTime_.[[Nanosecond]], _norm_).
1. Let _time_ be Time Record { [[Hour]]: _isoDateTime_.[[Hour]], [[Minute]]: _isoDateTime_.[[Minute]], [[Second]]: _isoDateTime_.[[Second]], [[Millisecond]]: _isoDateTime_.[[Millisecond]], [[Microsecond]]: _isoDateTime_.[[Microsecond]], [[Nanosecond]]: _isoDateTime_.[[Nanosecond]] }.
1. Let _laterTime_ be AddTime(_time_, _norm_).
1. Let _laterDate_ be BalanceISODate(_isoDateTime_.[[Year]], _isoDateTime_.[[Month]], _isoDateTime_.[[Day]] + _laterTime_.[[Days]]).
1. Let _laterDateTime_ be CombineISODateAndTimeRecord(_laterDate_, _laterTime_).
1. Set _possibleEpochNs_ to ? GetPossibleEpochNanoseconds(_timeZone_, _laterDateTime_).
Expand Down

0 comments on commit a6ec4bb

Please sign in to comment.