@anoki/fse-marche-ui
Version:
FSE UI components library
183 lines (182 loc) • 5.15 kB
JavaScript
import { parseZoneInfo as y, formatOffset as T, objToLocalTS as v, isUndefined as w } from "./index.es124.js";
import C from "./index.es133.js";
const h = /* @__PURE__ */ new Map();
function D(n) {
let t = h.get(n);
return t === void 0 && (t = new Intl.DateTimeFormat("en-US", {
hour12: !1,
timeZone: n,
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
era: "short"
}), h.set(n, t)), t;
}
const N = {
year: 0,
month: 1,
day: 2,
era: 3,
hour: 4,
minute: 5,
second: 6
};
function O(n, t) {
const e = n.format(t).replace(/\u200E/g, ""), r = /(\d+)\/(\d+)\/(\d+) (AD|BC),? (\d+):(\d+):(\d+)/.exec(e), [, a, i, o, s, f, d, u] = r;
return [o, a, i, s, f, d, u];
}
function S(n, t) {
const e = n.formatToParts(t), r = [];
for (let a = 0; a < e.length; a++) {
const { type: i, value: o } = e[a], s = N[i];
i === "era" ? r[s] = o : w(s) || (r[s] = parseInt(o, 10));
}
return r;
}
const m = /* @__PURE__ */ new Map();
class p extends C {
/**
* @param {string} name - Zone name
* @return {IANAZone}
*/
static create(t) {
let e = m.get(t);
return e === void 0 && m.set(t, e = new p(t)), e;
}
/**
* Reset local caches. Should only be necessary in testing scenarios.
* @return {void}
*/
static resetCache() {
m.clear(), h.clear();
}
/**
* Returns whether the provided string is a valid specifier. This only checks the string's format, not that the specifier identifies a known zone; see isValidZone for that.
* @param {string} s - The string to check validity on
* @example IANAZone.isValidSpecifier("America/New_York") //=> true
* @example IANAZone.isValidSpecifier("Sport~~blorp") //=> false
* @deprecated For backward compatibility, this forwards to isValidZone, better use `isValidZone()` directly instead.
* @return {boolean}
*/
static isValidSpecifier(t) {
return this.isValidZone(t);
}
/**
* Returns whether the provided string identifies a real zone
* @param {string} zone - The string to check
* @example IANAZone.isValidZone("America/New_York") //=> true
* @example IANAZone.isValidZone("Fantasia/Castle") //=> false
* @example IANAZone.isValidZone("Sport~~blorp") //=> false
* @return {boolean}
*/
static isValidZone(t) {
if (!t)
return !1;
try {
return new Intl.DateTimeFormat("en-US", { timeZone: t }).format(), !0;
} catch {
return !1;
}
}
constructor(t) {
super(), this.zoneName = t, this.valid = p.isValidZone(t);
}
/**
* The type of zone. `iana` for all instances of `IANAZone`.
* @override
* @type {string}
*/
get type() {
return "iana";
}
/**
* The name of this zone (i.e. the IANA zone name).
* @override
* @type {string}
*/
get name() {
return this.zoneName;
}
/**
* Returns whether the offset is known to be fixed for the whole year:
* Always returns false for all IANA zones.
* @override
* @type {boolean}
*/
get isUniversal() {
return !1;
}
/**
* Returns the offset's common name (such as EST) at the specified timestamp
* @override
* @param {number} ts - Epoch milliseconds for which to get the name
* @param {Object} opts - Options to affect the format
* @param {string} opts.format - What style of offset to return. Accepts 'long' or 'short'.
* @param {string} opts.locale - What locale to return the offset name in.
* @return {string}
*/
offsetName(t, { format: e, locale: r }) {
return y(t, e, r, this.name);
}
/**
* Returns the offset's value as a string
* @override
* @param {number} ts - Epoch milliseconds for which to get the offset
* @param {string} format - What style of offset to return.
* Accepts 'narrow', 'short', or 'techie'. Returning '+6', '+06:00', or '+0600' respectively
* @return {string}
*/
formatOffset(t, e) {
return T(this.offset(t), e);
}
/**
* Return the offset in minutes for this zone at the specified timestamp.
* @override
* @param {number} ts - Epoch milliseconds for which to compute the offset
* @return {number}
*/
offset(t) {
if (!this.valid) return NaN;
const e = new Date(t);
if (isNaN(e)) return NaN;
const r = D(this.name);
let [a, i, o, s, f, d, u] = r.formatToParts ? S(r, e) : O(r, e);
s === "BC" && (a = -Math.abs(a) + 1);
const g = v({
year: a,
month: i,
day: o,
hour: f === 24 ? 0 : f,
minute: d,
second: u,
millisecond: 0
});
let c = +e;
const l = c % 1e3;
return c -= l >= 0 ? l : 1e3 + l, (g - c) / (60 * 1e3);
}
/**
* Return whether this Zone is equal to another zone
* @override
* @param {Zone} otherZone - the zone to compare
* @return {boolean}
*/
equals(t) {
return t.type === "iana" && t.name === this.name;
}
/**
* Return whether this Zone is valid.
* @override
* @type {boolean}
*/
get isValid() {
return this.valid;
}
}
export {
p as default
};
//# sourceMappingURL=index.es132.js.map