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.

64 lines (62 loc) • 3.27 kB
import { stringOptionOrDefault } from "../../internal/typedOptions.js"; import { nowInstant } from "../../internal/temporalCompat/clock.js"; import { isOptionsObject } from "../../internal/isObject.js"; import { utcInstantOf } from "../../internal/utcInstant.js"; import { formatCalendarInstants } from "../../internal/formatCalendarDays.js"; import { normalizeTimeZone } from "../../internal/normalizeTimeZone.js"; // src/utc/format/formatCalendarUtc.ts /** * 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. * - A `chinese` or `dangi` date follows the observatory the standard names: Purple Mountain * Observatory for `chinese`, KASI for `dangi`. A runtime that prints a different date for one of * them returns `""` for any format with a year, month, day or era in that calendar. A runtime * that prints the observatory's date keeps its own text. A format with only a time or weekday * field always returns its text. * * @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" */ function formatCalendarUtc(value, locale, options = {}) { try { if (!isOptionsObject(options)) return ""; const target = utcInstantOf(value); if (target === null) return ""; const timeZone = normalizeTimeZone(options.timeZone); if (!timeZone) return ""; const referenceOption = options.reference; const parsedReference = referenceOption === void 0 ? void 0 : utcInstantOf(referenceOption); if (parsedReference === null) return ""; let reference = parsedReference; if (reference === void 0) try { reference = nowInstant(); } catch { return ""; } try { return formatCalendarInstants(target, reference, timeZone, locale, stringOptionOrDefault(options.timeStyle, "short")); } catch { return ""; } } catch { return ""; } } export { formatCalendarUtc };