Skip to content

Commit

Permalink
polyfill: compare methods should not include calendar
Browse files Browse the repository at this point in the history
This commit updates the polyfill to reflect the spec changes made for
addressing tc39#1431.
  • Loading branch information
ryzokuken committed Mar 22, 2021
1 parent 13470da commit 2b8c584
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 22 deletions.
2 changes: 1 addition & 1 deletion polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,7 @@ const makeHelperGregorian = (id, originalEras) => {
calendarIsVulnerableToJulianBug: false,
checkIcuBugs(calendarDate, isoDate) {
if (this.calendarIsVulnerableToJulianBug && this.v8IsVulnerableToJulianBug) {
const beforeJulianSwitch = ES.CompareISODate(isoDate.year, isoDate.month, isoDate.day, 1582, 10, 15) < 0;
const beforeJulianSwitch = ES.CompareTemporalDate(isoDate.year, isoDate.month, isoDate.day, 1582, 10, 15) < 0;
if (beforeJulianSwitch) {
throw new RangeError(
`calendar '${this.id}' is broken for ISO dates before 1582-10-15` +
Expand Down
12 changes: 6 additions & 6 deletions polyfill/lib/ecmascript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2790,15 +2790,15 @@ export const ES = ObjectAssign({}, ES2020, {
switch (largestUnit) {
case 'years':
case 'months': {
const sign = -ES.CompareISODate(y1, m1, d1, y2, m2, d2);
const sign = -ES.CompareTemporalDate(y1, m1, d1, y2, m2, d2);
if (sign === 0) return { years: 0, months: 0, weeks: 0, days: 0 };

const start = { year: y1, month: m1, day: d1 };
const end = { year: y2, month: m2, day: d2 };

let years = end.year - start.year;
let mid = ES.AddISODate(y1, m1, d1, years, 0, 0, 0, 'constrain');
let midSign = -ES.CompareISODate(mid.year, mid.month, mid.day, y2, m2, d2);
let midSign = -ES.CompareTemporalDate(mid.year, mid.month, mid.day, y2, m2, d2);
if (midSign === 0) {
return largestUnit === 'years'
? { years, months: 0, weeks: 0, days: 0 }
Expand All @@ -2810,7 +2810,7 @@ export const ES = ObjectAssign({}, ES2020, {
months += sign * 12;
}
mid = ES.AddISODate(y1, m1, d1, years, months, 0, 0, 'constrain');
midSign = -ES.CompareISODate(mid.year, mid.month, mid.day, y2, m2, d2);
midSign = -ES.CompareTemporalDate(mid.year, mid.month, mid.day, y2, m2, d2);
if (midSign === 0) {
return largestUnit === 'years'
? { years, months, weeks: 0, days: 0 }
Expand All @@ -2825,7 +2825,7 @@ export const ES = ObjectAssign({}, ES2020, {
months = 11 * sign;
}
mid = ES.AddISODate(y1, m1, d1, years, months, 0, 0, 'constrain');
midSign = -ES.CompareISODate(y1, m1, d1, mid.year, mid.month, mid.day);
midSign = -ES.CompareTemporalDate(y1, m1, d1, mid.year, mid.month, mid.day);
}

let days = 0;
Expand Down Expand Up @@ -2857,7 +2857,7 @@ export const ES = ObjectAssign({}, ES2020, {
case 'weeks':
case 'days': {
let larger, smaller, sign;
if (ES.CompareISODate(y1, m1, d1, y2, m2, d2) < 0) {
if (ES.CompareTemporalDate(y1, m1, d1, y2, m2, d2) < 0) {
smaller = { year: y1, month: m1, day: d1 };
larger = { year: y2, month: m2, day: d2 };
sign = 1;
Expand Down Expand Up @@ -3882,7 +3882,7 @@ export const ES = ObjectAssign({}, ES2020, {
return { years, months, weeks, days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds, total };
},

CompareISODate: (y1, m1, d1, y2, m2, d2) => {
CompareTemporalDate: (y1, m1, d1, y2, m2, d2) => {
for (const [x, y] of [
[y1, y2],
[m1, m2],
Expand Down
4 changes: 1 addition & 3 deletions polyfill/lib/plaindate.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -438,16 +438,14 @@ export class PlainDate {
static compare(one, two) {
one = ES.ToTemporalDate(one, PlainDate);
two = ES.ToTemporalDate(two, PlainDate);
const result = ES.CompareISODate(
return ES.CompareTemporalDate(
GetSlot(one, ISO_YEAR),
GetSlot(one, ISO_MONTH),
GetSlot(one, ISO_DAY),
GetSlot(two, ISO_YEAR),
GetSlot(two, ISO_MONTH),
GetSlot(two, ISO_DAY)
);
if (result !== 0) return result;
return ES.CalendarCompare(GetSlot(one, CALENDAR), GetSlot(two, CALENDAR));
}
}

Expand Down
2 changes: 1 addition & 1 deletion polyfill/lib/plaindatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ export class PlainDateTime {
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.CalendarCompare(GetSlot(one, CALENDAR), GetSlot(two, CALENDAR));
return 0;
}
}

Expand Down
14 changes: 8 additions & 6 deletions polyfill/lib/plainyearmonth.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,14 @@ export class PlainYearMonth {
static compare(one, two) {
one = ES.ToTemporalYearMonth(one, PlainYearMonth);
two = ES.ToTemporalYearMonth(two, PlainYearMonth);
for (const slot of [ISO_YEAR, ISO_MONTH, ISO_DAY]) {
const val1 = GetSlot(one, slot);
const val2 = GetSlot(two, slot);
if (val1 !== val2) return ES.ComparisonResult(val1 - val2);
}
return ES.CalendarCompare(GetSlot(one, CALENDAR), GetSlot(two, CALENDAR));
return ES.CompareTemporalDate(
GetSlot(one, ISO_YEAR),
GetSlot(one, ISO_MONTH),
GetSlot(one, ISO_DAY),
GetSlot(two, ISO_YEAR),
GetSlot(two, ISO_MONTH),
GetSlot(two, ISO_DAY)
);
}
}

Expand Down
2 changes: 0 additions & 2 deletions polyfill/lib/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -861,8 +861,6 @@ export class ZonedDateTime {
const ns2 = GetSlot(two, EPOCHNANOSECONDS);
if (bigInt(ns1).lesser(ns2)) return -1;
if (bigInt(ns1).greater(ns2)) return 1;
const calendarResult = ES.CalendarCompare(GetSlot(one, CALENDAR), GetSlot(two, CALENDAR));
if (calendarResult) return calendarResult;
return ES.TimeZoneCompare(GetSlot(one, TIME_ZONE), GetSlot(two, TIME_ZONE));
}
}
Expand Down
3 changes: 0 additions & 3 deletions polyfill/test/zoneddatetime.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2813,9 +2813,6 @@ describe('ZonedDateTime', () => {
it('compares time zone IDs if exact times are equal', () => {
equal(ZonedDateTime.compare(zdt1, zdt1.withTimeZone('Asia/Kolkata')), 1);
});
it('compares calendar IDs if exact times and time zones are equal', () => {
equal(ZonedDateTime.compare(zdt1, zdt1.withCalendar('japanese')), -1);
});
it('compares exact time, not clock time', () => {
const clockBefore = ZonedDateTime.from('1999-12-31T23:30-08:00[America/Vancouver]');
const clockAfter = ZonedDateTime.from('2000-01-01T01:30-04:00[America/Halifax]');
Expand Down

0 comments on commit 2b8c584

Please sign in to comment.