// dates.mjs — dates in the reader's locale (level-up build note 85). The // server picks the locale (rastrillo's middleware, Accept-Language, then // MAPA_LOCALE) and puts its CLDR patterns in the editor's island as // "dates"; page.mjs hands them to useDates before anything is drawn. // formatWith is the same interpreter internal/mapa/locale's format.go // is, held to it by internal/mapa/testdata/date_labels.json, so a chip // the editor labels reads exactly as the server's. Intl is never asked: // its patterns are the browser's ICU, which need not be the CLDR Mapa // ships (gen-dates.mjs reads that once, at build time). let current = null // useDates sets the locale's data: the island's "dates". export function useDates (data) { current = data ?? null } export function datesInUse () { return current } const pad = (value, wanted) => wanted ? String(value).padStart(2, '0') : String(value) function hourOf (hour, cycle) { switch (cycle) { case 'h12': return hour % 12 === 0 ? 12 : hour % 12 case 'h11': return hour % 12 case 'h24': return hour === 0 ? 24 : hour } return hour } // formatWith draws one pattern for fields: year, month (1 to 12), day, // weekday (0 is Sunday), hour and minute. export function formatWith (pattern, fields) { let out = '' for (const part of pattern.parts) { switch (part.t) { case 'lit': out += part.v; break case 'weekday': out += pattern.weekdays[fields.weekday]; break case 'month': out += pattern.months[fields.month - 1]; break case 'monthNumber': out += pad(fields.month, part.pad); break case 'day': out += pad(fields.day, part.pad); break case 'year': out += String(fields.year); break case 'hour': out += pad(hourOf(fields.hour, part.cycle), part.pad); break case 'minute': out += pad(fields.minute, true); break case 'period': out += pattern.periods[fields.hour < 12 ? 0 : 1]; break } } return out } // fieldsOf is an ISO date's fields. export function fieldsOf (iso) { const [year, month, day] = iso.split('-').map(Number) const date = new Date(0) date.setUTCFullYear(year, month - 1, day) return { year, month, day, weekday: date.getUTCDay(), hour: 0, minute: 0 } } // format is one kind of date (dates.json's kinds) for an ISO date, in // data's locale; the ISO date itself when there is no data. export function format (kind, iso, data = current) { const pattern = data?.patterns?.[kind] return pattern ? formatWith(pattern, fieldsOf(iso)) : iso } // dayLabel is locale.Locale.Day: "Thursday 12 November" in en-GB, with // the year when it is not thisYear. export function dayLabel (iso, thisYear = new Date().getFullYear(), data = current) { return format(Number(iso.slice(0, 4)) === thisYear ? 'day' : 'dayYear', iso, data) } // monthTitle is the month calendar's title: "November 2026". export function monthTitle (iso, data = current) { return format('monthYear', `${iso.slice(0, 7)}-01`, data) } // firstDay is the locale's first day of the week, 0 for Sunday; Monday // when there is no data. export function firstDay (data = current) { return data?.firstDay ?? 1 } // weekHeads are the week's columns from its first day: the day's name // and its narrow letter. export function weekHeads (data = current) { const english = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] const names = data?.weekdays ?? english const letters = data?.narrow ?? english.map(name => name[0]) return Array.from({ length: 7 }, (_, n) => { const day = (firstDay(data) + n) % 7 return { name: names[day], letter: letters[day] } }) } // order is how a numeric date is read: "dmy", "mdy" or "ymd". export function order (data = current) { return data?.order ?? 'dmy' }