@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.
33 lines (31 loc) • 1.66 kB
JavaScript
import { parseCalendarDateValue } from "./calendarDateString.js";
import { calendarSystemOfDateValue } from "./calendarValueOfDate.js";
// src/internal/calendarDatePairPolicy.ts
/**
* Parse two GMT PlainDate values for calendar-unit arithmetic (diff, count, length, split-by-
* unit): both must name the same calendar, which the pair is measured in.
*
* Follows TC39 Temporal's `DifferenceTemporalPlainDate`: when `CalendarEquals` is false it throws
* a RangeError, for every `largestUnit`. Calendars are compared by their canonical id, so
* `ethiopic-amete-alem` and `ethioaa` agree, while `iso8601` and `gregory`, or `ethiopic` and
* `ethioaa`, do not. A bare ISO string names `iso8601`. Callers catch the throw and return their
* sentinel. (Before 1.16.0 the pair fell back to ISO; ordering-only functions still accept
* mixed calendars, because `Temporal.PlainDate.compare` has no calendar check.)
*
* Throws if either value fails to parse as a valid GMT PlainDate string, or if the calendars
* differ — callers validate both values first (e.g. via `isValidCalendarDate`) and wrap this in
* try-catch, consistent with GMT's gate-then-parse-inside-try structure.
*/
function parseCalendarDatePairForArithmetic(aValue, bValue) {
const calendarA = calendarSystemOfDateValue(aValue);
const calendarB = calendarSystemOfDateValue(bValue);
const a = parseCalendarDateValue(aValue);
const b = parseCalendarDateValue(bValue);
if (!calendarA || calendarA !== calendarB) throw new RangeError(`Mismatched calendars: ${String(calendarA)} and ${String(calendarB)}`);
return {
calendar: calendarA,
a,
b
};
}
export { parseCalendarDatePairForArithmetic };