timezone-codes
Version:
[DEPRECATED] A comprehensive, typed dataset of IANA timezone identifiers with metadata.
79 lines (76 loc) • 2.97 kB
JavaScript
;
var fs = require('fs');
var path = require('path');
var url = require('url');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
var __dirname$1 = url.fileURLToPath(new URL(".", (typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (_documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === 'SCRIPT' && _documentCurrentScript.src || new URL('cli.js', document.baseURI).href))));
var timezonesData = JSON.parse(
fs.readFileSync(path.join(__dirname$1, "data", "timezones.json"), "utf-8")
);
var timezones = Object.freeze(timezonesData.timezones);
function getTimezoneByName(name) {
if (!name) return void 0;
return timezones.find((tz) => tz.name === name);
}
function isValidTimezoneName(name) {
if (!name) return false;
return timezones.some((tz) => tz.name === name);
}
async function getCurrentTimezoneDetails(name) {
if (!name || !isValidTimezoneName(name)) return void 0;
try {
const date = /* @__PURE__ */ new Date();
const formatter = new Intl.DateTimeFormat("en-US", {
timeZone: name,
timeZoneName: "shortOffset"
});
const parts = formatter.formatToParts(date);
const offsetPart = parts.find((part) => part.type === "timeZoneName");
const offset = offsetPart?.value || "";
const shortFormatter = new Intl.DateTimeFormat("en-US", {
timeZone: name,
timeZoneName: "short"
});
const shortParts = shortFormatter.formatToParts(date);
const abbreviationPart = shortParts.find((part) => part.type === "timeZoneName");
const abbreviation = abbreviationPart?.value || "";
return {
currentUtcOffset: offset,
currentAbbreviation: abbreviation
};
} catch (error) {
return void 0;
}
}
// src/cli.ts
async function main() {
const args = process.argv.slice(2);
if (args.length === 0) {
console.log("Usage: npx timezone-codes <timezone-name>");
console.log("Example: npx timezone-codes Asia/Tokyo");
process.exit(0);
}
const timezoneName = args[0];
const timezone = getTimezoneByName(timezoneName);
if (!timezone) {
console.error(`Error: "${timezoneName}" is not a valid IANA timezone name.`);
process.exit(1);
}
console.log(`Timezone: ${timezone.name}`);
console.log(`Region: ${timezone.region}`);
console.log(`Standard UTC Offset: ${timezone.standardUtcOffset}`);
console.log(`Standard Abbreviation: ${timezone.standardAbbreviation}`);
try {
const currentDetails = await getCurrentTimezoneDetails(timezoneName);
if (currentDetails) {
console.log(`Current UTC Offset: ${currentDetails.currentUtcOffset}`);
console.log(`Current Abbreviation: ${currentDetails.currentAbbreviation}`);
}
} catch (error) {
console.error("Error getting current timezone details:", error);
}
}
main().catch(console.error);
//# sourceMappingURL=cli.js.map
//# sourceMappingURL=cli.js.map