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.

166 lines • 7.72 kB
import { Temporal } from "./temporal.js"; /** The two units a Unix epoch value is expressed in. */ export type UnixEpochUnit = "seconds" | "milliseconds"; /** * Parse one Unix epoch argument into a number, or null — the one epoch grammar every `unix/` * function and validator shares. * * - A number must be a safe integer (`Number.isSafeInteger`): fractional values, `NaN`, * `±Infinity` and anything beyond ±(2^53 − 1) are rejected. Past 2^53 consecutive integers are * no longer distinct doubles (`2 ** 53 + 1 === 2 ** 53`). Every Temporal instant (±8.64e15 ms) is * well inside that range. * - A string must be an optionally negative run of ASCII digits (`/^-?\d+$/`) naming a safe * integer. No whitespace, no `+`, no hex, binary, exponent, decimal point or numeric separator: * POSIX epoch time is an integer count and has no other notation, so `Number()`'s wider * StringToNumber grammar (`" 12 "`, `"0x10"`, `"1e3"`, `""` → 0) is not used. * - `-0` reads as `0`. * - Any other type is rejected. * * @param value Unix epoch value as a number or digit string * @returns the epoch as a safe integer, or null when invalid * * @example parseUnixEpochValue(1704067200000) // 1704067200000 * @example parseUnixEpochValue("-86400") // -86400 * @example parseUnixEpochValue(1.5) // null * @example parseUnixEpochValue(" 12") // null * @example parseUnixEpochValue("1e3") // null */ export declare function parseUnixEpochValue(value: unknown): number | null; /** * Resolve an `epochUnit` option: omitted → `"milliseconds"`; singular or plural `second` / * `millisecond` → the plural name; anything else → null. * * - Singular and plural are the same unit, as in Temporal §13.17 GetTemporalUnitValuedOption. * * @param unit candidate `epochUnit` value * @returns the resolved unit, or null when invalid * * @example resolveUnixEpochUnit(undefined) // "milliseconds" * @example resolveUnixEpochUnit("second") // "seconds" * @example resolveUnixEpochUnit("nanoseconds") // null */ export declare function resolveUnixEpochUnit(unit: unknown): UnixEpochUnit | null; /** * Resolve the `epochUnit` of an options argument (`{ epochUnit }`), or null. * * - `undefined` options → `"milliseconds"`, the same as omitted (TC39 GetOptionsObject). * - Anything that is not an object (`null`, a legacy positional unit string such as `"seconds"`, a * number) → null: Temporal GetOptionsObject throws `TypeError` for it, so it is never silently * read as the default. * - Otherwise `resolveUnixEpochUnit(options.epochUnit)`. * * @param options candidate options argument * @returns the resolved unit, or null when invalid * * @example resolveUnixEpochUnitOptions(undefined) // "milliseconds" * @example resolveUnixEpochUnitOptions({ epochUnit: "second" }) // "seconds" * @example resolveUnixEpochUnitOptions("seconds") // null */ export declare function resolveUnixEpochUnitOptions(options: unknown): UnixEpochUnit | null; /** * Read a Unix epoch argument (see `parseUnixEpochValue`) as a `Temporal.Instant`, or null when it * is not an epoch or lies outside the Temporal instant range. * * - Seconds scale by 1000 exactly: every in-range seconds value (±8.64e12) times 1000 is a safe * integer, and any product that is not exact is already outside the instant range. * * @param value Unix epoch value as a number or digit string * @param epochUnit unit the value is expressed in * @returns the instant, or null * * @example unixEpochToInstant(0, "seconds")?.toString() // "1970-01-01T00:00:00Z" * @example unixEpochToInstant(8.64e15 + 1, "milliseconds") // null */ export declare function unixEpochToInstant(value: unknown, epochUnit: UnixEpochUnit): Temporal.Instant | null; /** * Express an instant (or zoned date-time) as a Unix epoch in `epochUnit`. * * - Seconds floor toward −∞, so `1969-12-31T23:59:59.999Z` is second `-1`, as POSIX * `time_t` counts whole elapsed seconds. * * @param value anything with `epochMilliseconds` * @param epochUnit unit of the result * @returns the epoch value * * @example toUnixEpoch({ epochMilliseconds: 1710685845999 }, "seconds") // 1710685845 * @example toUnixEpoch({ epochMilliseconds: -1 }, "seconds") // -1 */ export declare function toUnixEpoch(value: { epochMilliseconds: number; }, epochUnit: UnixEpochUnit): number; /** * Parse an epoch for the aggregators (`sortUnix`, `minUnix`, `maxUnix`) that take no `epochUnit`: * the epoch grammar (`parseUnixEpochValue`) inside ±8.64e15, or null. * * - ±8.64e15 is the instant range in milliseconds. It contains the seconds range (±8.64e12), so no * valid epoch of either unit is dropped. * * @param value candidate epoch value * @returns the epoch as a number, or null * * @example parseUnixEpochInInstantRange("1700000000000") // 1700000000000 * @example parseUnixEpochInInstantRange(1e20) // null */ export declare function parseUnixEpochInInstantRange(value: unknown): number | null; /** * The values of a list that `parseUnixEpochInInstantRange` accepts, in list order. * * @param values candidate epoch values * @returns the accepted epochs as numbers; empty when `values` is not an array or none is valid * * @example parseUnixEpochsInInstantRange(["1700000000000", 1.5, 1e20, -1]) // [1700000000000, -1] * @example parseUnixEpochsInInstantRange("1700000000000") // [] */ export declare function parseUnixEpochsInInstantRange(values: unknown): number[]; /** * Parse a Unix epoch interval `[start, end]`, or null when either value is invalid (see * `parseUnixEpochValue`) or the interval is reversed (`start > end`). * * @param start Unix epoch value, in the one unit all epoch arguments share — interval start * @param end Unix epoch value, in the one unit all epoch arguments share — interval end * @returns `{ start, end }` as safe integers, or null * * @example parseUnixEpochInterval(0, "1500000000") // { start: 0, end: 1500000000 } * @example parseUnixEpochInterval(10, 0) // null (reversed) * @example parseUnixEpochInterval(0, 1.5) // null (fractional) */ export declare function parseUnixEpochInterval(start: unknown, end: unknown): { start: number; end: number; } | null; /** * Parse two Unix epoch intervals `[aStart, aEnd]` and `[bStart, bEnd]`, or null when either is * invalid or reversed (see `parseUnixEpochInterval`). * * @param aStart first interval start * @param aEnd first interval end * @param bStart second interval start * @param bEnd second interval end * @returns `[a, b]` as `{ start, end }` safe-integer records, or null * * @example parseUnixEpochIntervalPair(0, 10, "5", "20") // [{ start: 0, end: 10 }, { start: 5, end: 20 }] * @example parseUnixEpochIntervalPair(0, 10, 20, 5) // null (B reversed) */ export declare function parseUnixEpochIntervalPair(aStart: unknown, aEnd: unknown, bStart: unknown, bEnd: unknown): [{ start: number; end: number; }, { start: number; end: number; }] | null; /** * Parse a list of Unix epoch `{ start, end }` records, or null when `intervals` is not an array or * any element is not an object holding a valid, non-reversed interval (see * `parseUnixEpochInterval`). An empty list parses to `[]`. * * @param intervals candidate list of `{ start, end }` records * @returns the parsed intervals in input order, or null * * @example parseUnixEpochIntervalList([{ start: 0, end: "10" }]) // [{ start: 0, end: 10 }] * @example parseUnixEpochIntervalList([{ start: 10, end: 0 }]) // null (reversed) * @example parseUnixEpochIntervalList("not-an-array") // null */ export declare function parseUnixEpochIntervalList(intervals: unknown): Array<{ start: number; end: number; }> | null;