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.

71 lines • 6.17 kB
import { DateTimeFormatOptions } from "../../types/datetime-format-options.js"; import "../../types/index.js"; /** * Return the locale-formatted parts of a ZonedDateTime. * * - 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"`, * `"timeZoneName"`, `"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. This holds on a runtime that prints the observatory's Chinese or Dangi dates * (see the next item). * - 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 parts. A format with only a time or weekday * field always returns its parts. * - 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"` and `timeZoneName` to * `"short"` unless given (ECMA-402 as amended by Temporal). The parts, joined, * are the text `formatZonedDateTime` returns for the same arguments before its * whitespace normalisation. * - A ZonedDateTime is formatted in its own zone: a `timeZone` option returns * `[]`, as `ZonedDateTime#toLocaleString` throws a TypeError for one. * - 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, with * no time zone name. Pass `{ year: "numeric", month: "numeric", day: "numeric" }` to keep that output. * Before 1.16.0 a `timeZone` option re-rendered the instant in that zone; `formatUtc` with its * `timeZone` option gives that text. * - `options` null returns `[]`, as ECMA-402's CoerceOptionsToObject rejects it (a string or number * options value formats with the defaults, as `Intl.DateTimeFormat` does). * * @param value zoned ISO 8601 datetime string * @param locale optional locale tag (e.g. "en-GB", "ja-JP"), or a preference list of tags (ECMA-402) * @param options optional Intl.DateTimeFormatOptions * @returns array of `{ type, value }` parts, or `[]` on invalid input * * @example formatZonedToParts("2024-03-15T14:30:00.000+00:00[Europe/London]", "en-GB") // [{ type: "day", value: "15" }, { type: "literal", value: "/" }, { type: "month", value: "03" }, { 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" }, { type: "literal", value: " " }, { type: "timeZoneName", value: "GMT" }] * @example formatZonedToParts("2024-03-15T14:30:00.000+05:30[Asia/Kolkata]", "en-US", { timeZoneName: "longOffset" }) // [{ type: "month", value: "3" }, { type: "literal", value: "/" }, { type: "day", value: "15" }, { type: "literal", value: "/" }, { type: "year", value: "2024" }, { type: "literal", value: ", " }, { type: "hour", value: "2" }, { type: "literal", value: ":" }, { type: "minute", value: "30" }, { type: "literal", value: ":" }, { type: "second", value: "00" }, { type: "literal", value: "\u202f" }, { type: "dayPeriod", value: "PM" }, { type: "literal", value: " " }, { type: "timeZoneName", value: "GMT+05:30" }] * @example formatZonedToParts("2024-03-15T14:30:00.000+00:00[Europe/London]", "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 formatZonedToParts("2024-03-15T14:30:00.000+00:00[Europe/London]", "en-GB", { timeZone: "Asia/Tokyo" }) // [] — a ZonedDateTime keeps its own zone * @example formatZonedToParts("invalid", "de-DE") // [] * @example formatZonedToParts("2024-02-03T14:30:45+01:00[Europe/Paris]", ["fr-FR", "en-US"], { dateStyle: "medium", timeStyle: "short" }) // parts of "3 févr. 2024, 14:30" * @example formatZonedToParts("2024-02-03T14:30:00+02:00[Africa/Johannesburg]", "ar-EG", null as never) // [] (null options, as ECMA-402 rejects them) */ export declare function formatZonedToParts(value: string, locale?: string | string[], options?: DateTimeFormatOptions): Array<{ /** * The kind of part, named as ECMA-402 `Intl.DateTimeFormat.prototype.formatToParts` names it: a * field such as `"year"`, `"month"` or `"day"`, or `"literal"` for the separator text between * fields. */ type: string; /** * The text of the part in the requested locale, exactly as the formatter wrote it. Joining every * `value` in array order gives the whole formatted text. */ value: string; }>;