timezone-codes
Version:
[DEPRECATED] A comprehensive, typed dataset of IANA timezone identifiers with metadata.
57 lines (55 loc) • 2 kB
JavaScript
import { readFileSync } from 'fs';
import { join } from 'path';
import { fileURLToPath } from 'url';
// src/index.ts
var __dirname = fileURLToPath(new URL(".", import.meta.url));
var timezonesData = JSON.parse(
readFileSync(join(__dirname, "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;
}
}
export { getCurrentTimezoneDetails, getStandardUtcOffset, getTimezoneByName, getTimezonesByRegion, isValidTimezoneName, timezones };
//# sourceMappingURL=index.mjs.map
//# sourceMappingURL=index.mjs.map