timezone-codes
Version:
[DEPRECATED] A comprehensive, typed dataset of IANA timezone identifiers with metadata.
65 lines (62 loc) • 2.5 kB
JavaScript
;
var fs = require('fs');
var path = require('path');
var url = require('url');
var _documentCurrentScript = typeof document !== 'undefined' ? document.currentScript : null;
// src/index.ts
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('index.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 getTimezonesByRegion(region) {
if (!region) return [];
const normalizedRegion = region.toLowerCase();
return timezones.filter((tz) => tz.region.toLowerCase() === normalizedRegion);
}
function getStandardUtcOffset(name) {
if (!name) return void 0;
return getTimezoneByName(name)?.standardUtcOffset;
}
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;
}
}
exports.getCurrentTimezoneDetails = getCurrentTimezoneDetails;
exports.getStandardUtcOffset = getStandardUtcOffset;
exports.getTimezoneByName = getTimezoneByName;
exports.getTimezonesByRegion = getTimezonesByRegion;
exports.isValidTimezoneName = isValidTimezoneName;
exports.timezones = timezones;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map