@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.
29 lines • 2.61 kB
TypeScript
import { Interval } from "../../types/interval.js";
import "../../types/index.js";
/**
* Split a half-open interval at instant boundaries into consecutive half-open pieces.
*
* - Each piece's `end` is the next piece's `start`; under `[start, end)` the pieces share no
* instant and together cover the interval exactly once.
* - `boundaries` may be unsorted. Boundaries at or outside the interval's edges are dropped, so
* no piece is empty; duplicate instants keep their first occurrence's spelling (GMT rule).
* - Endpoints are the caller's own strings: the interval's own `start`/`end`, and each boundary
* exactly as passed.
* - Splitting an empty interval returns it unchanged, as a single piece.
* - Never returns `[]` for valid input — that is the invalid-input sentinel alone.
* - To split at zone-aligned boundaries (local midnights), build them with `floorToZone` or
* `bucketRange` first.
* - Returns `[]` when `interval` is not a valid `Interval`, `boundaries` is not an array, or any
* boundary is not an ISO 8601 instant string — even one outside the interval.
*
* @param interval `{ start, end }` record of ISO 8601 instant strings to split
* @param boundaries array of ISO 8601 instant strings to split at
* @returns consecutive `{ start, end }` pieces covering `interval`, or [] on invalid input
*
* @example splitIntervalAt({ start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" }, ["2024-01-01T12:00:00Z"]) // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T12:00:00Z" }, { start: "2024-01-01T12:00:00Z", end: "2024-01-01T17:00:00Z" }]
* @example splitIntervalAt({ start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" }, ["2024-01-01T15:00:00Z", "2024-01-01T11:00:00Z"]) // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T11:00:00Z" }, { start: "2024-01-01T11:00:00Z", end: "2024-01-01T15:00:00Z" }, { start: "2024-01-01T15:00:00Z", end: "2024-01-01T17:00:00Z" }]
* @example splitIntervalAt({ start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" }, ["2024-01-01T09:00:00Z", "2024-01-01T18:00:00Z"]) // [{ start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" }] — edge and outside dropped
* @example splitIntervalAt({ start: "2024-01-01T12:00:00Z", end: "2024-01-01T12:00:00Z" }, ["2024-01-01T12:00:00Z"]) // [{ start: "2024-01-01T12:00:00Z", end: "2024-01-01T12:00:00Z" }] — empty interval
* @example splitIntervalAt({ start: "2024-01-01T09:00:00Z", end: "2024-01-01T17:00:00Z" }, ["2024-01-01T12:00:00"]) // [] — no offset
*/
export declare function splitIntervalAt(interval: Interval, boundaries: string[]): Interval[];