Files
indiekit-server/node_modules/@date-fns/tz/date/index.cjs

84 lines
2.4 KiB
JavaScript

"use strict";
exports.TZDate = void 0;
var _index = require("../tzName/index.cjs");
var _mini = require("./mini.cjs");
class TZDate extends _mini.TZDateMini {
//#region static
static tz(tz, ...args) {
return args.length ? new TZDate(...args, tz) : new TZDate(Date.now(), tz);
}
//#endregion
//#region representation
toISOString() {
const [sign, hours, minutes] = this.tzComponents();
const tz = `${sign}${hours}:${minutes}`;
return this.internal.toISOString().slice(0, -1) + tz;
}
toString() {
// "Tue Aug 13 2024 07:50:19 GMT+0800 (Singapore Standard Time)";
return `${this.toDateString()} ${this.toTimeString()}`;
}
toDateString() {
// toUTCString returns RFC 7231 ("Mon, 12 Aug 2024 23:36:08 GMT")
const [day, date, month, year] = this.internal.toUTCString().split(" ");
// "Tue Aug 13 2024"
return `${day?.slice(0, -1) /* Remove "," */} ${month} ${date} ${year}`;
}
toTimeString() {
// toUTCString returns RFC 7231 ("Mon, 12 Aug 2024 23:36:08 GMT")
const time = this.internal.toUTCString().split(" ")[4];
const [sign, hours, minutes] = this.tzComponents();
// "07:42:23 GMT+0800 (Singapore Standard Time)"
return `${time} GMT${sign}${hours}${minutes} (${(0, _index.tzName)(this.timeZone, this)})`;
}
toLocaleString(locales, options) {
return Date.prototype.toLocaleString.call(this, locales, {
...options,
timeZone: options?.timeZone || this.timeZone
});
}
toLocaleDateString(locales, options) {
return Date.prototype.toLocaleDateString.call(this, locales, {
...options,
timeZone: options?.timeZone || this.timeZone
});
}
toLocaleTimeString(locales, options) {
return Date.prototype.toLocaleTimeString.call(this, locales, {
...options,
timeZone: options?.timeZone || this.timeZone
});
}
//#endregion
//#region private
tzComponents() {
const offset = this.getTimezoneOffset();
const sign = offset > 0 ? "-" : "+";
const hours = String(Math.floor(Math.abs(offset) / 60)).padStart(2, "0");
const minutes = String(Math.abs(offset) % 60).padStart(2, "0");
return [sign, hours, minutes];
}
//#endregion
withTimeZone(timeZone) {
return new TZDate(+this, timeZone);
}
//#region date-fns integration
[Symbol.for("constructDateFrom")](date) {
return new TZDate(+new Date(date), this.timeZone);
}
//#endregion
}
exports.TZDate = TZDate;