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.

54 lines (52 loc) • 3.08 kB
import { Temporal } from "../../internal/temporal.js"; import { plainTimeOf } from "../../internal/inputChecks.js"; import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js"; import { normalizeDateTime } from "../../internal/normalizeDateTime.js"; import { plainTimeFormatOptions } from "../../internal/plainFormatOptions.js"; import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js"; // src/plain/format/formatTime.ts /** * Return a localized string for a PlainTime ISO input using Intl options. * * - Formats as Temporal's ECMA-402 PlainTime format (`PlainTime#toLocaleString`) does, through * the runtime's `Intl.DateTimeFormat`. `era` and `timeZoneName` are ignored, so alone they get * the hour, minute and second defaults. * - Accepts optional BCP 47 locale and Intl.DateTimeFormatOptions. * - Returns "" for invalid input, for a `dateStyle` (with or without `timeStyle`) and for only * date fields, where `PlainTime#toLocaleString` throws a TypeError. * - **Compatibility:** before 1.16.0 `era` or `timeZoneName` alone returned `""`, `era` beside a * time field added the era name, and a `dateStyle` beside `timeStyle` was ignored. Pass only * `timeStyle` to keep the styled text. * - `options` null returns `""`, as ECMA-402's CoerceOptionsToObject rejects it. Any * other non-object — a string, a number, a boolean — formats with the defaults, as * `Intl.DateTimeFormat` does: CoerceOptionsToObject calls ToObject on it and the wrapper * carries no recognised option. Only `null` and `undefined` are special-cased. * * @param value ISO PlainTime string * @param locale optional BCP 47 locale identifier, or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns localized time string or "" on invalid input * * @example formatTime("14:30:00", "hi-IN", { timeStyle: "short" }) // "2:30 pm" * @example formatTime("14:30:00", "de-DE", { timeStyle: "short" }) // "14:30" * @example formatTime("14:30:45", "pt-BR", { era: "long" }) // "14:30:45" * @example formatTime("14:30:45", "ja-JP", { dateStyle: "short", timeStyle: "short" }) // "" — a PlainTime has no date * @example formatTime("14:30:45", "ja-JP", { timeStyle: "short" }) // "14:30" — the pre-1.16.0 text * @example formatTime("invalid") // "" * @example formatTime("14:30:45", ["fr-FR", "en-US"], { hour: "2-digit", minute: "2-digit" }) // "14:30" */ function formatTime(value, locale, options) { const valueParsed = plainTimeOf(value); if (valueParsed === null) return ""; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); const resolved = plainTimeFormatOptions(read); if (resolved === null) return ""; const epochMilliseconds = Temporal.PlainDate.from("1970-01-01").toPlainDateTime(valueParsed).toZonedDateTime("UTC").epochMilliseconds; return normalizeDateTime(cachedFormatter("DateTimeFormat", locale, resolved).format(epochMilliseconds)); } catch { return ""; } } export { formatTime };