Skip to content

Commit 76452d2

Browse files
ptomatoMs2ger
authored andcommitted
Editorial: Simplify DifferenceISODate
Similar to DaysUntil, here we use the already-existing MakeDay operation in ECMA-262 to convert ISO year/month/day to days since epoch, which can be subtracted directly. The algorithm is essentially doing what it already did: calculating a number of days, and converting to weeks if necessary. However, it now does so without having to swap the order of the dates and negate the result if the second is earlier than the first. (Unfortunately, the first half of DifferenceISODate, to compute the difference in months, is still hard-to-read. I'll keep looking for a way to simplify this.)
1 parent b2254b4 commit 76452d2

File tree

3 files changed

+13
-22
lines changed

3 files changed

+13
-22
lines changed

spec/calendar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -890,7 +890,7 @@ <h1>Temporal.Calendar.prototype.dateUntil ( _one_, _two_ [ , _options_ ] )</h1>
890890
1. Set _two_ to ? ToTemporalDate(_two_).
891891
1. Set _options_ to ? GetOptionsObject(_options_).
892892
1. Let _largestUnit_ be ? ToLargestTemporalUnit(_options_, « *"hour"*, *"minute"*, *"second"*, *"millisecond"*, *"microsecond"*, *"nanosecond"* », *"auto"*, *"day"*).
893-
1. Let _result_ be ! DifferenceISODate(_one_.[[ISOYear]], _one_.[[ISOMonth]], _one_.[[ISODay]], _two_.[[ISOYear]], _two_.[[ISOMonth]], _two_.[[ISODay]], _largestUnit_).
893+
1. Let _result_ be DifferenceISODate(_one_.[[ISOYear]], _one_.[[ISOMonth]], _one_.[[ISODay]], _two_.[[ISOYear]], _two_.[[ISOMonth]], _two_.[[ISODay]], _largestUnit_).
894894
1. Return ! CreateTemporalDuration(_result_.[[Years]], _result_.[[Months]], _result_.[[Weeks]], _result_.[[Days]], 0, 0, 0, 0, 0, 0).
895895
</emu-alg>
896896
</emu-clause>

spec/intl.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1713,7 +1713,7 @@ <h1>Temporal.Calendar.prototype.dateUntil ( _one_, _two_ [ , _options_ ] )</h1>
17131713
1. Set _options_ to ? GetOptionsObject(_options_).
17141714
1. Let _largestUnit_ be ? ToLargestTemporalUnit(_options_, « *"hour"*, *"minute"*, *"second"*, *"millisecond"*, *"microsecond"*, *"nanosecond"* », *"auto"*, *"day"*).
17151715
1. If _calendar_.[[Identifier]] is *"iso8601"*, then
1716-
1. Let _result_ be ! DifferenceISODate(_one_.[[ISOYear]], _one_.[[ISOMonth]], _one_.[[ISODay]], _two_.[[ISOYear]], _two_.[[ISOMonth]], _two_.[[ISODay]], _largestUnit_).
1716+
1. Let _result_ be DifferenceISODate(_one_.[[ISOYear]], _one_.[[ISOMonth]], _one_.[[ISODay]], _two_.[[ISOYear]], _two_.[[ISOMonth]], _two_.[[ISODay]], _largestUnit_).
17171717
1. Else,
17181718
1. Let _result_ be ! CalendarDateDifference(_calendar_.[[Identifier]], _one_, _two_, _largestUnit_).
17191719
1. Assert: ! IsValidDuration(_result_.[[Years]], _result_.[[Months]], _result_.[[Weeks]], _result_.[[Days]], 0, 0, 0, 0, 0, 0) is *true*.

spec/plaindate.html

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,17 @@ <h1>
707707
_y2_: an integer,
708708
_m2_: an integer,
709709
_d2_: an integer,
710-
_largestUnit_: a String,
711-
)
710+
_largestUnit_: *"year"*, *"month"*, *"week"*, or *"day"*,
711+
): a Date Duration Record
712712
</h1>
713713
<dl class="header">
714714
<dt>description</dt>
715715
<dd>
716-
It returns a Date Duration Record with the elapsed duration from a first date until a second date, according to the reckoning of the ISO 8601 calendar.
716+
The return value is the elapsed duration from a first date until a second date, according to the reckoning of the ISO 8601 calendar.
717717
No fields larger than _largestUnit_ will be non-zero in the resulting Date Duration Record.
718718
</dd>
719719
</dl>
720720
<emu-alg>
721-
1. Assert: _largestUnit_ is one of *"year"*, *"month"*, *"week"*, or *"day"*.
722721
1. If _largestUnit_ is *"year"* or *"month"*, then
723722
1. Let _sign_ be -(! CompareISODate(_y1_, _m1_, _d1_, _y2_, _m2_, _d2_)).
724723
1. If _sign_ is 0, return ! CreateDateDurationRecord(0, 0, 0, 0).
@@ -756,24 +755,16 @@ <h1>
756755
1. Set _years_ to 0.
757756
1. Return ! CreateDateDurationRecord(_years_, _months_, 0, _days_).
758757
1. If _largestUnit_ is *"day"* or *"week"*, then
759-
1. If ! CompareISODate(_y1_, _m1_, _d1_, _y2_, _m2_, _d2_) &lt; 0, then
760-
1. Let _smaller_ be the Record { [[Year]]: _y1_, [[Month]]: _m1_, [[Day]]: _d1_ }.
761-
1. Let _greater_ be the Record { [[Year]]: _y2_, [[Month]]: _m2_, [[Day]]: _d2_ }.
762-
1. Let _sign_ be 1.
763-
1. Else,
764-
1. Let _smaller_ be the Record { [[Year]]: _y2_, [[Month]]: _m2_, [[Day]]: _d2_ }.
765-
1. Let _greater_ be the Record { [[Year]]: _y1_, [[Month]]: _m1_, [[Day]]: _d1_ }.
766-
1. Let _sign_ be -1.
767-
1. Let _days_ be ! ToISODayOfYear(_greater_.[[Year]], _greater_.[[Month]], _greater_.[[Day]]) - ! ToISODayOfYear(_smaller_.[[Year]], _smaller_.[[Month]], _smaller_.[[Day]]).
768-
1. Let _year_ be _smaller_.[[Year]].
769-
1. Repeat, while _year_ &lt; _greater_.[[Year]],
770-
1. Set _days_ to _days_ + ! ISODaysInYear(_year_).
771-
1. Set _year_ to _year_ + 1.
758+
1. Let _epochDays1_ be MakeDay(𝔽(_y1_), 𝔽(_m1_ - 1), 𝔽(_d1_)).
759+
1. Assert: _epochDays1_ is finite.
760+
1. Let _epochDays2_ be MakeDay(𝔽(_y2_), 𝔽(_m2_ - 1), 𝔽(_d2_)).
761+
1. Assert: _epochDays2_ is finite.
762+
1. Let _days_ be ℝ(_epochDays2_) - ℝ(_epochDays1_).
772763
1. Let _weeks_ be 0.
773764
1. If _largestUnit_ is *"week"*, then
774-
1. Set _weeks_ to floor(_days_ / 7).
775-
1. Set _days_ to _days_ modulo 7.
776-
1. Return ! CreateDateDurationRecord(0, 0, _weeks_ &times; _sign_, _days_ &times; _sign_).
765+
1. Set _weeks_ to RoundTowardsZero(_days_ / 7).
766+
1. Set _days_ to remainder(_days_, 7).
767+
1. Return ! CreateDateDurationRecord(0, 0, _weeks_, _days_).
777768
</emu-alg>
778769
</emu-clause>
779770

0 commit comments

Comments
 (0)