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.

59 lines (57 loc) • 3.14 kB
import { Temporal } from "../../internal/temporal.js"; import { plainTimeOf } from "../../internal/inputChecks.js"; import { halfOpenContainsSpan } from "../../internal/halfOpenIntervals.js"; // src/plain/interval/intervalEngulfsTime.ts /** * Return true when the half-open interval B `[bStart, bEnd)` lies within the half-open interval * A `[aStart, aEnd)` — every clock time 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 `intervalContainsTime(aStart, aEnd, bStart, bEnd)`. * - PlainTime has no day rollover: an interval never wraps past midnight. And because `end` is * excluded, no interval *contains* `23:59:59.999999999` — containing it would need an `end` * greater than it, and no PlainTime is. An interval may still *have* it as its `end`: * `intervalContainsTime("09:00:00", "23:59:59.999999999", "23:00:00")` is `true`, while * `intervalContainsTime("09:00:00", "23:59:59.999999999", "23:59:59.999999999")` is `false`. * - Uses `Temporal.PlainTime.compare` for comparison. * - Returns `false` if either interval is invalid (`start > end`). * - Returns `false` on invalid input (wrong type, malformed strings). * * @param aStart ISO 8601 time string for the outer interval start * @param aEnd ISO 8601 time string for the outer interval end (excluded) * @param bStart ISO 8601 time string for the inner interval start * @param bEnd ISO 8601 time string for the inner interval end (excluded) * @returns true if B lies within A, or false on invalid input * * @example intervalEngulfsTime("09:00:00", "17:00:00", "12:00:00", "13:00:00") // true * @example intervalEngulfsTime("09:00:00", "17:00:00", "09:00:00", "17:00:00") // true (equal intervals) * @example intervalEngulfsTime("09:00:00", "17:00:00", "12:00:00", "17:00:00") // true (same end) * @example intervalEngulfsTime("09:00:00", "17:00:00", "12:00:00", "12:00:00") // true (empty interval strictly inside) * @example intervalEngulfsTime("09:00:00", "17:00:00", "17:00:00", "17:00:00") // false (empty interval at the edge) * @example intervalEngulfsTime("12:00:00", "13:00:00", "09:00:00", "17:00:00") // false */ function intervalEngulfsTime(aStart, aEnd, bStart, bEnd) { if (typeof aStart !== "string" || typeof aEnd !== "string" || typeof bStart !== "string" || typeof bEnd !== "string") return false; const aS = plainTimeOf(aStart); const aE = plainTimeOf(aEnd); const bS = plainTimeOf(bStart); const bE = plainTimeOf(bEnd); if (aS === null || aE === null || bS === null || bE === null) return false; try { if (Temporal.PlainTime.compare(aS, aE) > 0) return false; if (Temporal.PlainTime.compare(bS, bE) > 0) return false; return halfOpenContainsSpan({ start: aS, end: aE }, { start: bS, end: bE }, Temporal.PlainTime.compare); } catch { return false; } } export { intervalEngulfsTime };