@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.
45 lines • 2.88 kB
TypeScript
/**
* Construct a time interval from a single point plus an ISO 8601 duration, anchored at either end.
*
* - `anchor: "start"` treats `value` as the interval start and adds `duration` to get the end.
* - `anchor: "end"` treats `value` as the interval end and subtracts `duration` to get the start.
* - `PlainTime` has no calendar, so a `duration` with a nonzero years/months/weeks/days component
* cannot be resolved against a bare clock time — there is no `relativeTo` to supply (mirrors
* A2/A3's documented `relativeTo` gap). `Temporal.PlainTime.prototype.add` silently discards
* those fields rather than throwing, so this is checked explicitly and returns null.
* - The span must fit inside one day in the anchored direction. A `duration` that crosses
* midnight (e.g. `"23:00:00"` + `"PT2H"`), any `duration` of 24 hours or more (e.g. `"PT24H"`,
* even though the wrapped clock time looks valid), and any negative `duration` return null —
* PlainTime has no date component for the day rollover. The span is measured exactly in
* nanoseconds, not by comparing wrapped clock times.
* - There is no options argument (its only member, `overflow`, was removed in 1.16.0): Temporal
* `PlainTime#add`/`#subtract` take no options.
* - Returns null on invalid input (unparseable `value`, invalid `duration`, or an `anchor` other
* than `"start"`/`"end"`).
*
* @param value ISO PlainTime string
* @param duration ISO 8601 duration string
* @param anchor "start" | "end" — which endpoint `value` represents
* @returns `{ start, end }` with the constructed span, or null on invalid input
*
* @example intervalFromDurationTime("12:00:00", "PT1H", "start") // { start: "12:00:00", end: "13:00:00" }
* @example intervalFromDurationTime("13:00:00", "PT1H", "end") // { start: "12:00:00", end: "13:00:00" }
* @example intervalFromDurationTime("12:00:00", "P1D", "start") // null (date units need relativeTo, unsupported)
* @example intervalFromDurationTime("23:00:00", "PT2H", "start") // null (wraps past midnight, inverted span)
* @example intervalFromDurationTime("10:00:00", "PT24H", "start") // null (24 hours or more never fits one day)
* @example intervalFromDurationTime("12:00:00", "-PT23H", "start") // null (negative duration)
* @example intervalFromDurationTime("invalid", "PT1H", "start") // null
*/
export declare function intervalFromDurationTime(value: string, duration: string, anchor: "start" | "end"): {
/**
* The clock time the interval begins at, as an ISO 8601 time string with no date, offset or time
* zone.
*/
start: string;
/**
* The first clock time after the interval, in the same format as `start`. It is exclusive: the
* interval holds everything from `start` up to but not including this value. It can equal
* `start`, which makes the interval empty.
*/
end: string;
} | null;