A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!
TypeScript agent-skillsaidsh-pluginhacktoberfestjavascriptmcp-serverreactresume-builderself-hostedtypescript
// Generates packages/utils/src/region-zones.ts: IANA time zone name -> ISO 3166-1 alpha-2 country code.
// Run with: node tooling/locales/generate-region-zones.mjs
// Reads IANA tzdata `zone.tab` (zone -> country) and `backward` (legacy link name -> canonical zone).
// Bump TZDATA_VERSION to refresh the table, then commit the regenerated file.
import { writeFile } from "node:fs/promises";
import { fileURLToPath } from "node:url";
const TZDATA_VERSION = "2026e";
const SOURCE_BASE = `https://raw.githubusercontent.com/eggert/tz/${TZDATA_VERSION}`;
const RELEASE_URL = `https://data.iana.org/time-zones/releases/tzdata${TZDATA_VERSION}.tar.gz`;
const OUTPUT = fileURLToPath(new URL("../../packages/utils/src/region-zones.ts", import.meta.url));
const fetchText = async (file) => {
const response = await fetch(`${SOURCE_BASE}/${file}`);
if (!response.ok) throw new Error(`Failed to fetch ${file}: ${response.status}`);
return response.text();
};
const dataLines = (text) => text.split("\n").filter((line) => line.trim() !== "" && !line.startsWith("#"));
const [zoneTab, backward] = await Promise.all([fetchText("zone.tab"), fetchText("backward")]);
const zoneCountry = new Map();
for (const line of dataLines(zoneTab)) {
const [country, , zone] = line.split("\t");
if (/^[A-Z]{2}$/.test(country) && zone) zoneCountry.set(zone, country);
}
// `backward` lines look like "Link<TAB>target<TAB>legacy-name # comment". When the comment says
// "#= Some/Zone", that zone is the one the legacy name really meant, because tzdata has since merged
// it into a shared canonical zone (Iceland -> Africa/Abidjan would wrongly give CI), so prefer it.
// Resolve repeatedly so a link to another link still reaches a zone that has a country.
const links = dataLines(backward)
.map((line) => {
const [data, comment = ""] = line.split("#");
const [kind, target, name] = data.trim().split(/\s+/);
const intended = comment.match(/^=\s*(\S+)/)?.[1];
return { kind, target, name, intended };
})
.filter(({ kind, target, name }) => kind === "Link" && target && name);
// Standard-time abbreviations name a time rule, not a country, so tzdata's backing zone says nothing.
const REGIONLESS = new Set(["CET", "EET", "EST", "HST", "MET", "MST", "WET"]);
// These aliases lack #= hints in backward. Their locations are preserved in tzdata's backzone file.
const LOCATION_OVERRIDES = new Map([
["America/Coral_Harbour", "CA"],
["Pacific/Yap", "FM"],
]);
for (let resolved = true; resolved;) {
resolved = false;
for (const { target, name, intended } of links) {
if (REGIONLESS.has(name) || zoneCountry.has(name)) continue;
const country = LOCATION_OVERRIDES.get(name) ?? zoneCountry.get(intended) ?? zoneCountry.get(target);
if (country) {
zoneCountry.set(name, country);
resolved = true;
}
}
}
const entries = [...zoneCountry].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
// Bare identifier keys where valid, so the output already matches the repo formatter.
const key = (zone) => (/^[A-Za-z_$][\w$]*$/.test(zone) ? zone : JSON.stringify(zone));
const body = entries.map(([zone, country]) => `\t${key(zone)}: ${JSON.stringify(country)},`).join("\n");
await writeFile(
OUTPUT,
`// Generated by tooling/locales/generate-region-zones.mjs. Do not edit by hand.
// Source: IANA tzdata ${TZDATA_VERSION} (${RELEASE_URL}),
// files zone.tab and backward, read from ${SOURCE_BASE}/.
// Legacy link names from \`backward\` are resolved to the country of their canonical zone.
export const ZONE_COUNTRY: Record<string, string> = {
${body}
};
`,
);
console.log(`Wrote ${entries.length} zones from tzdata ${TZDATA_VERSION} to ${OUTPUT}`);