Skip to content

Commit

Permalink
Editorial: Refactor AddInstant and AddZonedDateTime to accept a Durat…
Browse files Browse the repository at this point in the history
…ion (Record) rather than individual fields
  • Loading branch information
gibson042 committed Jun 14, 2022
1 parent f92e82e commit ec0f2a3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 49 deletions.
16 changes: 8 additions & 8 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ <h1>
<emu-clause id="sec-temporal-defaulttemporallargestunit" type="abstract operation">
<h1>
DefaultTemporalLargestUnit (
_duration_: a Duration Record,
_duration_: a Duration Record or Temporal.Duration,
)
</h1>
<dl class="header">
Expand Down Expand Up @@ -1119,7 +1119,7 @@ <h1>
1. If Type(_relativeTo_) is not Object or _relativeTo_ does not have an [[InitializedTemporalZonedDateTime]] internal slot, return 0.
1. Let _instant_ be ! CreateTemporalInstant(_relativeTo_.[[Nanoseconds]]).
1. Let _offsetBefore_ be ? GetOffsetNanosecondsFor(_relativeTo_.[[TimeZone]], _instant_).
1. Let _after_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], _y_, _mon_, _w_, _d_, 0, 0, 0, 0, 0, 0).
1. Let _after_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(_y_, _mon_, _w_, _d_, 0, 0, 0, 0, 0, 0)).
1. Let _instantAfter_ be ! CreateTemporalInstant(_after_).
1. Let _offsetAfter_ be ? GetOffsetNanosecondsFor(_relativeTo_.[[TimeZone]], _instantAfter_).
1. Return _offsetAfter_ - _offsetBefore_.
Expand Down Expand Up @@ -1176,7 +1176,7 @@ <h1>
<emu-alg>
1. If _relativeTo_ is not present, set _relativeTo_ to *undefined*.
1. If Type(_relativeTo_) is Object and _relativeTo_ has an [[InitializedTemporalZonedDateTime]] internal slot, then
1. Let _endNs_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], 0, 0, 0, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_).
1. Let _endNs_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(0, 0, 0, _days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_)).
1. Set _nanoseconds_ to ℝ(_endNs_ - _relativeTo_.[[Nanoseconds]]).
1. Else,
1. Set _nanoseconds_ to ! TotalDurationNanoseconds(_days_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_, 0).
Expand Down Expand Up @@ -1444,8 +1444,8 @@ <h1>
1. Assert: _relativeTo_ has an [[InitializedTemporalZonedDateTime]] internal slot.
1. Let _timeZone_ be _relativeTo_.[[TimeZone]].
1. Let _calendar_ be _relativeTo_.[[Calendar]].
1. Let _intermediateNs_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _timeZone_, _calendar_, _duration1_.[[Years]], _duration1_.[[Months]], _duration1_.[[Weeks]], _duration1_.[[Days]], _duration1_.[[Hours]], _duration1_.[[Minutes]], _duration1_.[[Seconds]], _duration1_.[[Milliseconds]], _duration1_.[[Microseconds]], _duration1_.[[Nanoseconds]]).
1. Let _endNs_ be ? AddZonedDateTime(_intermediateNs_, _timeZone_, _calendar_, _duration2_.[[Years]], _duration2_.[[Months]], _duration2_.[[Weeks]], _duration2_.[[Days]], _duration2_.[[Hours]], _duration2_.[[Minutes]], _duration2_.[[Seconds]], _duration2_.[[Milliseconds]], _duration2_.[[Microseconds]], _duration2_.[[Nanoseconds]]).
1. Let _intermediateNs_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _timeZone_, _calendar_, _duration1_).
1. Let _endNs_ be ? AddZonedDateTime(_intermediateNs_, _timeZone_, _calendar_, _duration2_).
1. If _largestUnit_ is not one of *"year"*, *"month"*, *"week"*, or *"day"*, then
1. Let _diffNs_ be ! DifferenceInstant(_relativeTo_.[[Nanoseconds]], _endNs_, 1, *"nanosecond"*, *"halfExpand"*).
1. Assert: The following steps cannot fail due to overflow in the Number domain because abs(_diffNs_) &le; 2 &times; nsMaxInstant.
Expand Down Expand Up @@ -1516,7 +1516,7 @@ <h1>
<dd>It adjusts the calendar part of _zonedDateTime_ for use as the "relative-to" parameter of another operation and returns a new `Temporal.ZonedDateTime` instance.</dd>
</dl>
<emu-alg>
1. Let _intermediateNs_ be ? AddZonedDateTime(_zonedDateTime_.[[Nanoseconds]], _zonedDateTime_.[[TimeZone]], _zonedDateTime_.[[Calendar]], _years_, _months_, _weeks_, _days_, 0, 0, 0, 0, 0, 0).
1. Let _intermediateNs_ be ? AddZonedDateTime(_zonedDateTime_.[[Nanoseconds]], _zonedDateTime_.[[TimeZone]], _zonedDateTime_.[[Calendar]], ! CreateTemporalDurationRecord(_years_, _months_, _weeks_, _days_, 0, 0, 0, 0, 0, 0)).
1. Return ! CreateTemporalZonedDateTime(_intermediateNs_, _zonedDateTime_.[[TimeZone]], _zonedDateTime_.[[Calendar]]).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -1704,8 +1704,8 @@ <h1>
1. If _timeRemainderNs_ = 0, let _direction_ be 0.
1. Else if _timeRemainderNs_ &lt; 0, let _direction_ be -1.
1. Else, let _direction_ be 1.
1. Let _dayStart_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], _duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _dayEnd_ be ? AddZonedDateTime(_dayStart_, _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], 0, 0, 0, _direction_, 0, 0, 0, 0, 0, 0).
1. Let _dayStart_ be ? AddZonedDateTime(_relativeTo_.[[Nanoseconds]], _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], 0, 0, 0, 0, 0, 0)).
1. Let _dayEnd_ be ? AddZonedDateTime(_dayStart_, _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(0, 0, 0, _direction_, 0, 0, 0, 0, 0, 0)).
1. Let _dayLengthNs_ be ℝ(_dayEnd_ - _dayStart_).
1. If (_timeRemainderNs_ - _dayLengthNs_) &times; _direction_ &lt; 0, then
1. Return _duration_.
Expand Down
31 changes: 12 additions & 19 deletions spec/instant.html
Original file line number Diff line number Diff line change
Expand Up @@ -558,25 +558,21 @@ <h1>CompareEpochNanoseconds ( _epochNanosecondsOne_, _epochNanosecondsTwo_ )</h1
<h1>
AddInstant (
_epochNanoseconds_: a BigInt value,
_hours_: an integer,
_minutes_: an integer,
_seconds_: an integer,
_milliseconds_: an integer,
_microseconds_: an integer,
_nanoseconds_: an integer,
_duration_: a Duration Record or Temporal.Duration,
)
</h1>
<dl class="header">
<dt>description</dt>
<dd>It adds a duration in various time units to a number of nanoseconds since the Unix epoch, and returns the result as a BigInt value.</dd>
</dl>
<emu-alg>
1. Let _result_ be _epochNanoseconds_ + ℤ(_nanoseconds_) +
ℤ(_microseconds_) &times; *1000*<sub>ℤ</sub> +
ℤ(_milliseconds_) &times; *10<sup>6</sup>*<sub>ℤ</sub> +
ℤ(_seconds_) &times; *10<sup>9</sup>*<sub>ℤ</sub> +
ℤ(_minutes_) &times; *60*<sub>ℤ</sub> &times; *10<sup>9</sup>*<sub>ℤ</sub> +
ℤ(_hours_) &times; *3600*<sub>ℤ</sub> &times; *10<sup>9</sup>*<sub>ℤ</sub>.
1. Assert: DefaultTemporalLargestUnit(_duration_) is not *"year"*, *"month"*, *"week"*, or *"day"*.
1. Let _result_ be _epochNanoseconds_ + ℤ(_duration_.[[Nanoseconds]]) +
ℤ(_duration_.[[Microseconds]]) &times; *1000*<sub>ℤ</sub> +
ℤ(_duration_.[[Milliseconds]]) &times; *10<sup>6</sup>*<sub>ℤ</sub> +
ℤ(_duration_.[[Seconds]]) &times; *10<sup>9</sup>*<sub>ℤ</sub> +
ℤ(_duration_.[[Minutes]]) &times; *60*<sub>ℤ</sub> &times; *10<sup>9</sup>*<sub>ℤ</sub> +
ℤ(_duration_.[[Hours]]) &times; *3600*<sub>ℤ</sub> &times; *10<sup>9</sup>*<sub>ℤ</sub>.
1. If ! IsValidEpochNanoseconds(_result_) is *false*, throw a *RangeError* exception.
1. Return _result_.
</emu-alg>
Expand Down Expand Up @@ -683,13 +679,10 @@ <h1>
<dd>It adds/subtracts _temporalDurationLike_ to/from _instant_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. If _duration_.[[Days]] is not 0, throw a *RangeError* exception.
1. If _duration_.[[Months]] is not 0, throw a *RangeError* exception.
1. If _duration_.[[Weeks]] is not 0, throw a *RangeError* exception.
1. If _duration_.[[Years]] is not 0, throw a *RangeError* exception.
1. Let _ns_ be ? AddInstant(_instant_.[[Nanoseconds]], _sign_ &times; _duration_.[[Hours]], _sign_ &times; _duration_.[[Minutes]], _sign_ &times; _duration_.[[Seconds]], _sign_ &times; _duration_.[[Milliseconds]], _sign_ &times; _duration_.[[Microseconds]], _sign_ &times; _duration_.[[Nanoseconds]]).
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. If DefaultTemporalLargestUnit(_duration_) is *"year"*, *"month"*, *"week"*, or *"day"*, throw a *RangeError* exception.
1. Let _ns_ be ? AddInstant(_instant_.[[Nanoseconds]], _duration_).
1. Return ! CreateTemporalInstant(_ns_).
</emu-alg>
</emu-clause>
Expand Down
36 changes: 14 additions & 22 deletions spec/zoneddatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ <h1>Temporal.ZonedDateTime.prototype.round ( _roundTo_ )</h1>
1. Let _dtStart_ be ? CreateTemporalDateTime(_temporalDateTime_.[[ISOYear]], _temporalDateTime_.[[ISOMonth]], _temporalDateTime_.[[ISODay]], 0, 0, 0, 0, 0, 0, _isoCalendar_).
1. Let _instantStart_ be ? BuiltinTimeZoneGetInstantFor(_timeZone_, _dtStart_, *"compatible"*).
1. Let _startNs_ be _instantStart_.[[Nanoseconds]].
1. Let _endNs_ be ? AddZonedDateTime(_startNs_, _timeZone_, _calendar_, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0).
1. Let _endNs_ be ? AddZonedDateTime(_startNs_, _timeZone_, _calendar_, ! CreateTemporalDurationRecord(0, 0, 0, 1, 0, 0, 0, 0, 0, 0)).
1. Let _dayLengthNs_ be ℝ(_endNs_ - _startNs_).
1. If _dayLengthNs_ is 0, then
1. Throw a *RangeError* exception.
Expand Down Expand Up @@ -1217,16 +1217,7 @@ <h1>
_epochNanoseconds_: a BigInt,
_timeZone_: an Object,
_calendar_: an Object,
_years_: an integer,
_months_: an integer,
_weeks_: an integer,
_days_: an integer,
_hours_: an integer,
_minutes_: an integer,
_seconds_: an integer,
_milliseconds_: an integer,
_microseconds_: an integer,
_nanoseconds_: an integer,
_duration_: a Duration Record or Temporal.Duration,
optional _options_: an Object,
): either a normal completion containing a BigInt or an abrupt completion
</h1>
Expand All @@ -1240,16 +1231,17 @@ <h1>
<emu-alg>
1. If _options_ is not present, set _options_ to *undefined*.
1. Assert: Type(_options_) is Object or Undefined.
1. If all of _years_, _months_, _weeks_, and _days_ are 0, then
1. Return ? AddInstant(_epochNanoseconds_, _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_).
1. If DefaultTemporalLargestUnit(_duration_) is not *"year"*, *"month"*, *"week"*, or *"day"*, then
1. Return ? AddInstant(_epochNanoseconds_, _duration_).
1. Let _instant_ be ! CreateTemporalInstant(_epochNanoseconds_).
1. Let _temporalDateTime_ be ? BuiltinTimeZoneGetPlainDateTimeFor(_timeZone_, _instant_, _calendar_).
1. Let _datePart_ be ? CreateTemporalDate(_temporalDateTime_.[[ISOYear]], _temporalDateTime_.[[ISOMonth]], _temporalDateTime_.[[ISODay]], _calendar_).
1. Let _dateDuration_ be ! CreateTemporalDuration(_years_, _months_, _weeks_, _days_, 0, 0, 0, 0, 0, 0).
1. Let _dateDuration_ be ! CreateTemporalDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], 0, 0, 0, 0, 0, 0).
1. Let _timeDuration_ be ! CreateTemporalDurationRecord(0, 0, 0, 0, _duration.[[Hours]], _duration.[[Minutes]], _duration.[[Seconds]], _duration.[[Milliseconds]], _duration.[[Microseconds]], _duration.[[Nanoseconds]]).
1. Let _addedDate_ be ? CalendarDateAdd(_calendar_, _datePart_, _dateDuration_, _options_).
1. Let _intermediateDateTime_ be ? CreateTemporalDateTime(_addedDate_.[[ISOYear]], _addedDate_.[[ISOMonth]], _addedDate_.[[ISODay]], _temporalDateTime_.[[ISOHour]], _temporalDateTime_.[[ISOMinute]], _temporalDateTime_.[[ISOSecond]], _temporalDateTime_.[[ISOMillisecond]], _temporalDateTime_.[[ISOMicrosecond]], _temporalDateTime_.[[ISONanosecond]], _calendar_).
1. Let _intermediateInstant_ be ? BuiltinTimeZoneGetInstantFor(_timeZone_, _intermediateDateTime_, *"compatible"*).
1. Return ? AddInstant(_intermediateInstant_.[[Nanoseconds]], _hours_, _minutes_, _seconds_, _milliseconds_, _microseconds_, _nanoseconds_).
1. Return ? AddInstant(_intermediateInstant_.[[Nanoseconds]], _timeDuration_).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -1278,7 +1270,7 @@ <h1>
1. Let _endInstant_ be ! CreateTemporalInstant(_ns2_).
1. Let _endDateTime_ be ? BuiltinTimeZoneGetPlainDateTimeFor(_timeZone_, _endInstant_, _calendar_).
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]], _calendar_, _largestUnit_, _options_).
1. Let _intermediateNs_ be ? AddZonedDateTime(_ns1_, _timeZone_, _calendar_, _dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], 0, 0, 0, 0, 0, 0, 0).
1. Let _intermediateNs_ be ? AddZonedDateTime(_ns1_, _timeZone_, _calendar_, ! CreateTemporalDurationRecord(_dateDifference_.[[Years]], _dateDifference_.[[Months]], _dateDifference_.[[Weeks]], 0, 0, 0, 0, 0, 0, 0)).
1. Let _timeRemainderNs_ be _ns2_ - _intermediateNs_.
1. Let _intermediate_ be ! CreateTemporalZonedDateTime(_intermediateNs_, _timeZone_, _calendar_).
1. Let _result_ be ? NanosecondsToDays(ℝ(_timeRemainderNs_), _intermediate_).
Expand Down Expand Up @@ -1320,15 +1312,15 @@ <h1>
1. Let _endDateTime_ be ? BuiltinTimeZoneGetPlainDateTimeFor(_relativeTo_.[[TimeZone]], _endInstant_, _relativeTo_.[[Calendar]]).
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]], _relativeTo_.[[Calendar]], *"day"*, OrdinaryObjectCreate(*null*)).
1. Let _days_ be _dateDifference_.[[Days]].
1. Let _intermediateNs_ be ℝ(? AddZonedDateTime(ℤ(_startNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], 0, 0, 0, _days_, 0, 0, 0, 0, 0, 0)).
1. Let _intermediateNs_ be ℝ(? AddZonedDateTime(ℤ(_startNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(0, 0, 0, _days_, 0, 0, 0, 0, 0, 0)).
1. If _sign_ is 1, then
1. Repeat, while _days_ &gt; 0 and _intermediateNs_ &gt; _endNs_,
1. Set _days_ to _days_ - 1.
1. Set _intermediateNs_ to ℝ(? AddZonedDateTime(ℤ(_startNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], 0, 0, 0, _days_, 0, 0, 0, 0, 0, 0)).
1. Set _intermediateNs_ to ℝ(? AddZonedDateTime(ℤ(_startNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(0, 0, 0, _days_, 0, 0, 0, 0, 0, 0))).
1. Set _nanoseconds_ to _endNs_ - _intermediateNs_.
1. Let _done_ be *false*.
1. Repeat, while _done_ is *false*,
1. Let _oneDayFartherNs_ be ℝ(? AddZonedDateTime(ℤ(_intermediateNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], 0, 0, 0, _sign_, 0, 0, 0, 0, 0, 0)).
1. Let _oneDayFartherNs_ be ℝ(? AddZonedDateTime(ℤ(_intermediateNs_), _relativeTo_.[[TimeZone]], _relativeTo_.[[Calendar]], ! CreateTemporalDurationRecord(0, 0, 0, _sign_, 0, 0, 0, 0, 0, 0))).
1. Set _dayLengthNs_ to _oneDayFartherNs_ - _intermediateNs_.
1. If (_nanoseconds_ - _dayLengthNs_) &times; _sign_ &ge; 0, then
1. Set _nanoseconds_ to _nanoseconds_ - _dayLengthNs_.
Expand Down Expand Up @@ -1400,12 +1392,12 @@ <h1>
<dd>It adds/subtracts _temporalDurationLike_ to/from _zonedDateTime_.</dd>
</dl>
<emu-alg>
1. If _operation_ is ~subtract~, let _sign_ be -1. Otherwise, let _sign_ be 1.
1. Let _duration_ be ? ToTemporalDurationRecord(_temporalDurationLike_).
1. Let _duration_ be ? ToTemporalDuration(_temporalDurationLike_).
1. If _operation_ is ~subtract~, set _duration_ to CreateNegatedTemporalDuration(_duration_).
1. Set _options_ to ? GetOptionsObject(_options_).
1. Let _timeZone_ be _zonedDateTime_.[[TimeZone]].
1. Let _calendar_ be _zonedDateTime_.[[Calendar]].
1. Let _epochNanoseconds_ be ? AddZonedDateTime(_zonedDateTime_.[[Nanoseconds]], _timeZone_, _calendar_, _sign_ &times; _duration_.[[Years]], _sign_ &times; _duration_.[[Months]], _sign_ &times; _duration_.[[Weeks]], _sign_ &times; _duration_.[[Days]], _sign_ &times; _duration_.[[Hours]], _sign_ &times; _duration_.[[Minutes]], _sign_ &times; _duration_.[[Seconds]], _sign_ &times; _duration_.[[Milliseconds]], _sign_ &times; _duration_.[[Microseconds]], _sign_ &times; _duration_.[[Nanoseconds]], _options_).
1. Let _epochNanoseconds_ be ? AddZonedDateTime(_zonedDateTime_.[[Nanoseconds]], _timeZone_, _calendar_, _duration_, _options_).
1. Return ! CreateTemporalZonedDateTime(_epochNanoseconds_, _timeZone_, _calendar_).
</emu-alg>
</emu-clause>
Expand Down

0 comments on commit ec0f2a3

Please sign in to comment.