datezone
Version:
A lightweight and comprehensive date and timeZone utility library for JavaScript.
110 lines • 3.99 kB
JavaScript
import { getUTCtoTimezoneOffsetMinutes } from "./offset.pub.js";
import { isUTC } from "./timezone.pub.js";
// 3. Implementation signature (handles both overload shapes).
export function calendarToTimestamp(arg1, monthOrTz, day, hour, minute, second, millisecond, _timeZone) {
// -----------------------------------------------------------------------
// Normalise the arguments to canonical variables.
// -----------------------------------------------------------------------
let year;
let month;
let d;
let h;
let min;
let s;
let ms;
let tz;
if (typeof arg1 === "object") {
// Overload 1: calendar object, monthOrTz is actually the timeZone.
const { year: _year = 1970, month: _month = 1, day: _day = 1, hour: _hour = 0, minute: _minute = 0, second: _second = 0, millisecond: _millisecond = 0, } = arg1;
year = _year;
month = _month;
d = _day;
h = _hour;
min = _minute;
s = _second;
ms = _millisecond;
tz = monthOrTz;
}
else {
// Overload 2: positional arguments.
year = arg1;
month = monthOrTz;
d = day;
h = hour;
min = minute;
s = second;
ms = millisecond;
tz = _timeZone ?? null;
}
// -----------------------------------------------------------------------
// Core conversion logic (identical to the previous implementation).
const utcTs = Date.UTC(year, month - 1, d, h, min, s, ms);
// Fast path: no timeZone, use local time.
if (!tz) {
return new Date(year, month - 1, d, h, min, s, ms).getTime();
}
// Fast path: if timeZone is UTC, offset is always 0.
if (isUTC(tz)) {
return utcTs;
}
// For DST zones, check if the offset changes after conversion
const offsetMin = getUTCtoTimezoneOffsetMinutes(utcTs, tz);
const tsGuess = utcTs - offsetMin * 60_000;
const offsetAfter = getUTCtoTimezoneOffsetMinutes(tsGuess, tz);
if (offsetMin === offsetAfter) {
return tsGuess;
}
// Fallback: original logic
return utcTs - offsetMin * 60_000;
}
/**
* Timestamp to calendar.
*
* @param ts - The timestamp in UTC milliseconds
* @param tz - The IANA timeZone identifier (e.g., 'America/New_York').
* @returns The calendar in the specified timeZone
* @see https://datezone.dev/docs/reference/calendar#timestamptocalendar
*/
export function timestampToCalendar(ts, tz) {
if (!tz) {
const d = new Date(ts);
return {
day: d.getDate(),
hour: d.getHours(),
millisecond: d.getMilliseconds(),
minute: d.getMinutes(),
month: d.getMonth() + 1,
second: d.getSeconds(),
year: d.getFullYear(),
};
}
// Fast path for UTC and fixed offset zones is already handled by getUTCtoTimezoneOffsetMinutes
const offsetMinutes = getUTCtoTimezoneOffsetMinutes(ts, tz);
const offsetMs = offsetMinutes * 60000;
const calendarTs = ts + offsetMs;
const d = new Date(calendarTs);
// For DST zones, check if the offset changes after conversion
const offsetAfter = getUTCtoTimezoneOffsetMinutes(calendarTs - offsetMs, tz);
if (offsetMinutes === offsetAfter) {
return {
day: d.getUTCDate(),
hour: d.getUTCHours(),
millisecond: d.getUTCMilliseconds(),
minute: d.getUTCMinutes(),
month: d.getUTCMonth() + 1,
second: d.getUTCSeconds(),
year: d.getUTCFullYear(),
};
}
// Fallback: use the original logic (which is the same as above, but ensures correctness)
return {
day: d.getUTCDate(),
hour: d.getUTCHours(),
millisecond: d.getUTCMilliseconds(),
minute: d.getUTCMinutes(),
month: d.getUTCMonth() + 1,
second: d.getUTCSeconds(),
year: d.getUTCFullYear(),
};
}
//# sourceMappingURL=calendar.pub.js.map