Skip to content

Commit

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

See: #2949
  • Loading branch information
ptomato authored and Ms2ger committed Oct 8, 2024
1 parent a11d31c commit aa3a36e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 104 deletions.
41 changes: 23 additions & 18 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3745,7 +3745,15 @@ export function DifferencePlainDateTimeWithRounding(
smallestUnit,
roundingMode
) {
if (CompareISODateTime(y1, mon1, d1, h1, min1, s1, ms1, µs1, ns1, y2, mon2, d2, h2, min2, s2, ms2, µs2, ns2) == 0) {
const isoDateTime1 = {
isoDate: { year: y1, month: mon1, day: d1 },
time: { hour: h1, minute: min1, second: s1, millisecond: ms1, microsecond: µs1, nanosecond: ns1 }
};
const isoDateTime2 = {
isoDate: { year: y2, month: mon2, day: d2 },
time: { hour: h2, minute: min2, second: s2, millisecond: ms2, microsecond: µs2, nanosecond: ns2 }
};
if (CompareISODateTime(isoDateTime1, isoDateTime2) == 0) {
return { date: ZeroDateDuration(), norm: TimeDuration.ZERO };
}

Expand Down Expand Up @@ -3774,15 +3782,11 @@ export function DifferencePlainDateTimeWithRounding(

if (smallestUnit === 'nanosecond' && roundingIncrement === 1) return duration;

const isoDateTime = {
isoDate: { year: y1, month: mon1, day: d1 },
time: { hour: h1, minute: min1, second: s1, millisecond: ms1, microsecond: µs1, nanosecond: ns1 }
};
const destEpochNs = GetUTCEpochNanoseconds(y2, mon2, d2, h2, min2, s2, ms2, µs2, ns2);
return RoundRelativeDuration(
duration,
destEpochNs,
isoDateTime,
isoDateTime1,
null,
calendar,
largestUnit,
Expand Down Expand Up @@ -3814,7 +3818,15 @@ export function DifferencePlainDateTimeWithTotal(
calendar,
unit
) {
if (CompareISODateTime(y1, mon1, d1, h1, min1, s1, ms1, µs1, ns1, y2, mon2, d2, h2, min2, s2, ms2, µs2, ns2) == 0) {
const isoDateTime1 = {
isoDate: { year: y1, month: mon1, day: d1 },
time: { hour: h1, minute: min1, second: s1, millisecond: ms1, microsecond: µs1, nanosecond: ns1 }
};
const isoDateTime2 = {
isoDate: { year: y2, month: mon2, day: d2 },
time: { hour: h2, minute: min2, second: s2, millisecond: ms2, microsecond: µs2, nanosecond: ns2 }
};
if (CompareISODateTime(isoDateTime1, isoDateTime2) == 0) {
return 0;
}

Expand Down Expand Up @@ -3843,12 +3855,8 @@ export function DifferencePlainDateTimeWithTotal(

if (unit === 'nanosecond') return duration.norm.totalNs.toJSNumber();

const isoDateTime = {
isoDate: { year: y1, month: mon1, day: d1 },
time: { hour: h1, minute: min1, second: s1, millisecond: ms1, microsecond: µs1, nanosecond: ns1 }
};
const destEpochNs = GetUTCEpochNanoseconds(y2, mon2, d2, h2, min2, s2, ms2, µs2, ns2);
return TotalRelativeDuration(duration, destEpochNs, isoDateTime, null, calendar, unit);
return TotalRelativeDuration(duration, destEpochNs, isoDateTime1, null, calendar, unit);
}

export function DifferenceZonedDateTimeWithRounding(
Expand Down Expand Up @@ -4536,13 +4544,10 @@ export function CompareTimeRecord(time1, time2) {
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 });
export function CompareISODateTime(isoDateTime1, isoDateTime2) {
const dateResult = CompareISODate(isoDateTime1.isoDate, isoDateTime2.isoDate);
if (dateResult !== 0) return dateResult;
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 }
);
return CompareTimeRecord(isoDateTime1.time, isoDateTime2.time);
}

// Not abstract operations from the spec
Expand Down
50 changes: 6 additions & 44 deletions polyfill/lib/plaindatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -317,30 +317,9 @@ export class PlainDateTime {
equals(other) {
if (!ES.IsTemporalDateTime(this)) throw new TypeErrorCtor('invalid receiver');
other = ES.ToTemporalDateTime(other);
if (
ES.CompareISODateTime(
GetSlot(this, ISO_YEAR),
GetSlot(this, ISO_MONTH),
GetSlot(this, ISO_DAY),
GetSlot(this, ISO_HOUR),
GetSlot(this, ISO_MINUTE),
GetSlot(this, ISO_SECOND),
GetSlot(this, ISO_MILLISECOND),
GetSlot(this, ISO_MICROSECOND),
GetSlot(this, ISO_NANOSECOND),
GetSlot(other, ISO_YEAR),
GetSlot(other, ISO_MONTH),
GetSlot(other, ISO_DAY),
GetSlot(other, ISO_HOUR),
GetSlot(other, ISO_MINUTE),
GetSlot(other, ISO_SECOND),
GetSlot(other, ISO_MILLISECOND),
GetSlot(other, ISO_MICROSECOND),
GetSlot(other, ISO_NANOSECOND)
) !== 0
) {
return false;
}
const isoDateTime = ES.PlainDateTimeToISODateTimeRecord(this);
const isoDateTimeOther = ES.PlainDateTimeToISODateTimeRecord(other);
if (ES.CompareISODateTime(isoDateTime, isoDateTimeOther) !== 0) return false;
return ES.CalendarEquals(GetSlot(this, CALENDAR), GetSlot(other, CALENDAR));
}
toString(options = undefined) {
Expand Down Expand Up @@ -393,26 +372,9 @@ export class PlainDateTime {
static compare(one, two) {
one = ES.ToTemporalDateTime(one);
two = ES.ToTemporalDateTime(two);
return ES.CompareISODateTime(
GetSlot(one, ISO_YEAR),
GetSlot(one, ISO_MONTH),
GetSlot(one, ISO_DAY),
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_YEAR),
GetSlot(two, ISO_MONTH),
GetSlot(two, ISO_DAY),
GetSlot(two, ISO_HOUR),
GetSlot(two, ISO_MINUTE),
GetSlot(two, ISO_SECOND),
GetSlot(two, ISO_MILLISECOND),
GetSlot(two, ISO_MICROSECOND),
GetSlot(two, ISO_NANOSECOND)
);
const isoDateTime1 = ES.PlainDateTimeToISODateTimeRecord(one);
const isoDateTime2 = ES.PlainDateTimeToISODateTimeRecord(two);
return ES.CompareISODateTime(isoDateTime1, isoDateTime2);
}
}

Expand Down
65 changes: 25 additions & 40 deletions spec/plaindatetime.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ <h1>Temporal.PlainDateTime.compare ( _one_, _two_ )</h1>
<emu-alg>
1. Set _one_ to ? ToTemporalDateTime(_one_).
1. Set _two_ to ? ToTemporalDateTime(_two_).
1. Return 𝔽(CompareISODateTime(_one_.[[ISOYear]], _one_.[[ISOMonth]], _one_.[[ISODay]], _one_.[[ISOHour]], _one_.[[ISOMinute]], _one_.[[ISOSecond]], _one_.[[ISOMillisecond]], _one_.[[ISOMicrosecond]], _one_.[[ISONanosecond]], _two_.[[ISOYear]], _two_.[[ISOMonth]], _two_.[[ISODay]], _two_.[[ISOHour]], _two_.[[ISOMinute]], _two_.[[ISOSecond]], _two_.[[ISOMillisecond]], _two_.[[ISOMicrosecond]], _two_.[[ISONanosecond]])).
1. Let _isoDateTime1_ be PlainDateTimeToISODateTimeRecord(_one_).
1. Let _isoDateTime2_ be PlainDateTimeToISODateTimeRecord(_two_).
1. Return 𝔽(CompareISODateTime(_isoDateTime1_, _isoDateTime2_)).
</emu-alg>
</emu-clause>
</emu-clause>
Expand Down Expand Up @@ -536,8 +538,9 @@ <h1>Temporal.PlainDateTime.prototype.equals ( _other_ )</h1>
1. Let _dateTime_ be the *this* value.
1. Perform ? RequireInternalSlot(_dateTime_, [[InitializedTemporalDateTime]]).
1. Set _other_ to ? ToTemporalDateTime(_other_).
1. Let _result_ be CompareISODateTime(_dateTime_.[[ISOYear]], _dateTime_.[[ISOMonth]], _dateTime_.[[ISODay]], _dateTime_.[[ISOHour]], _dateTime_.[[ISOMinute]], _dateTime_.[[ISOSecond]], _dateTime_.[[ISOMillisecond]], _dateTime_.[[ISOMicrosecond]], _dateTime_.[[ISONanosecond]], _other_.[[ISOYear]], _other_.[[ISOMonth]], _other_.[[ISODay]], _other_.[[ISOHour]], _other_.[[ISOMinute]], _other_.[[ISOSecond]], _other_.[[ISOMillisecond]], _other_.[[ISOMicrosecond]], _other_.[[ISONanosecond]]).
1. If _result_ is not 0, return *false*.
1. Let _isoDateTime_ be PlainDateTimeToISODateTimeRecord(_dateTime_).
1. Let _isoDateTimeOther_ be PlainDateTimeToISODateTimeRecord(_other_).
1. If CompareISODateTime(_isoDateTime_, _isoDateTimeOther_) ≠ 0, return *false*.
1. Return CalendarEquals(_dateTime_.[[Calendar]], _other_.[[Calendar]]).
</emu-alg>
</emu-clause>
Expand Down Expand Up @@ -1006,38 +1009,18 @@ <h1>
<emu-clause id="sec-temporal-compareisodatetime" type="abstract operation">
<h1>
CompareISODateTime (
_y1_: an integer,
_mon1_: an integer,
_d1_: an integer,
_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,
_y2_: an integer,
_mon2_: an integer,
_d2_: an integer,
_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,
_isoDateTime1_: an ISO Date-Time Record,
_isoDateTime2_: an ISO Date-Time Record,
): -1, 0, or 1
</h1>
<dl class="header">
<dt>description</dt>
<dd>It performs a comparison of two date-times according to ISO 8601 calendar arithmetic.</dd>
</dl>
<emu-alg>
1. Let _isoDate1_ be CreateISODateRecord(_y1_, _mon1_, _d1_).
1. Let _isoDate2_ be CreateISODateRecord(_y2_, _mon2_, _d2_).
1. Let _dateResult_ be CompareISODate(_isoDate1_, _isoDate2_).
1. Let _dateResult_ be CompareISODate(_isoDateTime1_.[[ISODate]], _isoDateTime2_.[[ISODate]]).
1. If _dateResult_ ≠ 0, return _dateResult_.
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_).
1. Return CompareTimeRecord(_isoDateTime1_.[[Time]], _isoDateTime2_.[[Time]]).
</emu-alg>
</emu-clause>

Expand Down Expand Up @@ -1152,15 +1135,16 @@ <h1>
<dd></dd>
</dl>
<emu-alg>
1. Assert: IsValidISODate(_y1_, _mon1_, _d1_) is *true*.
1. Assert: IsValidISODate(_y2_, _mon2_, _d2_) is *true*.
1. If CompareISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_) = 0, then
1. Return ! CombineDateAndNormalizedTimeDuration(ZeroDateDuration(), ZeroTimeDuration()).
1. Let _diff_ be ? DifferenceISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_, _calendar_, _largestUnit_).
1. If _smallestUnit_ is ~nanosecond~ and _roundingIncrement_ = 1, return _diff_.
1. Let _isoDate1_ be CreateISODateRecord(_y1_, _mon1_, _d1_).
1. Let _time1_ be Time Record { [[Hour]]: _h1_, [[Minute]]: _min1_, [[Second]]: _s1_, [[Millisecond]]: _ms1_, [[Microsecond]]: _mus1_, [[Nanosecond]]: _ns1_ }.
1. Let _isoDateTime1_ be CombineISODateAndTimeRecord(_isoDate1_, _time1_).
1. Let _isoDate2_ be CreateISODateRecord(_y2_, _mon2_, _d2_).
1. Let _time2_ be Time Record { [[Hour]]: _h2_, [[Minute]]: _min2_, [[Second]]: _s2_, [[Millisecond]]: _ms2_, [[Microsecond]]: _mus2_, [[Nanosecond]]: _ns2_ }.
1. Let _isoDateTime2_ be CombineISODateAndTimeRecord(_isoDate2_, _time2_).
1. If CompareISODateTime(_isoDateTime1_, _isoDateTime2_) = 0, then
1. Return ! CombineDateAndNormalizedTimeDuration(ZeroDateDuration(), ZeroTimeDuration()).
1. Let _diff_ be ? DifferenceISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_, _calendar_, _largestUnit_).
1. If _smallestUnit_ is ~nanosecond~ and _roundingIncrement_ = 1, return _diff_.
1. Let _destEpochNs_ be GetUTCEpochNanoseconds(_y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_).
1. Return ? RoundRelativeDuration(_diff_, _destEpochNs_, _isoDateTime1_, ~unset~, _calendar_, _largestUnit_, _roundingIncrement_, _smallestUnit_, _roundingMode_).
</emu-alg>
Expand Down Expand Up @@ -1196,17 +1180,18 @@ <h1>
<dd></dd>
</dl>
<emu-alg>
1. Assert: IsValidISODate(_y1_, _mon1_, _d1_) is *true*.
1. Assert: IsValidISODate(_y2_, _mon2_, _d2_) is *true*.
1. If CompareISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_) = 0, then
1. Let _isoDate1_ be CreateISODateRecord(_y1_, _mon1_, _d1_).
1. Let _time1_ be Time Record { [[Hour]]: _h1_, [[Minute]]: _min1_, [[Second]]: _s1_, [[Millisecond]]: _ms1_, [[Microsecond]]: _mus1_, [[Nanosecond]]: _ns1_ }.
1. Let _isoDateTime1_ be CombineISODateAndTimeRecord(_isoDate1_, _time1_).
1. Let _isoDate2_ be CreateISODateRecord(_y2_, _mon2_, _d2_).
1. Let _time2_ be Time Record { [[Hour]]: _h2_, [[Minute]]: _min2_, [[Second]]: _s2_, [[Millisecond]]: _ms2_, [[Microsecond]]: _mus2_, [[Nanosecond]]: _ns2_ }.
1. Let _isoDateTime2_ be CombineISODateAndTimeRecord(_isoDate2_, _time2_).
1. If CompareISODateTime(_isoDateTime1_, _isoDateTime2_) = 0, then
1. Return 0.
1. Let _diff_ be ? DifferenceISODateTime(_y1_, _mon1_, _d1_, _h1_, _min1_, _s1_, _ms1_, _mus1_, _ns1_, _y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_, _calendar_, _unit_).
1. If _unit_ is ~nanosecond~, return _diff_.[[NormalizedTime]].[[TotalNanoseconds]].
1. Let _isoDate_ be CreateISODateRecord(_y1_, _mon1_, _d1_).
1. Let _time_ be Time Record { [[Hour]]: _h1_, [[Minute]]: _min1_, [[Second]]: _s1_, [[Millisecond]]: _ms1_, [[Microsecond]]: _mus1_, [[Nanosecond]]: _ns1_ }.
1. Let _isoDateTime_ be CombineISODateAndTimeRecord(_isoDate_, _time_).
1. Let _destEpochNs_ be GetUTCEpochNanoseconds(_y2_, _mon2_, _d2_, _h2_, _min2_, _s2_, _ms2_, _mus2_, _ns2_).
1. Return ? TotalRelativeDuration(_diff_, _destEpochNs_, _isoDateTime_, ~unset~, _calendar_, _unit_).
1. Return ? TotalRelativeDuration(_diff_, _destEpochNs_, _isoDateTime1_, ~unset~, _calendar_, _unit_).
</emu-alg>
</emu-clause>

Expand Down
4 changes: 2 additions & 2 deletions spec/timezone.html
Original file line number Diff line number Diff line change
Expand Up @@ -314,8 +314,8 @@ <h1>
1. Assert: _n_ = 0.
1. If _disambiguation_ is ~reject~, then
1. Throw a *RangeError* exception.
1. Let _before_ be the latest possible ISO Date-Time Record for which CompareISODateTime(_before_.[[Year]], _before_.[[Month]], _before_.[[Day]], _before_.[[Hour]], _before_.[[Minute]], _before_.[[Second]], _before_.[[Millisecond]], _before_.[[Microsecond]], _before_.[[Nanosecond]], _isoDateTime_.[[Year]], _isoDateTime_.[[Month]], _isoDateTime_.[[Day]], _isoDateTime_.[[Hour]], _isoDateTime_.[[Minute]], _isoDateTime_.[[Second]], _isoDateTime_.[[Millisecond]], _isoDateTime_.[[Microsecond]], _isoDateTime_.[[Nanosecond]]) = -1 and ! GetPossibleEpochNanoseconds(_timeZone_, _before_) is not empty.
1. Let _after_ be the earliest possible ISO Date-Time Record for which CompareISODateTime(_after_.[[Year]], _after_.[[Month]], _after_.[[Day]], _after_.[[Hour]], _after_.[[Minute]], _after_.[[Second]], _after_.[[Millisecond]], _after_.[[Microsecond]], _after_.[[Nanosecond]], _isoDateTime_.[[Year]], _isoDateTime_.[[Month]], _isoDateTime_.[[Day]], _isoDateTime_.[[Hour]], _isoDateTime_.[[Minute]], _isoDateTime_.[[Second]], _isoDateTime_.[[Millisecond]], _isoDateTime_.[[Microsecond]], _isoDateTime_.[[Nanosecond]]) = 1 and ! GetPossibleEpochNanoseconds(_timeZone_, _after_) is not empty.
1. Let _before_ be the latest possible ISO Date-Time Record for which CompareISODateTime(_before_, _isoDateTime_) = -1 and ! GetPossibleEpochNanoseconds(_timeZone_, _before_) is not empty.
1. Let _after_ be the earliest possible ISO Date-Time Record for which CompareISODateTime(_after_, _isoDateTime_) = 1 and ! GetPossibleEpochNanoseconds(_timeZone_, _after_) is not empty.
1. Let _offsetBefore_ be GetOffsetNanosecondsFor(_timeZone_, _before_).
1. Let _offsetAfter_ be GetOffsetNanosecondsFor(_timeZone_, _after_).
1. Let _nanoseconds_ be _offsetAfter_ - _offsetBefore_.
Expand Down

0 comments on commit aa3a36e

Please sign in to comment.