#!/usr/bin/env node // Writes src/data/homeCountries.js: each time zone's country, and where the // opening stops over that country. // // node scripts/build-home-countries.mjs [zone.tab] [tzdata.zi] // // Zones come from tzdata's zone.tab (macOS and most Linux systems ship it and // tzdata.zi at /usr/share/zoneinfo), with the names ICU gives them // (Asia/Calcutta for Asia/Kolkata) and the older names tzdata.zi links to // them (Asia/Choibalsan): a browser may give any of them. A country's point // lies inside its largest part in the Natural Earth countries pack (the // contiguous United States, mainland France), or is its first zone's place // where the pack has no such country. import { readFileSync, writeFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { countryAnchor, countryByCode, } from '../server/providers/news/areas.js'; const root = new URL('../', import.meta.url); const zoneTab = process.argv[2] ?? '/usr/share/zoneinfo/zone.tab'; const zoneLinks = process.argv[3] ?? '/usr/share/zoneinfo/tzdata.zi'; const out = new URL('src/data/homeCountries.js', root); const COUNTRY = /^[A-Z]{2}$/; const ZONE = /^[A-Za-z0-9_+-]+(?:\/[A-Za-z0-9_+-]+)*$/; const round = (value) => Math.round(value * 10) / 10; /** ISO 6709 `±DDMM[SS]±DDDMM[SS]` → [lat, lon]. */ function iso6709(text) { const [, lat, lon] = text.match( /^([+-]\d{2}[0-5]\d(?:[0-5]\d)?)([+-]\d{3}[0-5]\d(?:[0-5]\d)?)$/, ) ?? []; if (!lat) throw new Error(`zone.tab: bad place ${text}`); const part = (s, degreeDigits) => { const sign = s[0] === '-' ? -1 : 1; const d = s.slice(1); const deg = Number(d.slice(0, degreeDigits)); const min = Number(d.slice(degreeDigits, degreeDigits + 2)); const sec = Number(d.slice(degreeDigits + 2) || 0); return sign * (deg + min / 60 + sec / 3600); }; const place = [part(lat, 2), part(lon, 3)]; if (Math.abs(place[0]) > 90 || Math.abs(place[1]) > 180) throw new Error(`zone.tab: place out of range ${text}`); return place; } const zones = new Map(); const centres = new Map(); const icuName = (zone) => { try { return new Intl.DateTimeFormat('en', { timeZone: zone }).resolvedOptions() .timeZone; } catch { return null; } }; for (const line of readFileSync(zoneTab, 'utf8').split('\n')) { if (!line || line.startsWith('#')) continue; const [country, place, zone] = line.split('\t'); if (!COUNTRY.test(country) || !zone || !ZONE.test(zone)) throw new Error(`zone.tab: bad line ${JSON.stringify(line)}`); const coordinates = iso6709(place); zones.set(zone, country); const icu = icuName(zone); if (icu && icu !== zone && !zones.has(icu)) zones.set(icu, country); if (centres.has(country)) continue; const entry = countryByCode(country); const point = entry ? countryAnchor(entry) : null; centres.set( country, point ? [round(point.lat), round(point.lon)] : coordinates.map(round), ); } // Older Area/Place names (Asia/Choibalsan, Europe/Uzhgorod), from tzdata.zi's // `L ` links, each with the country of the zone ICU reads // it as: a link can lead to another country's zone with the same clocks // (Africa/Timbuktu to Abidjan). Bare names (CET, Japan) are left out: some // are clocks several countries keep. for (const line of readFileSync(zoneLinks, 'utf8').split('\n')) { const [kind, , name] = line.split(' '); if (kind !== 'L') continue; if (!name || !ZONE.test(name)) throw new Error(`tzdata.zi: bad link ${line}`); if (!name.includes('/') || zones.has(name)) continue; const country = zones.get(icuName(name)); if (!country) continue; zones.set(name, country); } const sortedEntries = (map) => [...map].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)); const lines = [ '// Generated by scripts/build-home-countries.mjs. Do not edit by hand.', '// Sources: tzdata zone.tab and tzdata.zi (public domain) and the Natural', '// Earth countries pack in src/data/local_data/natural_earth (public domain).', '', "/** ISO 3166 country → [lat, lon]: in its largest part, else its first zone's place. */", 'export const COUNTRY_CENTRES = {', ...sortedEntries(centres).map( ([country, [lat, lon]]) => ` ${country}: [${lat}, ${lon}],`, ), '};', '', '/** Time zone (tzdata, ICU and older names) → ISO 3166 country. */', 'export const ZONE_COUNTRIES = {', ...sortedEntries(zones).map( ([zone, country]) => ` '${zone}': '${country}',`, ), '};', '', ]; writeFileSync(out, lines.join('\n')); console.log( `${fileURLToPath(out)}: ${centres.size} countries, ${zones.size} zones`, );