// gen-dates.mjs — make locale-dates: writes internal/mapa/locale/ // dates.json, the CLDR date data Mapa formats with (level-up build note // 85). rastrillo's locale packs carry CLDR's number and money data but // no dates, so Mapa carries its own, for the locales it offers. // // The data is read out of the ICU this Node links, which is CLDR (the // file records which versions), one pattern per kind of date Mapa // shows: a CLDR skeleton, given to Intl.DateTimeFormat, comes back as // its pattern in formatToParts, and the names each pattern uses are // read through that same pattern, so a language that declines a month // after a day gets the declined form there and only there. Nothing // reads Intl at run time: Go (internal/mapa/locale) and the editor // (dates.mjs) both interpret these patterns, held to each other by // internal/mapa/testdata/date_labels.json. // // Before writing, every pattern is drawn by this file's interpreter // (the one dates.mjs exports) for every day of 2024 to 2026 and every // minute pair that matters, and compared with Intl; any difference // fails the run, so the file never holds a pattern that misreads ICU. import { writeFileSync } from 'node:fs' import { dirname, join } from 'node:path' import { fileURLToPath } from 'node:url' import { formatWith } from './dates.mjs' const here = dirname(fileURLToPath(import.meta.url)) const out = join(here, '..', 'internal', 'mapa', 'locale', 'dates.json') // The locales Mapa offers, in the order Accept-Language falls back // through them after the team's own (locale.go). const offered = ['en-IE', 'en-GB', 'en-US', 'de', 'fr', 'es'] // kinds are the dates Mapa shows, as Intl options (CLDR skeletons). const kinds = { day: { weekday: 'long', day: 'numeric', month: 'long' }, dayYear: { weekday: 'long', day: 'numeric', month: 'long', year: 'numeric' }, short: { weekday: 'short', day: 'numeric', month: 'short' }, date: { day: 'numeric', month: 'long' }, dateYear: { day: 'numeric', month: 'long', year: 'numeric' }, monthYear: { month: 'long', year: 'numeric' }, month: { month: 'long' }, year: { year: 'numeric' }, // CLDR's own short time and long date: as skeletons, hour: 'numeric' // would come back unpadded (9:07) where CLDR writes 09:07. time: { timeStyle: 'short' }, stamp: { dateStyle: 'long', timeStyle: 'short' } } const at = (year, month, day, hour = 0, minute = 0) => new Date(Date.UTC(year, month - 1, day, hour, minute)) function formatter (locale, options) { return new Intl.DateTimeFormat(locale, { timeZone: 'UTC', ...options }) } // fieldOf is one formatToParts part as a pattern field. A sample whose // day, month and hour are single digits shows whether each is padded. function fieldOf (part, hourCycle) { switch (part.type) { case 'literal': return { t: 'lit', v: part.value } case 'weekday': return { t: 'weekday' } case 'year': return { t: 'year' } case 'dayPeriod': return { t: 'period' } case 'minute': return { t: 'minute' } case 'day': return { t: 'day', pad: part.value.length === 2 } case 'month': return /^[0-9]+$/u.test(part.value) ? { t: 'monthNumber', pad: part.value.length === 2 } : { t: 'month' } case 'hour': return { t: 'hour', pad: part.value.length === 2, cycle: hourCycle } } throw new Error(`no field for ${part.type}`) } function namesOf (format, type, dates) { return dates.map(date => format.formatToParts(date).find(part => part.type === type).value) } // patternOf is one kind's pattern for a locale, with the names it uses. function patternOf (locale, options) { const format = formatter(locale, options) const cycle = format.resolvedOptions().hourCycle ?? '' const parts = format.formatToParts(at(2025, 1, 5, 9, 7)).map(part => fieldOf(part, cycle)) const pattern = { parts } const types = new Set(parts.map(part => part.t)) // 4 to 10 January 2026 run Sunday to Saturday. if (types.has('weekday')) pattern.weekdays = namesOf(format, 'weekday', [4, 5, 6, 7, 8, 9, 10].map(day => at(2026, 1, day))) if (types.has('month')) pattern.months = namesOf(format, 'month', [...Array(12)].map((_, month) => at(2026, month + 1, 15))) if (types.has('period')) pattern.periods = namesOf(format, 'dayPeriod', [at(2026, 1, 1, 9), at(2026, 1, 1, 21)]) return pattern } // check draws every pattern for 2024 to 2026 and compares it with Intl. function check (locale, kind, pattern) { const format = formatter(locale, kinds[kind]) const times = kind === 'time' || kind === 'stamp' ? [[0, 0], [0, 5], [9, 7], [11, 59], [12, 0], [13, 30], [21, 9], [23, 59]] : [[0, 0]] for (let day = at(2024, 1, 1); day < at(2027, 1, 1); day = new Date(day.getTime() + 864e5)) { for (const [hour, minute] of times) { const moment = new Date(day.getTime() + (hour * 60 + minute) * 6e4) const fields = { year: moment.getUTCFullYear(), month: moment.getUTCMonth() + 1, day: moment.getUTCDate(), weekday: moment.getUTCDay(), hour, minute } const ours = formatWith(pattern, fields) // formatToParts, not format: V8's format swaps CLDR's U+202F // before AM and PM for a plain space, for old pages' sake, and // the patterns are CLDR's. const theirs = format.formatToParts(moment).map(part => part.value).join('') if (ours !== theirs) throw new Error(`${locale} ${kind}: ${JSON.stringify(ours)} where ICU writes ${JSON.stringify(theirs)}`) } } } function standalone (locale, width) { const format = formatter(locale, { weekday: width }) return [4, 5, 6, 7, 8, 9, 10].map(day => format.format(at(2026, 1, day))) } // orderOf is the numeric date's order, "dmy", "mdy" or "ymd", which is // how a typed 12/11 is read (typeddate.go). function orderOf (locale) { const parts = formatter(locale, { day: 'numeric', month: 'numeric', year: 'numeric' }) .formatToParts(at(2025, 1, 5)) return parts.filter(part => part.type !== 'literal').map(part => part.type[0]).join('') } // separatorOf is the mark between that date's numbers: "/", or "." in // German. ⌘K reads a date in numbers only with it (typeddate.go). function separatorOf (locale) { const parts = formatter(locale, { day: 'numeric', month: 'numeric', year: 'numeric' }) .formatToParts(at(2025, 1, 5)) return parts.find(part => part.type === 'literal').value.trim() } function localeData (locale) { const info = new Intl.Locale(locale).getWeekInfo() const digits = new Intl.NumberFormat(locale).resolvedOptions().numberingSystem if (digits !== 'latn') throw new Error(`${locale} writes ${digits} digits; dates.mjs writes latn only`) const patterns = {} for (const [kind, options] of Object.entries(kinds)) { patterns[kind] = patternOf(locale, options) check(locale, kind, patterns[kind]) } return { // firstDay is Go's time.Weekday: 0 is Sunday. Intl counts 1 to 7 // from Monday. firstDay: info.firstDay % 7, order: orderOf(locale), separator: separatorOf(locale), weekdays: standalone(locale, 'long'), narrow: standalone(locale, 'narrow'), patterns } } // generate is dates.json's data, from the ICU this Node links. export function generate () { return { about: 'CLDR date patterns for the locales Mapa offers, read from ICU by editor/gen-dates.mjs (make locale-dates). Weekday lists run Sunday first; firstDay is Go\'s time.Weekday. Do not edit by hand.', source: { icu: process.versions.icu, cldr: process.versions.cldr, unicode: process.versions.unicode }, offered, locales: Object.fromEntries(offered.map(locale => [locale, localeData(locale)])) } } // encode is the file's bytes. export function encode (data) { return JSON.stringify(data, null, 1) + '\n' } if (process.argv[1] === fileURLToPath(import.meta.url)) { const data = generate() writeFileSync(out, encode(data)) console.log(`wrote ${out}: ${offered.join(', ')} (CLDR ${data.source.cldr})`) }