Skip to content

Instantly share code, notes, and snippets.

@eyedean
Last active November 14, 2021 20:16
Show Gist options
  • Save eyedean/18f2611624c8f9624f8bc6a3b74e9dba to your computer and use it in GitHub Desktop.
Save eyedean/18f2611624c8f9624f8bc6a3b74e9dba to your computer and use it in GitHub Desktop.
Generate IANA Time Zone Names, in a JSON array, from timezones.json package
// Creating a JSON file, containing IANA Time Zone Names from timezones.json package.
// See this stackoverflow question: https://stackoverflow.com/a/69966796/
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Short version:
// const namesSet = new Set();
// require("timezones.json").forEach((tzDefinition) => tzDefinition.utc.forEach((tzName) => namesSet.add(tzName)));
// console.log(JSON.stringify([...namesSet].sort()));
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Long version:
const timezones = require("timezones.json");
function printSortedIANATimeZonesList(prettyPrint = false) {
const namesSet = new Set();
timezones
.forEach((tzDefinition) => tzDefinition.utc
.forEach((tzName) => namesSet.add(tzName)));
const sortedNames = [...namesSet].sort();
if (prettyPrint) {
let pkgVersion = "";
try { pkgVersion = `@${require("./node_modules/timezones.json/package.json").version}`; } catch(e) {}
const isoTimeNow = new Date().toISOString();
console.log(`// ${sortedNames.length} entries extracted from "timezones.json${pkgVersion}" package at ${isoTimeNow}`);
console.log("[");
// printing out line-by-line that terminal doesn't eat it
sortedNames.forEach((name, i) => console.log(`\t"${name}"${i === sortedNames.length ? "" : ","}`));
console.log("]");
} else {
console.log(JSON.stringify(sortedNames));
}
}
printSortedIANATimeZonesList(true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment