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.

75 lines (73 loc) • 6.38 kB
import { Temporal } from "../../internal/temporal.js"; import { cachedFormatter, checkFormatterArguments } from "../../internal/formatterCache.js"; import { isValidDateTime } from "../validate/isValidDateTime.js"; import { plainDateTimeFormatOptions } from "../../internal/plainFormatOptions.js"; import { readDateTimeFormatOptions } from "../../internal/readDateTimeFormatOptions.js"; // src/plain/format/formatDateTimeToParts.ts /** * Return the locale-formatted parts of a PlainDateTime. * * - Use the parts instead of a fixed field pattern. A pattern such as UTS #35's `dd/MM/yyyy` fixes * one field order for every locale; ECMA-402's `Intl.DateTimeFormat.prototype.formatToParts` * (§11.3.6; `FormatDateTimeToParts` §11.5.7) returns the fields in the order of the locale's * CLDR date pattern, so the caller controls presentation and the locale keeps control of order. * - Each part is `{ type, value }` where `type` can be: * `"era"`, `"year"`, `"relatedYear"`, `"yearName"`, `"month"`, `"day"`, `"weekday"`, * `"hour"`, `"minute"`, `"second"`, `"fractionalSecond"`, `"dayPeriod"`, `"literal"`. * - A lunisolar calendar adds two more: `zh-u-ca-chinese` and `ko-u-ca-dangi` emit * `"relatedYear"` (the Gregorian year the cycle falls in) and `"yearName"` (the sexagenary * cycle name) in place of, or beside, `"year"`. Code that looks up a `"year"` part must handle * their absence. * - The caller should iterate the array as returned; reassembling in a fixed * order reintroduces exactly the bug `formatToParts` exists to avoid. * - With no date/time field and no `dateStyle`/`timeStyle`, year, month, day, * hour, minute and second default to `"numeric"` (ECMA-402 as amended by * Temporal), so the parts rebuild exactly what * `Temporal.PlainDateTime#toLocaleString` returns for the same value. * - A zoneless value never yields a `"timeZoneName"` part (Temporal's PlainDateTime format has no * zone field): a `timeZoneName` option is ignored, and a `"long"`/`"full"` `timeStyle` keeps its * hour, minute and second and drops the zone, formatting as the locale's `"medium"` time. As in * `Intl.DateTimeFormat`, `timeZoneName` together with `dateStyle`/`timeStyle` yields `[]`. * - The literal before the day period depends on the runtime. CLDR 42 (ICU 72) put U+202F NARROW * NO-BREAK SPACE there for `en-US`; Node 22 and 24 return it in the parts, and Node 26 returns * an ordinary space (U+0020) instead. Both were checked on those runtimes. The examples show the * Node 22 and 24 form, written as `"\u202f"` so the difference is visible. Treat the two * characters as equal when you compare parts. * - Returns `[]` for invalid input. * - **Compatibility:** before 1.16.0 a call with no field options returned only the date parts. * Pass `{ year: "numeric", month: "numeric", day: "numeric" }` to keep that output. * - **Compatibility:** before 1.16.0 a `"long"`/`"full"` `timeStyle` added the internal UTC anchor * as a `timeZoneName` part. `formatZonedToParts` on the value at UTC returns those parts. * - `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 PlainDateTime string * @param locale optional BCP 47 locale identifier (default: runtime default), or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns array of `{ type, value }` parts, or `[]` on invalid input * * @example formatDateTimeToParts("2024-03-15T14:30:00", "ja-JP") // [{ type: "year", value: "2024" }, { type: "literal", value: "/" }, { type: "month", value: "3" }, { type: "literal", value: "/" }, { type: "day", value: "15" }, { type: "literal", value: " " }, { type: "hour", value: "14" }, { type: "literal", value: ":" }, { type: "minute", value: "30" }, { type: "literal", value: ":" }, { type: "second", value: "00" }] * @example formatDateTimeToParts("2024-03-15T14:30:00", "de-DE") // [{ type: "day", value: "15" }, { type: "literal", value: "." }, { type: "month", value: "3" }, { type: "literal", value: "." }, { type: "year", value: "2024" }, { type: "literal", value: ", " }, { type: "hour", value: "14" }, { type: "literal", value: ":" }, { type: "minute", value: "30" }, { type: "literal", value: ":" }, { type: "second", value: "00" }] * @example formatDateTimeToParts("2024-03-15T14:30:00", "en-GB", { year: "numeric", month: "numeric", day: "numeric" }) // [{ type: "day", value: "15" }, { type: "literal", value: "/" }, { type: "month", value: "03" }, { type: "literal", value: "/" }, { type: "year", value: "2024" }] — the pre-1.16.0 default * @example formatDateTimeToParts("2024-02-29T12:00:00", "en-US", { timeStyle: "long" }) // [{ type: "hour", value: "12" }, { type: "literal", value: ":" }, { type: "minute", value: "00" }, { type: "literal", value: ":" }, { type: "second", value: "00" }, { type: "literal", value: "\u202f" }, { type: "dayPeriod", value: "PM" }] * @example formatZonedToParts("2024-02-29T12:00:00+00:00[UTC]", "en-US", { timeStyle: "long" }) // [{ type: "hour", value: "12" }, { type: "literal", value: ":" }, { type: "minute", value: "00" }, { type: "literal", value: ":" }, { type: "second", value: "00" }, { type: "literal", value: "\u202f" }, { type: "dayPeriod", value: "PM" }, { type: "literal", value: " " }, { type: "timeZoneName", value: "UTC" }] — the pre-1.16.0 parts * @example formatDateTimeToParts("invalid") // [] * @example formatDateTimeToParts("2024-02-03T14:30:45", ["fr-FR", "en-US"], { dateStyle: "medium", timeStyle: "short" }) // parts of "3 févr. 2024, 14:30" */ function formatDateTimeToParts(value, locale, options) { if (!isValidDateTime(value)) return []; try { const read = readDateTimeFormatOptions(options); checkFormatterArguments("DateTimeFormat", locale, read); const epochMilliseconds = Temporal.PlainDateTime.from(value).toZonedDateTime("UTC").epochMilliseconds; return cachedFormatter("DateTimeFormat", locale, plainDateTimeFormatOptions(read)).formatToParts(epochMilliseconds).map((p) => ({ type: p.type, value: p.value })); } catch { return []; } } export { formatDateTimeToParts };