Skip to content

Commit

Permalink
Editorial: Refactor Duration to use IsCalendarUnit
Browse files Browse the repository at this point in the history
Use this operation in the spec as well, for readability and conciseness.
  • Loading branch information
ptomato committed Jan 31, 2024
1 parent 897e303 commit 0451f32
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 18 additions & 0 deletions spec/abstractops.html
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,24 @@ <h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-iscalendarunit" type="abstract operation">
<h1>
IsCalendarUnit (
_unit_: a String from the Singular column of <emu-xref href="#table-temporal-units"></emu-xref>,
): a Boolean
</h1>
<dl class="header">
<dt>description</dt>
<dd>It returns whether _unit_ is a Temporal unit for which rounding would require calendar calculations.</dd>
</dl>
<emu-alg>
1. If _unit_ is *"year"*, return *true*.
1. If _unit_ is *"month"*, return *true*.
1. If _unit_ is *"week"*, return *true*.
1. Return *false*.
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-maximumtemporaldurationroundingincrement" type="abstract operation">
<h1>
MaximumTemporalDurationRoundingIncrement (
Expand Down
16 changes: 8 additions & 8 deletions spec/duration.html
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ <h1>Temporal.Duration.prototype.round ( _roundTo_ )</h1>
1. NOTE: The above conditions mean that the operation will have no effect: the smallest unit and rounding increment will leave the total duration unchanged, and it can be determined without calling a calendar or time zone method that no balancing will take place.
1. Return ! CreateTemporalDuration(_duration_.[[Years]], _duration_.[[Months]], _duration_.[[Weeks]], _duration_.[[Days]], _duration_.[[Hours]], _duration_.[[Minutes]], _duration_.[[Seconds]], _duration_.[[Milliseconds]], _duration_.[[Microseconds]], _duration_.[[Nanoseconds]]).
1. Let _precalculatedPlainDateTime_ be *undefined*.
1. If _roundingGranularityIsNoop_ is *false*, or _largestUnit_ is *"year"*, or _largestUnit_ is *"month"*, or _largestUnit_ is *"week"*, or _largestUnit_ is *"day"*, or _calendarUnitsPresent_ is *true*, or _duration_.[[Days]] &ne; 0, let _plainDateTimeOrRelativeToWillBeUsed_ be *true*; else let _plainDateTimeOrRelativeToWillBeUsed_ be *false*.
1. If _roundingGranularityIsNoop_ is *false*, or IsCalendarUnit(_largestUnit_) is *true*, or _largestUnit_ is *"day"*, or _calendarUnitsPresent_ is *true*, or _duration_.[[Days]] &ne; 0, let _plainDateTimeOrRelativeToWillBeUsed_ be *true*; else let _plainDateTimeOrRelativeToWillBeUsed_ be *false*.
1. If _zonedRelativeTo_ is not *undefined* and _plainDateTimeOrRelativeToWillBeUsed_ is *true*, then
1. NOTE: The above conditions mean that the corresponding `Temporal.PlainDateTime` or `Temporal.PlainDate` for _zonedRelativeTo_ will be used in one of the operations below.
1. Let _instant_ be ! CreateTemporalInstant(_zonedRelativeTo_.[[Nanoseconds]]).
Expand Down Expand Up @@ -505,7 +505,7 @@ <h1>Temporal.Duration.prototype.total ( _totalOf_ )</h1>
1. Let _timeZoneRec_ be _relativeToRecord_.[[TimeZoneRec]].
1. Let _unit_ be ? GetTemporalUnit(_totalOf_, *"unit"*, ~datetime~, ~required~).
1. Let _precalculatedPlainDateTime_ be *undefined*.
1. If _unit_ is *"year"*, or _unit_ is *"month"*, or _unit_ is *"week"*, or _unit_ is *"day"*, or _duration_.[[Years]] &ne; 0, or _duration_.[[Months]] &ne; 0, or _duration_.[[Weeks]] &ne; 0, or _duration_.[[Days]] &ne; 0, let _plainDateTimeOrRelativeToWillBeUsed_ be *true*; else let _plainDateTimeOrRelativeToWillBeUsed_ be *false*.
1. If IsCalendarUnit(_unit_) is *true*, or _unit_ is *"day"*, or _duration_.[[Years]] &ne; 0, or _duration_.[[Months]] &ne; 0, or _duration_.[[Weeks]] &ne; 0, or _duration_.[[Days]] &ne; 0, let _plainDateTimeOrRelativeToWillBeUsed_ be *true*; else let _plainDateTimeOrRelativeToWillBeUsed_ be *false*.
1. If _zonedRelativeTo_ is not *undefined* and _plainDateTimeOrRelativeToWillBeUsed_ is *true*, then
1. NOTE: The above conditions mean that the corresponding `Temporal.PlainDateTime` or `Temporal.PlainDate` for _zonedRelativeTo_ will be used in one of the operations below.
1. Let _instant_ be ! CreateTemporalInstant(_zonedRelativeTo_.[[Nanoseconds]]).
Expand All @@ -528,7 +528,7 @@ <h1>Temporal.Duration.prototype.total ( _totalOf_ )</h1>
1. Let _intermediateNs_ be _startNs_.
1. Let _endNs_ be ? AddInstant(_intermediateNs_, _norm_).
1. Set _norm_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_endNs_, _startNs_).
1. If _unit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
1. If IsCalendarUnit(_unit_) is *true* or _unit_ is *"day"*, then
1. If NormalizedTimeDurationIsZero(_norm_) is *false* and _startDateTime_ is *undefined*, set _startDateTime_ to ? GetPlainDateTimeFor(_timeZoneRec_, _startInstant_, *"iso8601"*).
1. Let _result_ be ? NormalizedTimeDurationToDays(_norm_, _intermediate_, _timeZoneRec_, _startDateTime_).
1. Set _norm_ to _result_.[[Remainder]].
Expand Down Expand Up @@ -1708,7 +1708,7 @@ <h1>
1. Let _endNs_ be ? AddInstant(_intermediateNs_, _norm_).
1. Set _norm_ to NormalizedTimeDurationFromEpochNanosecondsDifference(_endNs_, _startNs_).
1. If NormalizedTimeDurationIsZero(_norm_) is *true*, return ! CreateTimeDurationRecord(0, 0, 0, 0, 0, 0, 0).
1. If _largestUnit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
1. If IsCalendarUnit(_largestUnit_) is *true* or _largestUnit_ is *"day"*, then
1. If _precalculatedPlainDateTime_ is *undefined*, set _precalculatedPlainDateTime_ to ? GetPlainDateTimeFor(_timeZoneRec_, _startInstant_, *"iso8601"*).
1. Let _result_ be ? NormalizedTimeDurationToDays(_norm_, _zonedRelativeTo_, _timeZoneRec_, _precalculatedPlainDateTime_).
1. Set _days_ to _result_.[[Days]].
Expand Down Expand Up @@ -1901,7 +1901,7 @@ <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 _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 IsCalendarUnit(_largestUnit_) is *true*, 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,
Expand Down Expand Up @@ -2021,11 +2021,11 @@ <h1>
1. If _plainRelativeTo_ is not present, set _plainRelativeTo_ to *undefined*.
1. If _zonedRelativeTo_ is not present, set _zonedRelativeTo_ to *undefined*.
1. If _precalculatedPlainDateTime_ is not present, set _precalculatedPlainDateTime_ to *undefined*.
1. If _unit_ is *"year"*, *"month"*, or *"week"*, then
1. If IsCalendarUnit(_unit_) is *true*, then
1. If _plainRelativeTo_ is *undefined*, throw a *RangeError* exception.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-add~) is *true*.
1. Assert: CalendarMethodsRecordHasLookedUp(_calendarRec_, ~date-until~) is *true*.
1. If _unit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*, then
1. If IsCalendarUnit(_unit_) is *true* or _unit_ is *"day"*, then
1. If _zonedRelativeTo_ is not *undefined*, then
1. Let _intermediate_ be ? MoveRelativeZonedDateTime(_zonedRelativeTo_, _calendarRec_, _timeZoneRec_, _years_, _months_, _weeks_, _days_, _precalculatedPlainDateTime_).
1. Let _result_ be ? NormalizedTimeDurationToDays(_norm_, _intermediate_, _timeZoneRec_).
Expand Down Expand Up @@ -2179,7 +2179,7 @@ <h1>
</dd>
</dl>
<emu-alg>
1. If _unit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*; or _unit_ is *"nanosecond"* and _increment_ is 1, then
1. If IsCalendarUnit(_unit_) is *true*; or _unit_ is *"day"*; or _unit_ is *"nanosecond"* and _increment_ is 1, then
1. Return ! CreateNormalizedDurationRecord(_years_, _months_, _weeks_, _days_, _norm_).
1. Assert: _precalculatedPlainDateTime_ is not *undefined*.
1. Let _direction_ be NormalizedTimeDurationSign(_norm_).
Expand Down

0 comments on commit 0451f32

Please sign in to comment.