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.

81 lines • 4.38 kB
import { Temporal } from "../../internal/temporal.js"; import { UnixUnit } from "../validate/isValidUnixUnit.js"; /** * Units extractable from a unix epoch value via `parseUnitFromUnix`. Covers the * full set of date/time units plus sub-millisecond precision. * * @remarks Members: * * | Member | Description | * | --- | --- | * | `year` | Four digits (e.g. `2024`, `0005`); a sign and six digits outside 0000–9999 (e.g. `+010000`). | * | `month` | Zero-padded 2 (e.g. `03`). | * | `week` | Week-of-year, 1–53: ISO 8601 for `weekStartsOn: "monday"`, UTS #35 (minimal days 1) for `"sunday"`, so late-December days can be week 1. | * | `day` | Zero-padded 2. | * | `dayOfWeek` | 1 (Mon)–7 (Sun). | * | `hour` | Zero-padded 2. | * | `minute` | Zero-padded 2. | * | `second` | Zero-padded 2. | * | `millisecond` | Zero-padded 3. | * | `microsecond` | Zero-padded 3. | * | `nanosecond` | Zero-padded 3 (Temporal's `nanosecond` field is 0–999). | * * @example * import { PlainNowUnit } from "@northguild/gmt/unix"; * const u: PlainNowUnit = "month"; */ export type PlainNowUnit = "year" | "month" | "week" | "day" | "dayOfWeek" | "hour" | "minute" | "second" | "millisecond" | "microsecond" | "nanosecond"; /** * Extract a unit from a unix epoch value. * * - Valid units: "year", "month", "week", "day", "dayOfWeek", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond". Temporal units may also be plural ("hours"). * - `value` is a safe integer or a string of optionally negative ASCII digits. * - `microsecond` and `nanosecond` are the 0–999 Temporal fields, zero-padded to 3 digits. * - Converts to ZonedDateTime then extracts the unit. * - Returns "" for invalid input. * - The `"year"` unit is written as Temporal writes a year: four digits (`"2024"`, `"0005"`), or a * sign and six digits outside 0000–9999 (`"+010000"`, `"-000005"`). * * @param value unix epoch in milliseconds or seconds: a safe integer, or a string of optionally negative ASCII digits * @param unit unit to extract (e.g. "year", "month", "hour") * @param options optional: how `value` is read, the zone its wall clock is read in and the first day of the week; a non-object value (such as `null`) is invalid * @returns extracted unit value as string, or "" on invalid input * * @example parseUnitFromUnix(1700000000000, "year") // "2023" * @example parseUnitFromUnix(-61996320000000, "year") // "0005" (1 June of year 5) * @example parseUnitFromUnix(1700000000, "hour", { epochUnit: "seconds", timeZone: "UTC" }) // "22" * @example parseUnitFromUnix(1704067200000, "week", { timeZone: "UTC" }) // "1" * @example parseUnitFromUnix(1704067200000, "week", { weekStartsOn: "sunday", timeZone: "UTC" }) // "1" * @example parseUnitFromUnix(-86400, "year", { epochUnit: "seconds", timeZone: "UTC" }) // "1969" * @example parseUnitFromUnix("1709217045123", "hours") // "14" (digit string, plural unit, UTC by default) * @example parseUnitFromUnix(1709217045123, "nanosecond") // "000" * @example parseUnitFromUnix("", "year") // "" (a blank string is not epoch 0) * @example parseUnitFromUnix(45870000, "minute", { timeZone: "-00:44:30" }) // "00" (a stored offset with seconds) */ export declare function parseUnitFromUnix(value: number | string, unit: PlainNowUnit | Temporal.PluralUnit<Temporal.DateTimeUnit>, options?: { /** * The unit the epoch values are counted in: `"seconds"` or `"milliseconds"`, singular or * plural. Any other value returns `""`. * * @defaultValue `"milliseconds"` */ epochUnit?: UnixUnit; /** * The time zone the wall-clock fields are read in: an IANA name, a UTC offset (a time zone * identifier such as `+05:30`, `+0530` or `-08`, or a stored offset `±HH:MM[:SS]`, what * `getTimeZoneOffset` returns), or `"local"` for the system time zone. An unknown zone returns * `""`. * * @defaultValue `"UTC"` */ timeZone?: string; /** * The first day of the week, which sets how the `"week"` unit is numbered. `"monday"` gives the * ISO 8601 week number, where week 1 holds the year's first Thursday; `"sunday"` gives the UTS * #35 week number with a Sunday first day and one minimal day, where week 1 holds 1 January. * Any other value returns `""`, whatever the unit. * * @defaultValue `"monday"` */ weekStartsOn?: "monday" | "sunday"; }): string;