Skip to content
40 changes: 16 additions & 24 deletions polyfill/lib/calendar.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ function ISODateSurpasses(sign, baseDate, isoDate2, years, months, weeks, days)
let d1 = baseDate.day;
if (weeks !== 0 || days !== 0) {
const regulatedDate = ES.RegulateISODate(y1, m1, d1, 'constrain');
({
year: y1,
month: m1,
day: d1
} = ES.BalanceISODate(regulatedDate.year, regulatedDate.month, regulatedDate.day + 7 * weeks + days));
({ year: y1, month: m1, day: d1 } = ES.AddDaysToISODate(regulatedDate, 7 * weeks + days));
}
if (y1 !== isoDate2.year) {
if (sign * (y1 - isoDate2.year) > 0) return true;
Expand All @@ -155,10 +151,6 @@ function ISODateSurpasses(sign, baseDate, isoDate2, years, months, weeks, days)
return false;
}

function addDaysISO(isoDate, days) {
return ES.BalanceISODate(isoDate.year, isoDate.month, isoDate.day + days);
}

const impl = {};

impl['iso8601'] = {
Expand Down Expand Up @@ -199,9 +191,9 @@ impl['iso8601'] = {
year += years;
month += months;
({ year, month } = ES.BalanceISOYearMonth(year, month));
({ year, month, day } = ES.RegulateISODate(year, month, day, overflow));
day += days + 7 * weeks;
return ES.BalanceISODate(year, month, day);
const intermediate = ES.RegulateISODate(year, month, day, overflow);
days = days + 7 * weeks;
return ES.AddDaysToISODate(intermediate, days);
},
dateUntil(one, two, largestUnit) {
const sign = -ES.CompareISODate(one, two);
Expand Down Expand Up @@ -888,7 +880,7 @@ const nonIsoHelperBase = {
// that's currently worked around by a custom calendarToIsoDate
// implementation in those calendars. So this optimization should be safe
// for all ICU calendars.
let testIsoEstimate = addDaysISO(isoEstimate, diffDays);
let testIsoEstimate = ES.AddDaysToISODate(isoEstimate, diffDays);
if (date.day > this.minimumMonthLength(date)) {
// There's a chance that the calendar date is out of range. Throw or
// constrain if so.
Expand All @@ -898,7 +890,7 @@ const nonIsoHelperBase = {
throw new RangeErrorCtor(`day ${day} does not exist in month ${month} of year ${year}`);
}
// Back up a day at a time until we're not hanging over the month end
testIsoEstimate = addDaysISO(testIsoEstimate, -1);
testIsoEstimate = ES.AddDaysToISODate(testIsoEstimate, -1);
testCalendarDate = this.isoToCalendarDate(testIsoEstimate, cache);
}
}
Expand All @@ -909,7 +901,7 @@ const nonIsoHelperBase = {
let diff = simpleDateDiff(date, roundtripEstimate);
if (diff.years !== 0 || diff.months !== 0 || diff.days !== 0) {
const diffTotalDaysEstimate = diff.years * 365 + diff.months * 30 + diff.days;
isoEstimate = addDaysISO(isoEstimate, diffTotalDaysEstimate);
isoEstimate = ES.AddDaysToISODate(isoEstimate, diffTotalDaysEstimate);
roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
diff = simpleDateDiff(date, roundtripEstimate);
if (diff.years === 0 && diff.months === 0) {
Expand All @@ -922,7 +914,7 @@ const nonIsoHelperBase = {
// distance to the target, starting with 8 days per step.
let increment = 8;
while (sign) {
isoEstimate = addDaysISO(isoEstimate, sign * increment);
isoEstimate = ES.AddDaysToISODate(isoEstimate, sign * increment);
const oldRoundtripEstimate = roundtripEstimate;
roundtripEstimate = this.isoToCalendarDate(isoEstimate, cache);
const oldSign = sign;
Expand All @@ -949,7 +941,7 @@ const nonIsoHelperBase = {
// To constrain, pick the earliest value
const order = this.compareCalendarDates(roundtripEstimate, oldRoundtripEstimate);
// If current value is larger, then back up to the previous value.
if (order > 0) isoEstimate = addDaysISO(isoEstimate, -1);
if (order > 0) isoEstimate = ES.AddDaysToISODate(isoEstimate, -1);
sign = 0;
}
}
Expand Down Expand Up @@ -982,7 +974,7 @@ const nonIsoHelperBase = {
},
addDaysCalendar(calendarDate, days, cache) {
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
const addedIso = addDaysISO(isoDate, days);
const addedIso = ES.AddDaysToISODate(isoDate, days);
const addedCalendar = this.isoToCalendarDate(addedIso, cache);
return addedCalendar;
},
Expand All @@ -996,7 +988,7 @@ const nonIsoHelperBase = {
? -MathMax(day, this.daysInPreviousMonth(calendarDate, cache))
: this.daysInMonth(calendarDate, cache);
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
let addedIso = addDaysISO(isoDate, days);
let addedIso = ES.AddDaysToISODate(isoDate, days);
calendarDate = this.isoToCalendarDate(addedIso, cache);

// Normally, we can advance one month by adding the number of days in the
Expand All @@ -1007,7 +999,7 @@ const nonIsoHelperBase = {
if (months > 0) {
const monthsInOldYear = this.monthsInYear(oldCalendarDate, cache);
while (calendarDate.month - 1 !== month % monthsInOldYear) {
addedIso = addDaysISO(addedIso, -1);
addedIso = ES.AddDaysToISODate(addedIso, -1);
calendarDate = this.isoToCalendarDate(addedIso, cache);
}
}
Expand Down Expand Up @@ -1158,11 +1150,11 @@ const nonIsoHelperBase = {
// Add enough days to get into the next month, without skipping it
const increment = day <= max - min ? max : min;
const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
const addedIsoDate = addDaysISO(isoDate, increment);
const addedIsoDate = ES.AddDaysToISODate(isoDate, increment);
const addedCalendarDate = this.isoToCalendarDate(addedIsoDate, cache);

// Now back up to the last day of the original month
const endOfMonthIso = addDaysISO(addedIsoDate, -addedCalendarDate.day);
const endOfMonthIso = ES.AddDaysToISODate(addedIsoDate, -addedCalendarDate.day);
const endOfMonthCalendar = this.isoToCalendarDate(endOfMonthIso, cache);
return endOfMonthCalendar.day;
},
Expand All @@ -1179,7 +1171,7 @@ const nonIsoHelperBase = {
if (min === max) return max;

const isoDate = this.calendarToIsoDate(calendarDate, 'constrain', cache);
const lastDayOfPreviousMonthIso = addDaysISO(isoDate, -day);
const lastDayOfPreviousMonthIso = ES.AddDaysToISODate(isoDate, -day);
const lastDayOfPreviousMonthCalendar = this.isoToCalendarDate(lastDayOfPreviousMonthIso, cache);
return lastDayOfPreviousMonthCalendar.day;
},
Expand Down Expand Up @@ -1521,7 +1513,7 @@ const helperIndian = makeNonISOHelper([{ code: 'shaka', isoEpoch: { year: 79, mo
const isoYear = calendarDate.year + 78 + (monthInfo.nextYear ? 1 : 0);
const isoMonth = monthInfo.month;
const isoDay = monthInfo.day;
const isoDate = ES.BalanceISODate(isoYear, isoMonth, isoDay + calendarDate.day - 1);
const isoDate = ES.AddDaysToISODate({ year: isoYear, month: isoMonth, day: isoDay }, calendarDate.day - 1);
return isoDate;
},
// https://bugs.chromium.org/p/v8/issues/detail?id=10529 causes Intl's Indian
Expand Down
Loading
Loading