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.

427 lines (426 loc) • 7.01 kB
// src/internal/temporalCompat/rangeFieldRepros.ts const A = 17157795e5; const D = 1715964e6; const B = 17157912e5; const C = 1715784e6; const M = 17189016e5; const Y = 17206221e5; const Y2 = 17521581e5; const G = -653064243e5; const n = "numeric"; const ymdw = { year: n, month: n, day: n, weekday: "short" }; const hmDayPeriod = { hour: n, minute: n, dayPeriod: "long" }; const eraDate = { era: "short", year: n, month: n, day: n }; /** * One case per cause of the sweep (794,400 formatters, Node 22.22.2, 24.21.0 and 26.12.1), each the * shortest range that shows it. The gate (`isRangeFieldCompatNeeded`) stays on while any one of * them is broken, as the causes are fixed one at a time upstream. * * - chinese drops `relatedYear` from a range a year apart; * - `hi` drops a `B` day period, `ta` prints one for two (ICU-23622), `bgc` writes `PM` where the * single writes the long period (ICU-23625); * - hebrew prints the end's month with the wrong value (`fur`); * - malformed CLDR patterns (CLDR-19912): `yo` prints the end's weekday twice and a year on the * wrong side, `sw` drops the start's month, `rw` repeats a day, `ta` shortens a zone name, `hy` and * `az` drop the end's year or era in a `G` pattern, `it` and `brx` run two numbers together, * `zh` `iso8601` runs a month and a day together (CLDR-19913); * - ICU takes the `y` pattern when only the era differs (ICU-23623): `scn` drops the start's era, * `ko` japanese writes `S` and `R` for the eras; * - `ja` writes `0時` for the hour 12 under `hourCycle: "h12"` (ICU-23624). */ const rangeFieldCases = [ { name: "chineseYearDropsRelatedYear", locale: "en-u-ca-chinese", options: { year: n, month: "long" }, from: Y, to: Y2, fields: [[ "2024", 1, 1 ], [ "2025", 1, 1 ]] }, { name: "hiDropsDayPeriod", locale: "hi", options: hmDayPeriod, from: A, to: B, fields: [[ "दोपहर", 1, 1 ], [ "शाम", 1, 1 ]] }, { name: "furHebrewEndMonthValue", locale: "fur-u-ca-hebrew", options: { year: n, month: "narrow" }, from: Y, to: Y2, fields: [ [ "11", 1, 2 ], [ "10", 0, 0 ], [ "5784", 1, 1 ], [ "5785", 1, 1 ] ] }, { name: "yoWeekdayTwice", locale: "yo", options: ymdw, from: Y, to: 17207085e5, fields: [ [ "Ọjọ́rú", 1, 1 ], [ "Ọjọ́bọ", 1, 1 ], [ "10", 1, 1 ], [ "11", 1, 1 ], [ "07", 1, 2 ], [ "2024", 1, 2 ] ] }, { name: "yoYearOnWrongSide", locale: "yo", options: { year: n, month: "long" }, from: Y, to: Y2, fields: [[ "2024", 1, 1, 0 ], [ "2025", 1, 1, 1 ]] }, { name: "taDropsStartDayPeriod", locale: "ta", options: hmDayPeriod, from: A, to: B, fields: [[ "மதியம்", 1, 1 ], [ "மாலை", 1, 1 ]] }, { name: "bgcLongDayPeriodShort", locale: "bgc", options: hmDayPeriod, from: A, to: C, fields: [[ "सांझ", 1, 2 ]] }, { name: "swDropsStartMonth", locale: "sw", options: { year: n, month: "short", day: n }, from: A, to: M, fields: [[ "Mei", 1, 1 ], [ "Jun", 1, 1 ]] }, { name: "rwRepeatsDay", locale: "rw", options: ymdw, from: A, to: D, fields: [[ "15", 1, 1 ], [ "17", 1, 1 ]] }, { name: "taShortensZoneName", locale: "ta", options: { hour: n, minute: n, timeZoneName: "longGeneric" }, from: A, to: B, fields: [[ "GMT+00:00", 1, 2 ]] }, { name: "hyDropsEndYear", locale: "hy", options: { year: n, month: "short", day: n, weekday: "short" }, from: G, to: Y2, fields: [[ "101", 1, 1 ], [ "2025", 1, 1 ]] }, { name: "azDropsEndEra", locale: "az", options: { era: "long", year: n, month: "long", day: n, weekday: "long" }, from: G, to: Y2, fields: [[ "eramızdan", 1, 1 ], [ "yeni", 1, 1 ]] }, { name: "itRunsDayAndMonthTogether", locale: "it", options: { ...eraDate, weekday: "short" }, from: G, to: Y2, fields: [[ "1007", 0, 0 ], [ "gio", 1, 1 ]] }, { name: "brxRunsMonthAndYearTogether", locale: "brx", options: { ...eraDate, weekday: "short" }, from: A, to: 17636568e5, fields: [[ "112025", 0, 0 ]] }, { name: "zhIsoRunsMonthAndDayTogether", locale: "zh-u-ca-iso8601", options: { year: n, month: "narrow", day: n }, from: 17157837e5, to: 17504301e5, fields: [[ "515", 0, 0 ]] }, { name: "scnEraOnlyDropsStartEra", locale: "scn", options: { era: "short", year: n, month: "long" }, from: G, to: Y2, fields: [[ "p.C.", 1, 1 ], [ "d.C.", 1, 1 ]] }, { name: "koJapaneseEraNames", locale: "ko-u-ca-japanese", options: { dateStyle: "long" }, from: -1776723e5, to: 17636493e5, fields: [[ "쇼와", 1, 1 ], [ "레이와", 1, 1 ]] }, { name: "jaTwelveWritesZero", locale: "ja", options: { hour: n, minute: n, hourCycle: "h12" }, from: 17157318e5, to: 17157408e5, fields: [[ "12", 1, 1 ]] } ]; /** * `"complete"` when the engine's range holds every field text of the case the stated number of times, else the range text itself * (so a canary shows what the engine printed). A locale the runtime has no data for formats in the * fallback locale and cannot show a defect of its own data, so it answers `"complete"` * (ECMA-402 ResolveLocale falls back to the default locale). * * @param locale the locale * @param options the formatter options (UTC is added) * @param from range start, epoch milliseconds * @param to range end, epoch milliseconds * @returns `"complete"`, or the engine's range text * * @example rangeFieldProblem("yo", { year: "numeric", month: "numeric", day: "numeric", weekday: "short" }, 1720622100000, 1720708500000) // "Ọjọ́rú, 10-07-2024 – Ọjọ́bọ 11-07-2024, Ọjọ́bọ" where the engine repeats a weekday */ function rangeFieldProblem(locale, options, from, to) { if (Intl.DateTimeFormat.supportedLocalesOf([locale]).length === 0) return "complete"; const range = new Intl.DateTimeFormat(locale, { ...options, timeZone: "UTC" }).formatRange(from, to); const own = rangeFieldCases.find((c) => c.locale === locale && c.from === from && c.to === to && JSON.stringify(c.options) === JSON.stringify(options)); const cut = range.search(/[–—−~~]|\s-\s/u); return own?.fields.every(([text, min, max, side]) => { const count = range.split(text).length - 1; return count >= min && count <= max && (side === void 0 || cut < 0 || (side ? range.slice(cut) : range.slice(0, cut)).includes(text)); }) ? "complete" : range; } export { rangeFieldCases, rangeFieldProblem };