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.

50 lines • 2.88 kB
import { CalendarOptions } from "../../types/calendar-options.js"; import "../../types/index.js"; /** * Options for `formatCalendarUtc`: the instant the day label is measured from, the time zone the * days and the clock time are read in, and the width of the time-of-day half. */ export interface FormatCalendarUtcOptions extends CalendarOptions { /** * The time zone used for both the calendar-day comparison and the rendered clock time: an IANA * name, a UTC offset to the minute (what `isValidTimeZone` accepts), or `"local"` for the system * time zone. An unknown zone returns `""`, as ECMA-402 throws RangeError for it. * * @defaultValue `"UTC"` */ timeZone?: string; /** * The length of the time-of-day part, as the `timeStyle` of `Intl.DateTimeFormat`. `"short"` * writes hours and minutes, `"medium"` adds seconds, `"long"` adds the locale's short zone * name ("BST" in en-GB, "GMT+1" in en-US) and `"full"` the long one ("British Summer Time" in * English). Any other value returns `""`. * * @defaultValue `"short"` */ timeStyle?: "short" | "medium" | "long" | "full"; } /** * Format a UTC ISO string as a relative day label plus time-of-day, e.g. * "tomorrow at 14:30" — the UTC counterpart of `formatCalendar`. See that * function's JSDoc for the day-label/threshold/connector design; this * variant compares calendar days and renders the clock time in `timeZone`. * * - `options` must be an object or omitted: `null` returns `""`, as Temporal's GetOptionsObject * rejects it. * - An offset with seconds (`-00:44:30`) returns `""`. `Intl.DateTimeFormat` takes a time zone * identifier only: constructing one with an offset that has seconds throws (ECMA-402, the * `DateTimeFormat` construction step "If parseResult contains more than one MinuteSecond Parse * Node, throw a RangeError exception"). Pass the IANA name. * * @param value UTC ISO string to format * @param locale optional: BCP 47 locale tag, or a preference list of tags (ECMA-402) * @param options The reference instant, the time zone and the time style * @returns the formatted calendar string, or "" on invalid input * * @example formatCalendarUtc("2026-03-16T18:30:00Z", "en-GB", { timeZone: "Africa/Johannesburg", reference: "2026-03-15T13:00:00Z" }) // "tomorrow at 20:30" * @example formatCalendarUtc("2026-03-16T13:30:00Z", "fr-FR", { timeZone: "Europe/Paris", reference: "2026-03-15T12:00:00Z" }) // "demain à 14:30" * @example formatCalendarUtc("2026-03-16T18:30:00Z", "hi-IN", null as never) // "" * @example formatCalendarUtc("not-a-date") // "" * @example formatCalendarUtc("2024-03-16T18:30:00Z", ["fr-FR", "en-US"], { reference: "2024-03-15T13:00:00Z" }) // "demain à 18:30" */ export declare function formatCalendarUtc(value: string, locale?: string | string[], options?: FormatCalendarUtcOptions): string;