laif-ds
Version:
Design System di Laif con componenti React basati su principi di Atomic Design
66 lines (65 loc) • 2.99 kB
JavaScript
"use client";
import { tzOffset as i } from "../tzOffset/index.js";
class n extends Date {
//#region static
constructor(...e) {
super(), e.length > 1 && typeof e[e.length - 1] == "string" && (this.timeZone = e.pop()), this.internal = /* @__PURE__ */ new Date(), isNaN(i(this.timeZone, this)) ? this.setTime(NaN) : e.length ? typeof e[0] == "number" && (e.length === 1 || e.length === 2 && typeof e[1] != "number") ? this.setTime(e[0]) : typeof e[0] == "string" ? this.setTime(+new Date(e[0])) : e[0] instanceof Date ? this.setTime(+e[0]) : (this.setTime(+new Date(...e)), h(this), l(this)) : this.setTime(Date.now());
}
static tz(e, ...s) {
return s.length ? new n(...s, e) : new n(Date.now(), e);
}
//#endregion
//#region time zone
withTimeZone(e) {
return new n(+this, e);
}
getTimezoneOffset() {
return -i(this.timeZone, this);
}
//#endregion
//#region time
setTime(e) {
return Date.prototype.setTime.apply(this, arguments), l(this), +this;
}
//#endregion
//#region date-fns integration
[Symbol.for("constructDateFrom")](e) {
return new n(+new Date(e), this.timeZone);
}
//#endregion
}
const T = /^(get|set)(?!UTC)/;
Object.getOwnPropertyNames(Date.prototype).forEach((t) => {
if (!T.test(t)) return;
const e = t.replace(T, "$1UTC");
n.prototype[e] && (t.startsWith("get") ? n.prototype[t] = function() {
return this.internal[e]();
} : (n.prototype[t] = function() {
return Date.prototype[e].apply(this.internal, arguments), U(this), +this;
}, n.prototype[e] = function() {
return Date.prototype[e].apply(this, arguments), l(this), +this;
}));
});
function l(t) {
t.internal.setTime(+t), t.internal.setUTCMinutes(t.internal.getUTCMinutes() - t.getTimezoneOffset());
}
function U(t) {
Date.prototype.setFullYear.call(t, t.internal.getUTCFullYear(), t.internal.getUTCMonth(), t.internal.getUTCDate()), Date.prototype.setHours.call(t, t.internal.getUTCHours(), t.internal.getUTCMinutes(), t.internal.getUTCSeconds(), t.internal.getUTCMilliseconds()), h(t);
}
function h(t) {
const e = i(t.timeZone, t), s = /* @__PURE__ */ new Date(+t);
s.setUTCHours(s.getUTCHours() - 1);
const p = -(/* @__PURE__ */ new Date(+t)).getTimezoneOffset(), m = -(/* @__PURE__ */ new Date(+s)).getTimezoneOffset(), u = p - m, y = Date.prototype.getHours.apply(t) !== t.internal.getUTCHours();
u && y && t.internal.setUTCMinutes(t.internal.getUTCMinutes() + u);
const o = p - e;
o && Date.prototype.setUTCMinutes.call(t, Date.prototype.getUTCMinutes.call(t) + o);
const r = i(t.timeZone, t), D = -(/* @__PURE__ */ new Date(+t)).getTimezoneOffset() - r, C = r !== e, c = D - o;
if (C && c) {
Date.prototype.setUTCMinutes.call(t, Date.prototype.getUTCMinutes.call(t) + c);
const a = i(t.timeZone, t), f = r - a;
f && (t.internal.setUTCMinutes(t.internal.getUTCMinutes() + f), Date.prototype.setUTCMinutes.call(t, Date.prototype.getUTCMinutes.call(t) + f));
}
}
export {
n as TZDateMini
};