@barchart/common-js
Version:
Library of common JavaScript utilities
184 lines (178 loc) • 5.54 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var Timezones_exports = {};
__export(Timezones_exports, {
default: () => Timezones
});
module.exports = __toCommonJS(Timezones_exports);
var assert = __toESM(require("./assert.js"));
var is = __toESM(require("./is.js"));
var timezone = __toESM(require("./timezone.js"));
var import_Enum = __toESM(require("./Enum.js"));
var import_tz = require("@date-fns/tz");
class Timezones extends import_Enum.default {
/**
* @param {string} code - The timezone name.
*/
constructor(code) {
super(code, code);
}
/**
* Attempts to determine whether daylight saving time is in effect.
*
* @public
* @param {number=} timestamp - The moment at which daylight saving time is checked; otherwise, the current time is used.
* @returns {boolean}
*/
getIsDaylightSavingsTime(timestamp) {
assert.argumentIsOptional(timestamp, "timestamp", Number);
const now = /* @__PURE__ */ new Date();
let baseline = Date.UTC(now.getFullYear(), 0, 1);
let candidate;
if (timestamp) {
candidate = timestamp;
} else {
candidate = now.getTime();
}
const baselineOffset = this.getUtcOffset(baseline);
const candidateOffset = this.getUtcOffset(candidate);
return baselineOffset !== candidateOffset;
}
/**
* Calculates and returns the offset of a timezone from UTC.
*
* @public
* @param {number=} timestamp - The moment at which the offset is calculated; otherwise, the current time is used.
* @param {boolean=} milliseconds - Whether the offset should be returned in milliseconds instead of minutes.
* @returns {number}
*/
getUtcOffset(timestamp, milliseconds) {
assert.argumentIsOptional(timestamp, "timestamp", Number);
assert.argumentIsOptional(milliseconds, "milliseconds", Boolean);
let timestampToUse;
if (is.number(timestamp)) {
timestampToUse = timestamp;
} else {
timestampToUse = (/* @__PURE__ */ new Date()).getTime();
}
let multiplier;
if (is.boolean(milliseconds) && milliseconds) {
multiplier = 60 * 1e3;
} else {
multiplier = 1;
}
return (0, import_tz.tzOffset)(this.code, new Date(timestampToUse)) * multiplier;
}
/**
* Given a code, returns the corresponding enumeration item.
*
* @public
* @static
* @param {string} code
* @returns {Timezones|null}
*/
static parse(code) {
const value = import_Enum.default.fromCode(Timezones, code);
return value instanceof Timezones ? value : null;
}
/**
* UTC.
*
* @public
* @static
* @returns {Timezones}
*/
static get UTC() {
return utc;
}
/**
* America/Chicago.
*
* @public
* @static
* @returns {Timezones}
*/
static get AMERICA_CHICAGO() {
return america_chicago;
}
/**
* America/New_York.
*
* @public
* @static
* @returns {Timezones}
*/
static get AMERICA_NEW_YORK() {
return america_new_york;
}
/**
* America/Denver.
*
* @public
* @static
* @returns {Timezones}
*/
static get AMERICA_DENVER() {
return america_denver;
}
/**
* Returns a string representation.
*
* @public
* @returns {string}
*/
toString() {
return `[Timezone (name=${this.code})]`;
}
}
timezone.getTimezones().forEach((name) => {
new Timezones(name);
});
const utc = getRequiredTimezone("UTC");
const america_chicago = getRequiredTimezone("America/Chicago");
const america_new_york = getRequiredTimezone("America/New_York");
const america_denver = getRequiredTimezone("America/Denver");
function getRequiredTimezone(code) {
const value = Timezones.parse(code);
if (value === null) {
throw new Error(`Timezone "${code}" is not registered.`);
}
return value;
}
{
const cjsExports = module.exports;
const cjsDefaultExport = cjsExports && cjsExports.__esModule ? cjsExports.default : cjsExports;
if (cjsDefaultExport && (typeof cjsDefaultExport === 'function' || typeof cjsDefaultExport === 'object')) {
Object.keys(cjsExports).forEach((key) => {
if (key !== 'default' && key !== '__esModule') {
cjsDefaultExport[key] = cjsExports[key];
}
});
}
module.exports = cjsDefaultExport;
}