@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.
58 lines (56 loc) • 3.13 kB
JavaScript
import { isOptionsArgument } from "../../internal/isObject.js";
import { durationTotal, zonedUntil } from "../../internal/zonedWallClockDifference.js";
import { resolveUnixIntervalPair } from "../../internal/resolveUnixIntervalPair.js";
// src/unix/interval/intervalLengthUnix.ts
/**
* Return the exact length of a Unix epoch interval in `unit`, as a real (possibly fractional) number.
*
* - Distinct from `intervalCountUnix`, which counts local calendar `unit` boundaries *crossed*
* rather than measuring exact duration.
* - `start` and `end` are each a safe integer or a string of optionally negative ASCII digits.
* - Fixed-length units (hour, minute, second, …) are zone-independent; a day is a local day in
* `options.timeZone`, so Sydney's 23-hour 2024-10-06 is 1 day long there.
* - Accepts singular or plural units (`"day"` and `"days"` behave identically).
* - Returns `0` for a zero-length interval (`start === end`).
* - Returns `null` on invalid input (`start`/`end` that is not a safe integer or numeric string of
* one, or lies outside the Temporal instant range, `start > end`, an unsupported unit, an invalid
* `epochUnit` or an unknown `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 start Unix epoch in `epochUnit` — interval start
* @param end Unix epoch in `epochUnit` — interval end
* @param unit unit string — any `DateTimeUnit`, singular or plural
* @param options optional: how the epochs are read and the zone calendar units are measured in
* @returns exact length of the interval expressed in `unit`, or null on invalid input
*
* @example intervalLengthUnix(0, 86400000, "hour") // 24
* @example intervalLengthUnix(0, 5400000, "hour") // 1.5
* @example intervalLengthUnix(0, 0, "hour") // 0
* @example intervalLengthUnix(86400000, 0, "hour") // null
* @example intervalLengthUnix(1728136800000, 1728219600000, "day", { timeZone: "Australia/Sydney" }) // 1 (the 23-hour local day)
* @example intervalLengthUnix(1710046800000, 1710129600000, "day") // 0.9583333333333334 (23/24 of a UTC day)
* @example intervalLengthUnix("0", "86400", "hours", { epochUnit: "second" }) // 24
* @example intervalLengthUnix(NaN, 86400000, "hour") // null
*/
function intervalLengthUnix(start, end, unit, options) {
try {
if (!isOptionsArgument(options)) return null;
const resolved = resolveUnixIntervalPair(start, end, unit, options);
if (!resolved) return null;
try {
const { startVal, endVal, resolvedUnit } = resolved;
const duration = zonedUntil(startVal, endVal, { largestUnit: resolvedUnit });
return durationTotal(duration, resolvedUnit, startVal);
} catch {
return null;
}
} catch {
return null;
}
}
export { intervalLengthUnix };