Skip to content

Commit 07adf1e

Browse files
committed
Polyfill: Make day-of-week algorithm less opaque
Add a comment on where the formula (probably) came from, and give the variables more self-documenting names. UPSTREAM_COMMIT=8424055541b191589aacf4c97a33444a87ccb6d0
1 parent ae03d3a commit 07adf1e

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

lib/calendar.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -328,19 +328,18 @@ impl['iso8601'] = {
328328
};
329329
if (requestedFields.monthCode) date.monthCode = buildMonthCode(month);
330330
if (requestedFields.dayOfWeek) {
331-
const m = month + (month < 3 ? 10 : -2);
332-
const Y = year - (month < 3 ? 1 : 0);
331+
// https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Disparate_variation
332+
const shiftedMonth = month + (month < 3 ? 10 : -2);
333+
const shiftedYear = year - (month < 3 ? 1 : 0);
333334

334-
const c = MathFloor(Y / 100);
335-
const y = Y - c * 100;
336-
const d = day;
335+
const century = MathFloor(shiftedYear / 100);
336+
const yearInCentury = shiftedYear - century * 100;
337337

338-
const pD = d;
339-
const pM = MathFloor(2.6 * m - 0.2);
340-
const pY = y + MathFloor(y / 4);
341-
const pC = MathFloor(c / 4) - 2 * c;
338+
const monthTerm = MathFloor(2.6 * shiftedMonth - 0.2);
339+
const yearTerm = yearInCentury + MathFloor(yearInCentury / 4);
340+
const centuryTerm = MathFloor(century / 4) - 2 * century;
342341

343-
const dow = (pD + pM + pY + pC) % 7;
342+
const dow = (day + monthTerm + yearTerm + centuryTerm) % 7;
344343

345344
date.dayOfWeek = dow + (dow <= 0 ? 7 : 0);
346345
}

0 commit comments

Comments
 (0)