Skip to content

Commit

Permalink
Editorial: Use Time Record in CompareTemporalTime, and rename
Browse files Browse the repository at this point in the history
Instead of passing each component individually, pass Time Records. The
name is no longer really appropriate, since we are not passing a Temporal
object, so rename it to CompareTimeRecord.

See: #2949
  • Loading branch information
ptomato authored and Ms2ger committed Oct 8, 2024
1 parent 218ff5a commit 2d10744
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 49 deletions.
19 changes: 11 additions & 8 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4536,20 +4536,23 @@ export function CompareISODate(isoDate1, isoDate2) {
return 0;
}

export function CompareTemporalTime(h1, min1, s1, ms1, µs1, ns1, h2, min2, s2, ms2, µs2, ns2) {
if (h1 !== h2) return ComparisonResult(h1 - h2);
if (min1 !== min2) return ComparisonResult(min1 - min2);
if (s1 !== s2) return ComparisonResult(s1 - s2);
if (ms1 !== ms2) return ComparisonResult(ms1 - ms2);
if (µs1 !== µs2) return ComparisonResult(µs1 - µs2);
if (ns1 !== ns2) return ComparisonResult(ns1 - ns2);
export function CompareTimeRecord(time1, time2) {
if (time1.hour !== time2.hour) return ComparisonResult(time1.hour - time2.hour);
if (time1.minute !== time2.minute) return ComparisonResult(time1.minute - time2.minute);
if (time1.second !== time2.second) return ComparisonResult(time1.second - time2.second);
if (time1.millisecond !== time2.millisecond) return ComparisonResult(time1.millisecond - time2.millisecond);
if (time1.microsecond !== time2.microsecond) return ComparisonResult(time1.microsecond - time2.microsecond);
if (time1.nanosecond !== time2.nanosecond) return ComparisonResult(time1.nanosecond - time2.nanosecond);
return 0;
}

export function CompareISODateTime(y1, m1, d1, h1, min1, s1, ms1, µs1, ns1, y2, m2, d2, h2, min2, s2, ms2, µs2, ns2) {
const dateResult = CompareISODate({ year: y1, month: m1, day: d1 }, { year: y2, month: m2, day: d2 });
if (dateResult !== 0) return dateResult;
return CompareTemporalTime(h1, min1, s1, ms1, µs1, ns1, h2, min2, s2, ms2, µs2, ns2);
return CompareTimeRecord(
{ hour: h1, minute: min1, second: s1, millisecond: ms1, microsecond: µs1, nanosecond: ns1 },
{ hour: h2, minute: min2, second: s2, millisecond: ms2, microsecond: µs2, nanosecond: ns2 }
);
}

// Not abstract operations from the spec
Expand Down
30 changes: 17 additions & 13 deletions polyfill/lib/plaintime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,23 @@ export class PlainTime {
static compare(one, two) {
one = ES.ToTemporalTime(one);
two = ES.ToTemporalTime(two);
return ES.CompareTemporalTime(
GetSlot(one, ISO_HOUR),
GetSlot(one, ISO_MINUTE),
GetSlot(one, ISO_SECOND),
GetSlot(one, ISO_MILLISECOND),
GetSlot(one, ISO_MICROSECOND),
GetSlot(one, ISO_NANOSECOND),
GetSlot(two, ISO_HOUR),
GetSlot(two, ISO_MINUTE),
GetSlot(two, ISO_SECOND),
GetSlot(two, ISO_MILLISECOND),
GetSlot(two, ISO_MICROSECOND),
GetSlot(two, ISO_NANOSECOND)
return ES.CompareTimeRecord(
{
hour: GetSlot(one, ISO_HOUR),
minute: GetSlot(one, ISO_MINUTE),
second: GetSlot(one, ISO_SECOND),
millisecond: GetSlot(one, ISO_MILLISECOND),
microsecond: GetSlot(one, ISO_MICROSECOND),
nanosecond: GetSlot(one, ISO_NANOSECOND)
},
{
hour: GetSlot(two, ISO_HOUR),
minute: GetSlot(two, ISO_MINUTE),
second: GetSlot(two, ISO_SECOND),
millisecond: GetSlot(two, ISO_MILLISECOND),
microsecond: GetSlot(two, ISO_MICROSECOND),
nanosecond: GetSlot(two, ISO_NANOSECOND)
}
);
}
}
Expand Down
4 changes: 3 additions & 1 deletion spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -1078,7 +1078,9 @@ <h1>
1. Let _isoDate2_ be CreateISODateRecord(_y2_, _mon2_, _d2_).
1. Let _dateResult_ be CompareISODate(_isoDate1_, _isoDate2_).
1. If _dateResult_ ≠ 0, return _dateResult_.
1. Return CompareTemporalTime(_h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_).
1. Let _time1_ be Time Record { [[Hour]]: _h1_, [[Minute]]: _min1_, [[Second]]: _s1_, [[Millisecond]]: _ms1_, [[Microsecond]]: _mus1_, [[Nanosecond]]: _ns1_ }.
1. Let _time2_ be Time Record { [[Hour]]: _h2_, [[Minute]]: _min2_, [[Second]]: _s2_, [[Millisecond]]: _ms2_, [[Microsecond]]: _mus2_, [[Nanosecond]]: _ns2_ }.
1. Return CompareTimeRecord(_time1_, _time2_).
</emu-alg>
</emu-clause>

Expand Down
46 changes: 19 additions & 27 deletions spec/plaintime.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ <h1>Temporal.PlainTime.compare ( _one_, _two_ )</h1>
<emu-alg>
1. Set _one_ to ? ToTemporalTime(_one_).
1. Set _two_ to ? ToTemporalTime(_two_).
1. Return 𝔽(CompareTemporalTime(_one_.[[ISOHour]], _one_.[[ISOMinute]], _one_.[[ISOSecond]], _one_.[[ISOMillisecond]], _one_.[[ISOMicrosecond]], _one_.[[ISONanosecond]], _two_.[[ISOHour]], _two_.[[ISOMinute]], _two_.[[ISOSecond]], _two_.[[ISOMillisecond]], _two_.[[ISOMicrosecond]], _two_.[[ISONanosecond]])).
1. Let _time1_ be Time Record { [[Hour]]: _one_.[[ISOHour]], [[Minute]]: _one_.[[ISOMinute]], [[Second]]: _one_.[[ISOSecond]], [[Millisecond]]: _one_.[[ISOMillisecond]], [[Microsecond]]: _one_.[[ISOMicrosecond]], [[Nanosecond]]: _one_.[[ISONanosecond]] }.
1. Let _time2_ be Time Record { [[Hour]]: _two_.[[ISOHour]], [[Minute]]: _two_.[[ISOMinute]], [[Second]]: _two_.[[ISOSecond]], [[Millisecond]]: _two_.[[ISOMillisecond]], [[Microsecond]]: _two_.[[ISOMicrosecond]], [[Nanosecond]]: _two_.[[ISONanosecond]] }.
1. Return 𝔽(CompareTimeRecord(_time1_, _time2_)).
</emu-alg>
</emu-clause>
</emu-clause>
Expand Down Expand Up @@ -849,40 +851,30 @@ <h1>
</emu-alg>
</emu-clause>

<emu-clause id="sec-temporal-comparetemporaltime" type="abstract operation">
<emu-clause id="sec-temporal-comparetimerecord" type="abstract operation">
<h1>
CompareTemporalTime (
_h1_: an integer in the inclusive interval from 0 to 23,
_min1_: an integer in the inclusive interval from 0 to 59,
_s1_: an integer in the inclusive interval from 0 to 59,
_ms1_: an integer in the inclusive interval from 0 to 999,
_mus1_: an integer in the inclusive interval from 0 to 999,
_ns1_: an integer in the inclusive interval from 0 to 999,
_h2_: an integer in the inclusive interval from 0 to 23,
_min2_: an integer in the inclusive interval from 0 to 59,
_s2_: an integer in the inclusive interval from 0 to 59,
_ms2_: an integer in the inclusive interval from 0 to 999,
_mus2_: an integer in the inclusive interval from 0 to 999,
_ns2_: an integer in the inclusive interval from 0 to 999,
CompareTimeRecord (
_time1_: a Time Record,
_time2_: a Time Record,
): -1, 0, or 1
</h1>
<dl class="header">
<dt>description</dt>
<dd>It compares the two given times and returns -1 if the second comes earlier in the day than the first, 1 if the first comes earlier in the day than the second, and 0 if they are the same.</dd>
</dl>
<emu-alg>
1. If _h1_ > _h2_, return 1.
1. If _h1_ &lt; _h2_, return -1.
1. If _min1_ > _min2_, return 1.
1. If _min1_ &lt; _min2_, return -1.
1. If _s1_ > _s2_, return 1.
1. If _s1_ &lt; _s2_, return -1.
1. If _ms1_ > _ms2_, return 1.
1. If _ms1_ &lt; _ms2_, return -1.
1. If _mus1_ > _mus2_, return 1.
1. If _mus1_ &lt; _mus2_, return -1.
1. If _ns1_ > _ns2_, return 1.
1. If _ns1_ &lt; _ns2_, return -1.
1. If _time1_.[[Hour]] > _time2_.[[Hour]], return 1.
1. If _time1_.[[Hour]] &lt; _time2_.[[Hour]], return -1.
1. If _time1_.[[Minute]] > _time2_.[[Minute]], return 1.
1. If _time1_.[[Minute]] &lt; _time2_.[[Minute]], return -1.
1. If _time1_.[[Second]] > _time2_.[[Second]], return 1.
1. If _time1_.[[Second]] &lt; _time2_.[[Second]], return -1.
1. If _time1_.[[Millisecond]] > _time2_.[[Millisecond]], return 1.
1. If _time1_.[[Millisecond]] &lt; _time2_.[[Millisecond]], return -1.
1. If _time1_.[[Microsecond]] > _time2_.[[Microsecond]], return 1.
1. If _time1_.[[Microsecond]] &lt; _time2_.[[Microsecond]], return -1.
1. If _time1_.[[Nanosecond]] > _time2_.[[Nanosecond]], return 1.
1. If _time1_.[[Nanosecond]] &lt; _time2_.[[Nanosecond]], return -1.
1. Return 0.
</emu-alg>
</emu-clause>
Expand Down

0 comments on commit 2d10744

Please sign in to comment.