@anoki/fse-ui
Version:
FSE UI components library
180 lines (179 loc) • 4.99 kB
JavaScript
import { parseZoneInfo as g, formatOffset as T, objToLocalTS as C, isUndefined as D } from "./index.es167.js";
import N from "./index.es176.js";
let u = {};
function O(n) {
return u[n] || (u[n] = 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"
})), u[n];
}
const S = {
year: 0,
month: 1,
day: 2,
era: 3,
hour: 4,
minute: 5,
second: 6
};
function Z(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, c, l] = r;
return [o, a, i, s, f, c, l];
}
function v(n, t) {
const e = n.formatToParts(t), r = [];
for (let a = 0; a < e.length; a++) {
const { type: i, value: o } = e[a], s = S[i];
i === "era" ? r[s] = o : D(s) || (r[s] = parseInt(o, 10));
}
return r;
}
let d = {};
class p extends N {
/**
* @param {string} name - Zone name
* @return {IANAZone}
*/
static create(t) {
return d[t] || (d[t] = new p(t)), d[t];
}
/**
* Reset local caches. Should only be necessary in testing scenarios.
* @return {void}
*/
static resetCache() {
d = {}, u = {};
}
/**
* 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 g(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) {
const e = new Date(t);
if (isNaN(e)) return NaN;
const r = O(this.name);
let [a, i, o, s, f, c, l] = r.formatToParts ? v(r, e) : Z(r, e);
s === "BC" && (a = -Math.abs(a) + 1);
const y = C({
year: a,
month: i,
day: o,
hour: f === 24 ? 0 : f,
minute: c,
second: l,
millisecond: 0
});
let m = +e;
const h = m % 1e3;
return m -= h >= 0 ? h : 1e3 + h, (y - m) / (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.es175.js.map