UNPKG

@open-rlb/date-tz

Version:

A lightweight JavaScript/TypeScript date-time utility with full timezone support, custom formatting, parsing, and manipulation features.

77 lines 3.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getOffsetSeconds = getOffsetSeconds; exports.tzDiscover = tzDiscover; const canonical_link_1 = require("./canonical-link"); function getOffsetSeconds(timestamp, timezone) { if (timezone === 'UTC') return 0; const baseOffset = tzDiscover(timestamp, timezone).offset; let timestampdiscovery = timestamp - (baseOffset - 60) * 60 * 1000; const arr = []; const interval = 4; timestampdiscovery -= interval * 15 * 60 * 1000; for (const time2discover of Array.from({ length: interval * 2 }, (_, i) => timestampdiscovery + i * 15 * 60 * 1000)) { arr.push(tzDiscover(time2discover, timezone)); } const first = arr[0]; const last = arr[arr.length - 1]; const delta = last.offset - first.offset; if (delta === 0) return first.offset * 60; if (delta > 0) { return first.offset * 60; } if (delta < 0) { return last.offset * 60; } } function tzDiscover(timestamp, timezone) { const formatterTZS = new Intl.DateTimeFormat('en-US', { timeZone: timezone, timeZoneName: 'longOffset', hour12: false, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }); const formatterTZL = new Intl.DateTimeFormat('en-US', { timeZone: timezone, timeZoneName: 'long', hour12: false, year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', }); const partsTZS = formatterTZS.formatToParts(timestamp); const partsTZL = formatterTZL.formatToParts(timestamp); const longName = getTimeFormatPart(partsTZL, 'timeZoneName')?.toLowerCase(); const isDst = longName.includes('summer') || longName?.includes('daylight'); const _timezone = getTimeFormatPart(partsTZS, 'timeZoneName'); if (_timezone === 'UTC' || _timezone === 'GMT') return { isDst: false, offset: 0 }; const match = _timezone.match(/^(?:GMT|UTC)?([+-])(\d{1,2})(?::(\d{2}))?$/i); if (!match) { throw new Error(`Unexpected timeZoneName format: ${_timezone}`); } const sign = match[1] === '+' ? 1 : -1; const hours = parseInt(match[2], 10); const mins = match[3] ? parseInt(match[3], 10) : 0; return { isDst, offset: sign * (hours * 60 + mins), hh: `${getTimeFormatPart(partsTZS, 'hour')}:${getTimeFormatPart(partsTZS, 'minute')}` }; } function getTimeFormatPart(parts, type) { const part = parts.find(p => p.type === type); if (!part) { throw new Error(`Missing part ${type}`); } return part.value; } ; function supportedTimeZones() { const canonical = Array.from(new Set([ ...canonical_link_1.etc, ...Intl.supportedValuesOf('timeZone') ])); return canonical; } function fallbackTimeZone(timezone) { if (supportedTimeZones().includes(timezone)) { return timezone; } else if (Object.keys(canonical_link_1.canonicalLink).includes(timezone) && supportedTimeZones().includes(canonical_link_1.canonicalLink[timezone])) { return canonical_link_1.canonicalLink[timezone]; } else if (Object.values(canonical_link_1.canonicalLink).includes(timezone) && supportedTimeZones().includes(Object.entries(canonical_link_1.canonicalLink).find(([k, v]) => v === timezone)?.[0])) { return Object.entries(canonical_link_1.canonicalLink).find(([k, v]) => v === timezone)?.[0]; } else { throw new Error(`Unsupported time zone: ${timezone}`); } } //# sourceMappingURL=helpers.js.map