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.

37 lines (35 loc) • 2.31 kB
import { halfOpenIntersection } from "../../internal/halfOpenIntervals.js"; import { parseUnixEpochIntervalPair } from "../../internal/unixEpochValue.js"; // src/unix/interval/intervalIntersectionUnix.ts /** * Return the overlapping span of two half-open Unix epoch intervals `[start, end)`, or null when * they do not overlap. * * - Compares numeric Unix epoch values directly. * - The result is `[max(aStart, bStart), min(aEnd, bEnd))`, and it exists only when * `intervalsOverlapUnix` is true — the same rule as `intersectIntervals`. * - Touching intervals (e.g. `aEnd === bStart`) share no instant, because each `end` is excluded, * and return `null`. * - An empty interval (`start === end`) 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: non-numeric types, empty strings, and values that are not * safe integers (fractions, `NaN`, `±Infinity`, beyond ±(2^53 − 1)). * * @param aStart Unix epoch value, in the one unit all epoch arguments share — first interval start * @param aEnd Unix epoch value, in the one unit all epoch arguments share — first interval end * @param bStart Unix epoch value, in the one unit all epoch arguments share — second interval start * @param bEnd Unix epoch value, in the one unit all epoch arguments share — second interval end * @returns `{ start, end }` with the overlapping span, or null on invalid input / no overlap * * @example intervalIntersectionUnix(0, 1700000000, 1000000, 2000000) // { start: 1000000, end: 2000000 } * @example intervalIntersectionUnix(0, 1000000, 1000000, 2000000) // null (touching) * @example intervalIntersectionUnix(0, 1000001, 1000000, 2000000) // { start: 1000000, end: 1000001 } * @example intervalIntersectionUnix(NaN, 1700000000, 1000000, 2000000) // null * @example intervalIntersectionUnix("0", "1700000000", "1000000", "2000000") // { start: 1000000, end: 2000000 } */ function intervalIntersectionUnix(aStart, aEnd, bStart, bEnd) { const pair = parseUnixEpochIntervalPair(aStart, aEnd, bStart, bEnd); return pair === null ? null : halfOpenIntersection(pair[0], pair[1], (left, right) => left - right); } export { intervalIntersectionUnix };