@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.
64 lines (62 loc) • 3.89 kB
JavaScript
import { durationOf, plainTimeOf } from "../../internal/inputChecks.js";
// src/plain/interval/intervalFromDurationTime.ts
const NANOSECONDS_PER_DAY = 86400000000000n;
function clockNanoseconds(hours, minutes, seconds, milliseconds, microseconds, nanoseconds) {
return BigInt(hours) * 3600000000000n + BigInt(minutes) * 60000000000n + BigInt(seconds) * 1000000000n + BigInt(milliseconds) * 1000000n + BigInt(microseconds) * 1000n + BigInt(nanoseconds);
}
/**
* 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
*/
function intervalFromDurationTime(value, duration, anchor) {
const point = plainTimeOf(value);
if (typeof value !== "string" || point === null) return null;
const dur = durationOf(duration);
if (dur === null) return null;
if (anchor !== "start" && anchor !== "end") return null;
try {
if (dur.years !== 0 || dur.months !== 0 || dur.weeks !== 0 || dur.days !== 0) return null;
const spanNs = clockNanoseconds(dur.hours, dur.minutes, dur.seconds, dur.milliseconds, dur.microseconds, dur.nanoseconds);
const pointNs = clockNanoseconds(point.hour, point.minute, point.second, point.millisecond, point.microsecond, point.nanosecond);
const otherNs = anchor === "start" ? pointNs + spanNs : pointNs - spanNs;
if (spanNs < 0n || otherNs < 0n || otherNs >= NANOSECONDS_PER_DAY) return null;
const other = anchor === "start" ? point.add(dur) : point.subtract(dur);
const start = anchor === "start" ? point : other;
const end = anchor === "start" ? other : point;
return {
start: start.toString(),
end: end.toString()
};
} catch {
return null;
}
}
export { intervalFromDurationTime };