@stephenhebert/datetime-iso8601
Version:
A small library to simplify parsing and timezone conversion of computer date formats
214 lines (213 loc) • 6.67 kB
JavaScript
var $ = (a) => {
throw TypeError(a);
};
var O = (a, t, e) => t.has(a) || $("Cannot " + e);
var E = (a, t, e) => (O(a, t, "read from private field"), e ? e.call(a) : t.get(a)), g = (a, t, e) => t.has(a) ? $("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(a) : t.set(a, e), l = (a, t, e, r) => (O(a, t, "write to private field"), r ? r.call(a, e) : t.set(a, e), e), s = (a, t, e) => (O(a, t, "access private method"), e);
const k = "(?<year>\\d{4})-(?<month>\\d{2})-(?<day>\\d{2})", K = "(?<hour>\\d{2}):(?<minute>\\d{2}):(?<second>\\d{2})", S = "(?<tzOffsetHour>[+-]\\d{2}):(?<tzOffsetMinute>\\d{2})", U = `^${k}T${K}${S}$`, j = new RegExp(U), v = {
"-12:00": "Etc/GMT+12",
"-11:00": "Etc/GMT+11",
"-10:00": "Etc/GMT+10",
"-09:30": "Pacific/Marquesas",
"-09:00": "Etc/GMT+9",
"-08:00": "Etc/GMT+8",
"-07:00": "Etc/GMT+7",
"-06:00": "Etc/GMT+6",
"-05:00": "Etc/GMT+5",
"-04:00": "Etc/GMT+4",
"-03:00": "Etc/GMT+3",
"-02:00": "Etc/GMT+2",
"-01:00": "Etc/GMT+1",
"+00:00": "Etc/GMT",
"+01:00": "Etc/GMT-1",
"+02:00": "Etc/GMT-2",
"+03:00": "Etc/GMT-3",
"+03:30": "Asia/Tehran",
"+04:00": "Etc/GMT-4",
"+04:30": "Asia/Kabul",
"+05:00": "Etc/GMT-5",
"+05:30": "Asia/Kolkata",
"+05:45": "Asia/Kathmandu",
"+06:00": "Etc/GMT-6",
"+06:30": "Asia/Yangon",
"+07:00": "Etc/GMT-7",
"+08:00": "Etc/GMT-8",
"+08:45": "Australia/Eucla",
"+09:00": "Etc/GMT-9",
"+09:30": "Australia/Darwin",
"+10:00": "Etc/GMT-10",
"+11:00": "Etc/GMT-11",
"+12:00": "Etc/GMT-12",
"+13:00": "Etc/GMT-13",
"+14:00": "Etc/GMT-14"
};
var h, c, f, R, i, b, p, M, z, D, y, I, x, H;
const d = class d {
/**
* Constructs a new DateTimeIso8601 instance.
* @param isoString An ISO8601 date-time string.
* @throws {Error} If the string cannot be parsed.
*/
constructor(t) {
g(this, i);
g(this, h);
if (!j.test(t)) throw new Error("Unable to parse time string");
l(this, h, t);
}
/**
* Converts the current date-time to the specified time zone offset.
* @param tzOffset The time zone offset as a key of the IANA mapping (e.g., '+00:00').
* @returns The current instance for chaining.
* @throws {Error} If the tzOffset is not supported.
*/
tz(t) {
return l(this, h, s(this, i, x).call(this, t)), this;
}
/**
* Gets a parsed component or all components of the current date-time.
* @param key Optional. The specific component key to retrieve.
* @returns The requested component value or an object with all components.
*/
get(t) {
const e = s(this, i, y).call(this), r = {
...e,
date: s(this, i, b).call(this, e),
time: s(this, i, p).call(this, e),
tzOffset: s(this, i, M).call(this, e),
weekday: s(this, i, D).call(this),
weekdayIndex: s(this, i, z).call(this)
};
return t ? r[t] : r;
}
/**
* Updates the current date-time with new components.
* @param newComponents Partial date-time components to update.
* @param relative If true, adds values to the current components; otherwise, replaces them.
* @returns The current instance for chaining.
*/
update(t, e = !1) {
const r = s(this, i, y).call(this), n = e ? Object.fromEntries(
Object.entries(r).map(([
o,
u
]) => [
o,
u + (t[o] ?? 0)
])
) : { ...r, ...t };
return l(this, h, s(this, i, H).call(this, n)), this;
}
/**
* Returns the ISO8601 string representation of the current date-time.
* @returns The ISO8601 string.
*/
toString() {
return E(this, h);
}
};
h = new WeakMap(), c = new WeakSet(), f = function(t, e) {
return t.toString().padStart(e, "0");
}, R = function(t) {
var n;
const { sign: e = "+", value: r = "0" } = ((n = /(?<sign>[+-])?(?<value>\d+)/.exec(t.toString())) == null ? void 0 : n.groups) ?? {};
return `${e}${r.toString().padStart(2, "0")}`;
}, i = new WeakSet(), b = function({ year: t, month: e, day: r }) {
var n, o, u;
return [
s(n = d, c, f).call(n, t, 4),
s(o = d, c, f).call(o, e, 2),
s(u = d, c, f).call(u, r, 2)
].join("-");
}, p = function({ hour: t, minute: e, second: r }) {
var n, o, u;
return [
s(n = d, c, f).call(n, t, 2),
s(o = d, c, f).call(o, e, 2),
s(u = d, c, f).call(u, r, 2)
].join(":");
}, M = function({ tzOffsetHour: t, tzOffsetMinute: e }) {
var r, n;
return [
s(r = d, c, R).call(r, t),
s(n = d, c, f).call(n, e, 2)
].join(":");
}, z = function(t = E(this, h)) {
const { year: e, month: r, day: n } = s(this, i, y).call(this, t);
return new Date(e, r - 1, n).getDay();
}, D = function(t = E(this, h)) {
return [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday"
][s(this, i, z).call(this, t)];
}, y = function(t = E(this, h)) {
const e = j.exec(t);
if (!(e != null && e.groups)) throw new Error("Invalid ISO string");
const {
year: r,
month: n,
day: o,
hour: u,
minute: G,
second: m,
tzOffsetHour: w,
tzOffsetMinute: T
} = e.groups;
return {
year: +r,
month: +n,
day: +o,
hour: +u,
minute: +G,
second: +m,
tzOffsetHour: +w,
tzOffsetMinute: +T
};
}, I = function(t) {
const e = t.date ?? s(this, i, b).call(this, t), r = t.time ?? s(this, i, p).call(this, t), n = t.tzOffset ?? s(this, i, M).call(this, t);
return `${e}T${r}${n}`;
}, x = function(t, e = E(this, h)) {
if (!new RegExp(S).test(t))
throw new Error(`Expect tz format as UTC offset: ${S}`);
let r = t;
const n = typeof globalThis < "u" && Object.prototype.toString.call(globalThis) === "[object global]";
if (n && v[t] === void 0)
throw new Error("tzOffset not supported");
n && (r = v[t]);
const o = {
year: "numeric",
month: "numeric",
day: "numeric",
hour: "numeric",
minute: "numeric",
second: "numeric",
hour12: !1,
timeZone: r
}, u = new Intl.DateTimeFormat("en-US", o), G = new Date(e), m = u.formatToParts(G), w = Object.fromEntries(
m.filter((T) => T.type !== "literal").map((T) => [
T.type,
T.value
])
);
return s(this, i, I).call(this, { ...w, tzOffset: t });
}, H = function(t) {
const e = s(this, i, M).call(this, t);
let r = new Date(
t.year ?? 0,
t.month === void 0 ? 0 : t.month - 1,
t.day ?? 0,
t.hour ?? 0,
t.minute ?? 0,
t.second ?? 0
);
r.toISOString().endsWith("Z") && (r = new Date(r.getTime() - r.getTimezoneOffset() * 60 * 1e3));
const n = `${r.toISOString().slice(0, 19)}${e}`;
return s(this, i, x).call(this, e, n);
}, g(d, c);
let A = d;
export {
A as DateTimeIso8601
};