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.

51 lines (49 loc) • 2.37 kB
import { frameZoned, normalizeZoneFrame } from "./zoneFrame.js"; import { isValidDateTimeUnit } from "../plain/validate/isValidDateTimeUnit.js"; import { resolveDateTimeUnit } from "./resolveDateTimeUnit.js"; import { parseUnixEpochInterval, resolveUnixEpochUnit, unixEpochToInstant } from "./unixEpochValue.js"; // src/internal/resolveUnixIntervalPair.ts /** * Resolve the shared arguments of `intervalLengthUnix`, `intervalCountUnix` and * `splitIntervalByUnitUnix`: a non-reversed epoch pair in `epochUnit`, a singular-or-plural * `DateTimeUnit`, and the zone both ends are read in. * * - Epochs follow the one `unix/` grammar (`parseUnixEpochValue`) and must name Temporal instants. * - `epochUnit` defaults to `"milliseconds"`; `timeZone` defaults to `"UTC"` (`"local"` is the * system zone; a time zone identifier or a stored UTC offset `±HH:MM[:SS]` is read as written; * anything else is invalid). * - The ends are placed with `frameZoned`: for a seconds offset they sit in the offset's * whole-minute zone, moved by its seconds, to compute with and never to write. * * @param start interval start epoch * @param end interval end epoch * @param unit unit name, singular or plural * @param options optional `{ epochUnit, timeZone }` * @returns the zoned ends, singular unit, epoch unit and frame, or null on invalid input * * @example resolveUnixIntervalPair(0, 86400, "days", { epochUnit: "seconds" })?.resolvedUnit // "day" * @example resolveUnixIntervalPair(10, 0, "day") // null (reversed) */ function resolveUnixIntervalPair(start, end, unit, options) { const interval = parseUnixEpochInterval(start, end); const epochUnit = resolveUnixEpochUnit(options?.epochUnit); const frame = normalizeZoneFrame(options?.timeZone); if (interval === null || epochUnit === null || frame === null || typeof unit !== "string") return null; const resolvedUnit = resolveDateTimeUnit(unit); if (!isValidDateTimeUnit(resolvedUnit)) return null; const startInstant = unixEpochToInstant(interval.start, epochUnit); const endInstant = unixEpochToInstant(interval.end, epochUnit); if (startInstant === null || endInstant === null) return null; try { return { startVal: frameZoned(startInstant, frame), endVal: frameZoned(endInstant, frame), resolvedUnit, epochUnit, frame }; } catch { return null; } } export { resolveUnixIntervalPair };