Skip to content

Commit f6e78d0

Browse files
committed
Update reference code to match editorial change
For the weeks and days part, we already used a shortcut instead of looping, so we keep it the same as before. For the years and months calculation, we change to the new definition of ISODateSurpasses.
1 parent eddb77f commit f6e78d0

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

polyfill/lib/calendar.mjs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,19 @@ function calendarDateWeekOfYear(id, isoDate) {
131131
return { week: woy, year: yow };
132132
}
133133

134-
function ISODateSurpasses(sign, y1, m1, d1, isoDate2) {
134+
function ISODateSurpasses(sign, baseDate, isoDate2, years, months, weeks, days) {
135+
const yearMonth = ES.BalanceISOYearMonth(baseDate.year + years, baseDate.month + months);
136+
let y1 = yearMonth.year;
137+
let m1 = yearMonth.month;
138+
let d1 = baseDate.day;
139+
if (weeks !== 0 || days !== 0) {
140+
const regulatedDate = ES.RegulateISODate(y1, m1, d1, 'constrain');
141+
({
142+
year: y1,
143+
month: m1,
144+
day: d1
145+
} = ES.BalanceISODate(regulatedDate.year, regulatedDate.month, regulatedDate.day + 7 * weeks + days));
146+
}
135147
if (y1 !== isoDate2.year) {
136148
if (sign * (y1 - isoDate2.year) > 0) return true;
137149
} else if (m1 !== isoDate2.month) {
@@ -192,25 +204,22 @@ impl['iso8601'] = {
192204

193205
let years = 0;
194206
let months = 0;
195-
let intermediate;
196207
if (largestUnit === 'year' || largestUnit === 'month') {
197208
// We can skip right to the neighbourhood of the correct number of years,
198209
// it'll be at least one less than two.year - one.year (unless it's zero)
199210
let candidateYears = two.year - one.year;
200211
if (candidateYears !== 0) candidateYears -= sign;
201212
// loops at most twice
202-
while (!ISODateSurpasses(sign, one.year + candidateYears, one.month, one.day, two)) {
213+
while (!ISODateSurpasses(sign, one, two, candidateYears, 0, 0, 0)) {
203214
years = candidateYears;
204215
candidateYears += sign;
205216
}
206217

207218
let candidateMonths = sign;
208-
intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + candidateMonths);
209219
// loops at most 12 times
210-
while (!ISODateSurpasses(sign, intermediate.year, intermediate.month, one.day, two)) {
220+
while (!ISODateSurpasses(sign, one, two, years, candidateMonths, 0, 0)) {
211221
months = candidateMonths;
212222
candidateMonths += sign;
213-
intermediate = ES.BalanceISOYearMonth(intermediate.year, intermediate.month + sign);
214223
}
215224

216225
if (largestUnit === 'month') {
@@ -219,7 +228,7 @@ impl['iso8601'] = {
219228
}
220229
}
221230

222-
intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + months);
231+
const intermediate = ES.BalanceISOYearMonth(one.year + years, one.month + months);
223232
const constrained = ES.ConstrainISODate(intermediate.year, intermediate.month, one.day);
224233

225234
let weeks = 0;

0 commit comments

Comments
 (0)