@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.
42 lines (40 loc) • 2.89 kB
JavaScript
import { subtractIntervalRecords } from "../../internal/intervalNanoseconds.js";
import { utcIntervalRecordOf } from "../../internal/intervalChecks.js";
// src/utc/interval/intervalDifferenceUtc.ts
/**
* Return the portion(s) of interval A not covered by interval B.
*
* - 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.
* - Delegates to CORE-6's `subtractIntervals` once the arguments pass the UTC-string gate, and
* re-serialises the pieces to canonical `Z` strings.
* - 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, non-`Z` strings, malformed strings, leap seconds).
*
* @param aStart ISO 8601 UTC datetime string for the first interval start
* @param aEnd ISO 8601 UTC datetime string for the first interval end
* @param bStart ISO 8601 UTC datetime string for the second interval start
* @param bEnd ISO 8601 UTC datetime string for the second interval end
* @returns array of `{ start, end }` records representing A minus B, or `[]` on invalid input
*
* @example intervalDifferenceUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T13:00:00Z") // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T12:00:00Z" }, { start: "2024-01-01T13:00:00Z", end: "2024-01-01T17:00:00Z" }]
* @example intervalDifferenceUtc("2024-01-01T09:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T17:00:00Z") // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T12:00:00Z" }] (touching B removes nothing)
* @example intervalDifferenceUtc("2024-01-01T09:00:00Z", "2024-12-31T17:00:00Z", "2024-01-01T09:00:00Z", "2024-12-31T17:00:00Z") // []
* @example intervalDifferenceUtc("invalid", "2024-12-31T17:00:00Z", "2024-06-01T12:00:00Z", "2024-07-01T13:00:00Z") // []
*/
function intervalDifferenceUtc(aStart, aEnd, bStart, bEnd) {
if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return [];
const a = utcIntervalRecordOf(aStart, aEnd);
if (a === null) return [];
const b = utcIntervalRecordOf(bStart, bEnd);
if (b === null) return [];
return subtractIntervalRecords(a, [b]);
}
export { intervalDifferenceUtc };