@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.
70 lines (68 loc) • 3.82 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { calendarOfAllZonedValues } from "../../internal/calendarValueOfZoned.js";
import { formatZonedInCalendar } from "../../internal/calendarZonedString.js";
import { isObject } from "../../internal/isObject.js";
import { halfOpenXor } from "../../internal/halfOpenIntervals.js";
import { calendarZonedIntervalOf } from "../../internal/intervalChecks.js";
// src/zoned/interval/intervalXorAllZoned.ts
/**
* Return the symmetric difference across a list of zoned intervals — the set of instants
* covered by an odd number of the input intervals.
*
* - List-form generalization of `intervalXorZoned`, which is pairwise only.
* - Intervals are half-open `[start, end)`. The result is every maximal run of instants where the
* coverage count is odd, so touching runs join and an empty interval (`start` and `end` the same
* instant) changes nothing. Every boundary is an input's own `start` or `end`; none is stepped
* by a nanosecond, so an interval may end on the last representable instant. For two intervals
* this is exactly `intervalXorZoned`'s pairwise result.
* - Output boundaries carry the time zone of whichever input interval contributed them (the
* earlier one in the list on a tie).
* - Order of the input list does not matter; the result is sorted by start instant.
* - Returns `[]` for an empty list, and `[]` when every instant is covered an even number of
* times (e.g. two identical intervals cancel out).
* - Returns `[]` when `intervals` is not an array, when any element is not a
* `{ start, end }` record of valid ISO ZonedDateTime strings, or when any element has
* `start > end` or a leap-second string.
* - Accepts RFC 9557 calendar-annotated zoned strings (as produced by `convertZonedToCalendar`) as
* well as bare ISO ones — E7 (issue #152) — but **rejects a mismatched set**: every endpoint in
* the list must name the same calendar system (E7's D4-zoned). This function returns *values*
* the caller reads back as datetimes, and an array whose elements carried different calendar
* tags would be unreadable as a set. A mismatch returns `[]`.
* - Output boundaries are re-derived in the resolved calendar via `formatZonedInCalendar`, never
* copied from an input string (E7's D7-zoned).
*
* @param intervals array of `{ start, end }` records
* @returns array of `{ start, end }` records covered an odd number of times, or `[]` on invalid input
*
* @example intervalXorAllZoned([{ start: "2024-01-01T00:00:00+00:00[UTC]", end: "2024-01-10T00:00:00+00:00[UTC]" }, { start: "2024-01-05T00:00:00+00:00[UTC]", end: "2024-01-15T00:00:00+00:00[UTC]" }]) // [{ start: "2024-01-01T00:00:00+00:00[UTC]", end: "2024-01-05T00:00:00+00:00[UTC]" }, { start: "2024-01-10T00:00:00+00:00[UTC]", end: "2024-01-15T00:00:00+00:00[UTC]" }]
* @example intervalXorAllZoned([]) // []
*/
function intervalXorAllZoned(intervals) {
try {
if (!Array.isArray(intervals) || intervals.length === 0) return [];
const parsed = [];
if (!intervals.every((interval, index) => {
if (!isObject(interval) || typeof interval.start !== "string" || typeof interval.end !== "string") return false;
const pair = calendarZonedIntervalOf(interval.start, interval.end);
if (pair === null) return false;
parsed[index] = {
start: pair[0],
end: pair[1]
};
return true;
})) return [];
const calendar = calendarOfAllZonedValues(intervals.flatMap((interval) => [interval.start, interval.end]));
if (!calendar) return [];
try {
return halfOpenXor(parsed, Temporal.ZonedDateTime.compare).map(({ start, end }) => ({
start: formatZonedInCalendar(start, calendar),
end: formatZonedInCalendar(end, calendar)
}));
} catch {
return [];
}
} catch {
return [];
}
}
export { intervalXorAllZoned };