@northguild/gmt
Version:
Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.
178 lines (176 loc) • 8.59 kB
JavaScript
import { Temporal } from "../temporal.js";
// src/internal/temporalCompat/nonIsoArithmetic.ts
const ISO_CALENDAR = "iso8601";
/** The estimates below are off by at most one unit; a few extra steps cost nothing. */
const MAX_ADJUSTMENTS = 4;
/** A leap month recurs within one Metonic cycle, so a year lacking it is found within 19 years. */
const LEAP_MONTH_SEARCH_YEARS = 19;
const MONTH_CODE = /^M(\d{2})(L?)$/;
const constrainedMonthCodes = /* @__PURE__ */ new Map();
/** `ParseMonthCode` ordering: `M05 < M05L < M06` (grammar, not calendar data). */
function compareMonthCodes(a, b) {
const matchA = MONTH_CODE.exec(a);
const matchB = MONTH_CODE.exec(b);
if (!matchA || !matchB) throw new RangeError(`Invalid month code: ${matchA ? b : a}`);
return Number(matchA[1]) - Number(matchB[1]) || Number(matchA[2] === "L") - Number(matchB[2] === "L");
}
/**
* The month code the runtime constrains a leap month code to in a year without it (Hebrew
* `M05L` → `M06`), read once per calendar from the polyfill at a modern year, where its constrain
* behaviour is correct. GMT never states the mapping itself.
*/
function constrainedMonthCode(calendarId, monthCode) {
const key = `${calendarId}/${monthCode}`;
const cached = constrainedMonthCodes.get(key);
if (cached !== void 0) return cached;
const modernYear = Temporal.PlainDate.from("2000-01-01").withCalendar(calendarId).year;
for (let offset = 0; offset < LEAP_MONTH_SEARCH_YEARS; offset++) {
const fields = {
calendar: calendarId,
year: modernYear + offset,
monthCode,
day: 1
};
try {
Temporal.PlainDate.from(fields, { overflow: "reject" });
} catch (error) {
if (!(error instanceof RangeError)) throw error;
const code = Temporal.PlainDate.from(fields, { overflow: "constrain" }).monthCode;
constrainedMonthCodes.set(key, code);
return code;
}
}
throw new RangeError(`${calendarId} has ${monthCode} in every year`);
}
/** `ConstrainMonthCode`: the ordinal month for `monthCode` in `year`, or `null` outside the range. */
function constrainMonthCode(model, year, monthCode, overflow) {
const ordinal = model.monthOrdinal(year, monthCode);
if (ordinal !== "missing") return ordinal;
if (overflow === "reject") throw new RangeError(`${model.calendarId} year ${year} has no month ${monthCode}`);
const constrained = model.monthOrdinal(year, constrainedMonthCode(model.calendarId, monthCode));
if (constrained === "missing") throw new RangeError(`${model.calendarId} year ${year} has no month for ${monthCode}`);
return constrained;
}
function compareByMonthCode(year, monthCode, day, target) {
return year - target.year || compareMonthCodes(monthCode, target.monthCode) || day - target.day;
}
function compareByOrdinal(year, month, day, target) {
return year - target.year || month - target.month || day - target.day;
}
function requireInRange(value, what) {
if (value === null) throw new RangeError(`${what} is outside the representable range`);
return value;
}
/** The day of `month` regulated per `overflow`, as an ISO-calendared date (RangeError out of range). */
function regulatedDate(model, month, day, overflow) {
const length = model.daysInMonth(month);
if (length !== null && day > length) {
if (overflow === "reject") throw new RangeError(`Day ${day} is past the end of ${model.calendarId} ${month.year}-${month.month}`);
return model.toDate(month.year, month.month, length);
}
return model.toDate(month.year, month.month, day);
}
/** `NonISODateAdd` steps 1–9 for years and months only, as an ISO-calendared date. */
function addYearsAndMonths(model, from, years, months, overflow) {
const year = from.year + years;
const startMonth = requireInRange(constrainMonthCode(model, year, from.monthCode, overflow), `${model.calendarId} ${year}-${from.monthCode}`);
return regulatedDate(model, requireInRange(model.addMonths({
year,
month: startMonth
}, months), `${model.calendarId} month ${months} after ${year}-${startMonth}`), from.day, overflow);
}
/**
* `NonISODateAdd(calendar, date, duration, overflow)`. Weeks and days are ISO day arithmetic, as in
* the spec's final `BalanceNonISODate` + `CalendarIntegersToISO`.
*
* @returns the result as an ISO-calendared PlainDate; throws RangeError out of range or on reject
*/
function nonIsoDateAdd(model, date, duration, overflow) {
return addYearsAndMonths(model, model.fields(date), duration.years, duration.months, overflow).add({ days: 7 * duration.weeks + duration.days });
}
/**
* `NonISODateSurpasses(calendar, sign, from, to, years, months, weeks, days)`. A month or date the
* model cannot place lies beyond the range in the direction of `sign`, hence past `to`.
*/
function nonIsoDateSurpasses(model, sign, from, to, duration) {
const year = from.year + duration.years;
if (sign * compareByMonthCode(year, from.monthCode, from.day, to) > 0) return true;
const startMonth = constrainMonthCode(model, year, from.monthCode, "constrain");
const month = startMonth === null ? null : model.addMonths({
year,
month: startMonth
}, duration.months);
if (month === null || sign * compareByOrdinal(month.year, month.month, from.day, to) > 0) return true;
if (duration.weeks === 0 && duration.days === 0) return false;
try {
const end = regulatedDate(model, month, from.day, "constrain").add({ days: 7 * duration.weeks + duration.days });
return sign * Temporal.PlainDate.compare(end, to.date) > 0;
} catch (error) {
if (error instanceof RangeError) return true;
throw error;
}
}
/** The largest count (from `estimate`, toward `sign`) that does not surpass. */
function maximizeCount(estimate, sign, surpasses) {
let count = sign * estimate > 0 ? estimate : 0;
for (let step = 0; count !== 0 && surpasses(count); step++) {
if (step === MAX_ADJUSTMENTS) throw new RangeError("Calendar difference estimate did not converge");
count -= sign;
}
for (let step = 0; !surpasses(count + sign); step++) {
if (step === MAX_ADJUSTMENTS) throw new RangeError("Calendar difference estimate did not converge");
count += sign;
}
return count;
}
function durationOf(years, months, days = 0) {
return {
years,
months,
weeks: 0,
days
};
}
/** `NonISODateSurpasses` from `one` toward `two`, with the two dates' fields read once. */
function surpassesBetween(model, sign, one, two) {
const from = model.fields(one);
const to = {
...model.fields(two),
date: two
};
return (duration) => nonIsoDateSurpasses(model, sign, from, to, duration);
}
/**
* `NonISODateUntil(calendar, one, two, largestUnit)` for `largestUnit` year or month (days and
* weeks are calendar-independent: callers use ISO `until`).
*/
function nonIsoDateUntil(model, one, two, largestUnit) {
const sign = -Temporal.PlainDate.compare(one, two);
if (sign === 0) return durationOf(0, 0);
const from = model.fields(one);
const to = model.fields(two);
const surpasses = surpassesBetween(model, sign, one, two);
const years = largestUnit === "year" ? maximizeCount(to.year - from.year, sign, (count) => surpasses(durationOf(count, 0))) : 0;
const year = from.year + years;
const startMonth = years === 0 ? from.month : requireInRange(constrainMonthCode(model, year, from.monthCode, "constrain"), `${model.calendarId} ${year}-${from.monthCode}`);
const months = maximizeCount(model.monthsBetween({
year,
month: startMonth
}, to), sign, (count) => surpasses(durationOf(years, count)));
const { days } = addYearsAndMonths(model, from, years, months, "constrain").until(two.withCalendar(ISO_CALENDAR), { largestUnit: "days" });
return durationOf(years, months, days);
}
/**
* True when `result` is exactly `NonISODateUntil(one, two, largestUnit)`: it does not surpass, its
* signs agree, and adding one more of each unit it could hold does surpass (so no unit is short).
* The per-call guard that keeps a correct polyfill answer.
*/
function isNonIsoDateUntilResult(model, one, two, largestUnit, result) {
const sign = -Temporal.PlainDate.compare(one, two);
const { years, months, weeks, days } = result;
if (sign === 0) return years === 0 && months === 0 && weeks === 0 && days === 0;
if (weeks !== 0 || sign * years < 0 || sign * months < 0 || sign * days < 0 || largestUnit === "month" && years !== 0) return false;
const surpasses = surpassesBetween(model, sign, one, two);
return !surpasses(durationOf(years, months, days)) && surpasses(durationOf(years, months, days + sign)) && surpasses(durationOf(years, months + sign)) && (largestUnit === "month" || surpasses(durationOf(years + sign, 0)));
}
export { compareMonthCodes, isNonIsoDateUntilResult, nonIsoDateAdd, nonIsoDateUntil };