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.

78 lines (76 loc) • 4.82 kB
import { Temporal } from "../../internal/temporal.js"; import { isOptionsArgument } from "../../internal/isObject.js"; import { addToZoned, subtractFromZoned } from "../../internal/zonedWallClockOperations.js"; import { frameInstant, frameZoned, normalizeZoneFrame } from "../../internal/zoneFrame.js"; import { resolveOverflow } from "../../internal/resolveOverflow.js"; import { resolveUnixEpochUnit, toUnixEpoch, unixEpochToInstant } from "../../internal/unixEpochValue.js"; import { isValidDuration } from "../../duration/validate/isValidDuration.js"; // src/unix/interval/intervalFromDurationUnix.ts /** * Construct a Unix epoch 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. * - Converts to `ZonedDateTime` in `timeZone`, adds/subtracts * the duration there, then converts back to epoch — this is what lets calendar units (years/months/ * weeks/days) resolve without a `relativeTo`: unlike a bare `Temporal.Instant`, the `ZonedDateTime` * supplies its own implicit reference point. * - The computed boundary is floored to a whole `epochUnit`, so a duration shorter than one unit does * not survive: with `anchor: "start"` the interval is empty (`"PT0.5S"` in seconds gives the * same `{ start, end }` as `"PT0S"`), and with `anchor: "end"` the start falls back to the * previous whole unit, making the interval one unit long. * - A negative `duration` (e.g. `"-P1D"`) can invert the computed span; returns null when that * happens, mirroring `intervalIntersectionUnix`'s `start > end` rejection. * - Returns null on invalid input (a `value` that is not a safe integer or numeric string of one — * fractions, empty strings and values beyond ±(2^53 − 1) are invalid — invalid `duration`, an * `anchor` other than `"start"`/`"end"`, or an invalid/unavailable timeZone). * - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone, * moved by its seconds, and the moved instant must be inside Temporal's range. So within the * offset's seconds (under a minute) of the last instant Temporal supports * (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first * (`-271821-04-20T00:00:00Z`) for one west, this returns `null`. An offset to the minute has no * such limit. * * @param value Unix epoch value (seconds or milliseconds): a safe integer or a digit string * @param duration ISO 8601 duration string * @param anchor "start" | "end" — which endpoint `value` represents * @param options optional: how `value` is read and how an out-of-range result day is handled * @returns `{ start, end }` with the constructed span (epoch numbers), or null on invalid input * * @example intervalFromDurationUnix(1704067200000, "P1D", "start", { timeZone: "UTC" }) // { start: 1704067200000, end: 1704153600000 } * @example intervalFromDurationUnix(1704153600000, "P1D", "end", { timeZone: "UTC" }) // { start: 1704067200000, end: 1704153600000 } * @example intervalFromDurationUnix(1706659200000, "P1M", "start", { timeZone: "UTC", overflow: "reject" }) // null (Jan 31 + 1 month overflows) * @example intervalFromDurationUnix(1704067200000, "-P10D", "start", { timeZone: "UTC" }) // null (inverted span) * @example intervalFromDurationUnix("1704067200", "P1D", "start", { epochUnit: "second" }) // { start: 1704067200, end: 1704153600 } (UTC by default) * @example intervalFromDurationUnix(NaN, "P1D", "start") // null */ function intervalFromDurationUnix(value, duration, anchor, options) { try { if (!isOptionsArgument(options)) return null; if (!isValidDuration(duration)) return null; if (anchor !== "start" && anchor !== "end") return null; const epochUnit = resolveUnixEpochUnit(options?.epochUnit); const frame = normalizeZoneFrame(options?.timeZone); if (frame === null || epochUnit === null) return null; const instant = unixEpochToInstant(value, epochUnit); if (instant === null) return null; try { const point = frameZoned(instant, frame); const dur = Temporal.Duration.from(duration); const overflow = resolveOverflow(options?.overflow); const other = anchor === "start" ? addToZoned(point, dur, { overflow }) : subtractFromZoned(point, dur, { overflow }); const start = anchor === "start" ? point : other; const end = anchor === "start" ? other : point; if (Temporal.ZonedDateTime.compare(start, end) > 0) return null; return { start: toUnixEpoch(frameInstant(start, frame), epochUnit), end: toUnixEpoch(frameInstant(end, frame), epochUnit) }; } catch { return null; } } catch { return null; } } export { intervalFromDurationUnix };