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.

57 lines (55 loc) • 2.98 kB
import { Temporal } from "../../internal/temporal.js"; import { plainDateTimeOf } from "../../internal/inputChecks.js"; import { halfOpenDifference } from "../../internal/halfOpenIntervals.js"; // src/plain/interval/intervalDifferenceDateTime.ts /** * Return the portion(s) of interval A not covered by interval B. * * - Uses `Temporal.PlainDateTime.compare` for comparison. * - Half-open: an interval holds every `t` with `start <= t < end`. A returned piece ends exactly * where B starts, or starts exactly where B ends, because B's `end` is not in B (CORE-6's * `subtractIntervals`). No piece is stepped by one unit. * - B touching A (`bStart === aEnd` or `bEnd === aStart`), or empty, removes nothing. * - An empty A (`aStart === aEnd`) has nothing left: returns `[]`. Every returned piece is non-empty. * - Returns `[]` when B fully covers A, which includes B equal to A. * - Returns `[{ start, end }]` when B overlaps exactly one edge of A, leaving one piece. * - Returns `[{ start, end }, { start, end }]` when B is fully inside A with gaps on both sides. * - Returns A unchanged when B lies entirely before or after it. * - Returns `[]` if either interval is invalid (`start > end`). * - Returns `[]` on invalid input (wrong type, malformed strings). * * @param aStart ISO 8601 datetime string for the first interval start * @param aEnd ISO 8601 datetime string for the first interval end * @param bStart ISO 8601 datetime string for the second interval start * @param bEnd ISO 8601 datetime string for the second interval end * @returns array of `{ start, end }` records representing A minus B, or `[]` on invalid input * * @example intervalDifferenceDateTime("2024-01-01T09:00:00", "2024-01-01T17:00:00", "2024-01-01T12:00:00", "2024-01-01T13:00:00") // [{ start: "2024-01-01T09:00:00", end: "2024-01-01T12:00:00" }, { start: "2024-01-01T13:00:00", end: "2024-01-01T17:00:00" }] * @example intervalDifferenceDateTime("2024-01-01T09:00:00", "2024-01-01T17:00:00", "2024-01-01T09:00:00", "2024-01-01T17:00:00") // [] * @example intervalDifferenceDateTime("invalid", "2024-12-31T17:00:00", "2024-06-01T12:00:00", "2024-07-01T13:00:00") // [] */ function intervalDifferenceDateTime(aStart, aEnd, bStart, bEnd) { if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return []; const aS = plainDateTimeOf(aStart); const aE = plainDateTimeOf(aEnd); const bS = plainDateTimeOf(bStart); const bE = plainDateTimeOf(bEnd); if (aS === null || aE === null || bS === null || bE === null) return []; try { if (Temporal.PlainDateTime.compare(aS, aE) > 0) return []; if (Temporal.PlainDateTime.compare(bS, bE) > 0) return []; return halfOpenDifference({ start: aS, end: aE }, [{ start: bS, end: bE }], Temporal.PlainDateTime.compare).map(({ start, end }) => ({ start: start.toString(), end: end.toString() })); } catch { return []; } } export { intervalDifferenceDateTime };