#!/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 [path/to/zone.tab] // // Zones come from tzdata's zone.tab (macOS and most Linux systems ship it at // /usr/share/zoneinfo/zone.tab), with the older names ICU still reports for // some of them (Asia/Calcutta for Asia/Kolkata): a browser may give either. // A country's point is the centroid of its largest part in the Natural Earth // countries pack (the contiguous United States, mainland France), or the // zone's own place where the pack has no such country. import { readFileSync, writeFileSync } from 'node:fs'; import { fileURLToPath } from 'node:url'; import { geometryOf } from '../src/data/adminBoundaries.js'; const root = new URL('../', import.meta.url); const zoneTab = process.argv[2] ?? '/usr/share/zoneinfo/zone.tab'; const pack = JSON.parse( readFileSync( new URL('src/data/local_data/natural_earth/countries.json', root), 'utf8', ), ); const out = new URL('src/data/homeCountries.js', root); 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+)([+-]\d+)$/); 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); }; return [part(lat, 2), part(lon, 3)]; } const largest = new Map(); for (const feature of pack.features) { if (feature.type !== 'Country' || !/^[A-Z]{2}$/.test(feature.iso2)) continue; const geometry = geometryOf({ feature, decimals: pack.meta.decimals }); const held = largest.get(feature.iso2); if (!held || geometry.areaKm2 > held.areaKm2) largest.set(feature.iso2, geometry); } 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'); 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 label = largest.get(country)?.label; centres.set( country, label ? [round(label.lat), round(label.lon)] : iso6709(place).map(round), ); } 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 (public domain) and the Natural Earth countries', '// pack in src/data/local_data/natural_earth (public domain).', '', '/** ISO 3166 country → [lat, lon]: the centroid of its largest part. */', 'export const COUNTRY_CENTRES = {', ...sortedEntries(centres).map( ([country, [lat, lon]]) => ` ${country}: [${lat}, ${lon}],`, ), '};', '', '/** Time zone (tzdata and ICU 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`, );