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) • 3.06 kB
import { Temporal } from "../../internal/temporal.js"; import { halfOpenContainsSpan } from "../../internal/halfOpenIntervals.js"; import { utcInstantOf } from "../../internal/utcInstant.js"; // src/utc/interval/intervalEngulfsUtc.ts /** * Return true when the half-open interval B `[bStart, bEnd)` lies within the half-open interval * A `[aStart, aEnd)` — every instant of B is also in A. * * - Half-open: an interval holds every `t` with `start <= t < end`. B may start at A's start and * end at A's end, because neither end is part of either interval. * - B must also overlap A, so an empty B (`bStart === bEnd`) counts only strictly inside A, never * at an edge (CORE-6's `clampInterval` clamps it away there). * - Equivalent to 4-argument `intervalContainsUtc(aStart, aEnd, bStart, bEnd)`. * - Uses `Temporal.Instant.compare` for comparison. * - Returns `false` if either interval is invalid (`start > end`). * - Returns `false` on invalid input (wrong type, non-`Z` strings, malformed strings, leap seconds). * * @param aStart ISO 8601 UTC datetime string for the outer interval start * @param aEnd ISO 8601 UTC datetime string for the outer interval end (excluded) * @param bStart ISO 8601 UTC datetime string for the inner interval start * @param bEnd ISO 8601 UTC datetime string for the inner interval end (excluded) * @returns true if B lies within A, or false on invalid input * * @example intervalEngulfsUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T13:00:00Z") // true * @example intervalEngulfsUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z") // true (equal intervals) * @example intervalEngulfsUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T17:00:00Z") // true (same end) * @example intervalEngulfsUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T12:00:00Z", "2024-01-01T12:00:00Z") // true (empty interval strictly inside) * @example intervalEngulfsUtc("2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T17:00:00Z", "2024-01-01T17:00:00Z") // false (empty interval at the edge) * @example intervalEngulfsUtc("2024-01-01T12:00:00Z", "2024-01-01T13:00:00Z", "2024-01-01T09:00:00Z", "2024-01-01T17:00:00Z") // false */ function intervalEngulfsUtc(aStart, aEnd, bStart, bEnd) { if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return false; const aS = utcInstantOf(aStart); if (aS === null) return false; const aE = utcInstantOf(aEnd); if (aE === null) return false; const bS = utcInstantOf(bStart); if (bS === null) return false; const bE = utcInstantOf(bEnd); if (bE === null) return false; try { if (Temporal.Instant.compare(aS, aE) > 0) return false; if (Temporal.Instant.compare(bS, bE) > 0) return false; return halfOpenContainsSpan({ start: aS, end: aE }, { start: bS, end: bE }, Temporal.Instant.compare); } catch { return false; } } export { intervalEngulfsUtc };