UNPKG

@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.

78 lines (76 loc) • 4.59 kB
import { Temporal } from "../../internal/temporal.js"; import { calendarOfAllZonedValues } from "../../internal/calendarValueOfZoned.js"; import { formatZonedInCalendar, parseCalendarZonedValue } from "../../internal/calendarZonedString.js"; import { halfOpenIntersection } from "../../internal/halfOpenIntervals.js"; import { isValidCalendarZonedDateTime } from "../validate/isValidCalendarZonedDateTime.js"; // src/zoned/interval/intervalIntersectionZoned.ts /** * Return the overlapping span of two half-open zoned intervals `[start, end)`, or null when they * do not overlap. * * - Uses `Temporal.ZonedDateTime.compare` for comparison (same instant semantics). * - The result is `[max(aStart, bStart), min(aEnd, bEnd))`, and it exists only when * `intervalsOverlapZoned` is true — the same rule as `intersectIntervals`. On a tie the first * interval's boundary wins, so its time zone is the one kept. * - Touching intervals (e.g. `aEnd` the same instant as `bStart`) share no instant, because each * `end` is excluded, and return `null`. * - An empty interval (`start` and `end` the same instant) strictly inside the other returns * itself; at either edge, or against another empty interval, it returns `null`. * - Returns `null` if either interval is invalid (`start > end`). * - Returns `null` on invalid input (wrong type, malformed strings, leap seconds). * - Accepts RFC 9557 calendar-annotated zoned strings (as produced by `convertZonedToCalendar`) as * well as bare ISO ones — E7 (issue #152) — but **rejects a mismatched pair**: every endpoint * must name the same calendar system (E7's D4-zoned). Unlike the ordering functions, this one * returns a *value* the caller reads back as a datetime, and there is no principled way to pick * one endpoint's calendar as the answer's. Rejection also keeps a uniform policy across all * eight value-returning zoned set operations, four of which return arrays — a per-element * "winner's tag" would produce a result set whose members disagree about which calendar they * are in. (`intervalUnionZoned`'s existing "winning endpoint's *time zone* wins" is not * precedent: the zone is a property of the surviving point, the calendar is a property of the * answer.) A mismatch returns the sentinel. * - Output boundaries are re-derived in the resolved calendar via `formatZonedInCalendar`, never * copied from an input string (E7's D7-zoned). * - Rejects a calendar annotation before the time zone annotation, which is not RFC 9557. * * @param aStart ISO 8601 zoned datetime string for the first interval start * @param aEnd ISO 8601 zoned datetime string for the first interval end * @param bStart ISO 8601 zoned datetime string for the second interval start * @param bEnd ISO 8601 zoned datetime string for the second interval end * @returns `{ start, end }` with the overlapping span, or null on invalid input / no overlap * * @example intervalIntersectionZoned("2024-01-01T00:00:00+00:00[UTC]", "2024-06-30T23:59:59+00:00[UTC]", "2024-04-01T00:00:00+00:00[UTC]", "2024-12-31T23:59:59+00:00[UTC]") // { start: "2024-04-01T00:00:00+00:00[UTC]", end: "2024-06-30T23:59:59+00:00[UTC]" } * @example intervalIntersectionZoned("2024-01-01T09:00:00+00:00[UTC]", "2024-01-01T12:00:00+00:00[UTC]", "2024-01-01T12:00:00+00:00[UTC]", "2024-01-01T17:00:00+00:00[UTC]") // null (touching: no shared instant) * @example intervalIntersectionZoned("invalid", "2024-06-30T23:59:59+00:00[UTC]", "2024-04-01T00:00:00+00:00[UTC]", "2024-12-31T23:59:59+00:00[UTC]") // null */ function intervalIntersectionZoned(aStart, aEnd, bStart, bEnd) { if (!isValidCalendarZonedDateTime(aStart) || !isValidCalendarZonedDateTime(aEnd) || !isValidCalendarZonedDateTime(bStart) || !isValidCalendarZonedDateTime(bEnd)) return null; const calendar = calendarOfAllZonedValues([ aStart, aEnd, bStart, bEnd ]); if (!calendar) return null; try { const aZdt = parseCalendarZonedValue(aStart); const aZde = parseCalendarZonedValue(aEnd); const bZdt = parseCalendarZonedValue(bStart); const bZde = parseCalendarZonedValue(bEnd); if (Temporal.ZonedDateTime.compare(aZdt, aZde) > 0) return null; if (Temporal.ZonedDateTime.compare(bZdt, bZde) > 0) return null; const overlap = halfOpenIntersection({ start: aZdt, end: aZde }, { start: bZdt, end: bZde }, Temporal.ZonedDateTime.compare); return overlap === null ? null : { start: formatZonedInCalendar(overlap.start, calendar), end: formatZonedInCalendar(overlap.end, calendar) }; } catch { return null; } } export { intervalIntersectionZoned };