@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.
61 lines • 3.16 kB
TypeScript
import { Temporal } from "../../internal/temporal.js";
/**
* Units supported by `parseUnitFromZoned` when extracting a value from a zoned
* ISO string. Includes `timeZone` for reading the zone identifier. Each Temporal unit is also
* accepted in its plural form (`"months"`), as Temporal accepts.
*
* @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 (`weekStartsOn`-controlled). |
* | `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 (Temporal's 0–999 field). |
* | `nanosecond` | Zero-padded 3 (Temporal's 0–999 field). |
* | `timeZone` | IANA timeZone identifier string. |
*
* @example
* import { ZonedParseUnit } from "@northguild/gmt/zoned";
* const u: ZonedParseUnit = "month";
*/
export type ZonedParseUnit = Temporal.SmallestUnit<"year" | "month" | "week" | "day" | "hour" | "minute" | "second" | "millisecond" | "microsecond" | "nanosecond"> | "dayOfWeek" | "timeZone";
/**
* Return the requested unit value from an ISO 8601 zoned datetime string.
*
* - Valid units: "year", "month", "week", "day", "dayOfWeek", "hour", "minute", "second", "millisecond", "microsecond", "nanosecond", "timeZone".
* - Uses Temporal.ZonedDateTime.from to parse.
* - `unit` accepts the singular or plural name of a Temporal unit (`"hour"` or `"hours"`), as Temporal
* does; `"dayOfWeek"` has no plural.
* - 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 zoned ISO 8601 datetime string
* @param unit unit to extract
* @param optionsArg optional setting for the week's first day; a non-object value (such as `null`) is invalid
* @returns string representation of the requested unit or "" when invalid
*
* @example parseUnitFromZoned("2024-02-29T12:34:56.789+00:00[UTC]", "year") // "2024"
* @example parseUnitFromZoned("0005-06-01T12:30:00+00:00[UTC]", "year") // "0005"
* @example parseUnitFromZoned("2024-02-29T12:34:56.789+00:00[UTC]", "milliseconds") // "789"
* @example parseUnitFromZoned("2024-12-31T12:00:00+00:00[UTC]", "week", { weekStartsOn: "sunday" }) // "1"
* @example parseUnitFromZoned("invalid", "year") // ""
*/
export declare function parseUnitFromZoned(value: string, unit: ZonedParseUnit, optionsArg?: {
/**
* 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;