UNPKG

dayjs-tz-helper

Version:
102 lines (97 loc) 4.29 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('dayjs'), require('dayjs/plugin/utc'), require('dayjs/plugin/timezone'), require('@lazy-num/parse-number-string')) : typeof define === 'function' && define.amd ? define(['exports', 'dayjs', 'dayjs/plugin/utc', 'dayjs/plugin/timezone', '@lazy-num/parse-number-string'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.DayjsTzHelper = {}, global.dayjs, global.utc, global.timezone, global.parseNumberString)); })(this, (function (exports, dayjs, utc, timezone, parseNumberString) { 'use strict'; /// <reference types="dayjs/plugin/timezone.d.ts" /> /// <reference types="dayjs/plugin/utc.d.ts" /> dayjs.extend(utc); dayjs.extend(timezone); function _isUnsafeOffsetDateString(date) { return /(?:\dZ|\+\d{2}:?\d{2})$/.test(date); } function _isUnsafeUTCDateString(date) { return /(?:GMT|UTC)/.test(date); } /** * @example * tzDayjsSafeParse('2023-03-31T04:00:00.000Z') * tzDayjsSafeParse('2023-03-31T12:00:00+08:00') * tzDayjsSafeParse('2023-03-31T04:00:00.800Z') * tzDayjsSafeParse('2023-03-31T04:00:00+00:00') * // => 2023-03-31T04:00:00Z * // => 1680235200 * @example * tzDayjsSafeParse('Fri, 31 Mar 2023 04:00:00', 'GMT') * * @see https://github.com/iamkun/dayjs/issues/2300 * @see https://github.com/iamkun/dayjs/issues/2303 * * @see https://day.js.org/docs/en/timezone/timezone * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones */ function tzDayjsSafeParse(dateOrMilliseconds, timezone) { var _dateOrMilliseconds, _timezone$timezone; if (timezone === null || typeof timezone !== 'object') { timezone = { timezone }; } if (typeof dateOrMilliseconds === 'number' || parseNumberString.isFloatString(dateOrMilliseconds)) { dateOrMilliseconds = Number(dateOrMilliseconds); if (typeof timezone.minValidTimestamp === 'number') { dateOrMilliseconds = Math.max(dateOrMilliseconds, timezone.minValidTimestamp); } else if (timezone.minValidTimestamp) { dateOrMilliseconds = Math.max(dateOrMilliseconds, 0); } dateOrMilliseconds = (timezone.isUnixTimestampSeconds ? dayjs.unix : dayjs)(dateOrMilliseconds); } else if (typeof dateOrMilliseconds === 'string') { if (_isUnsafeOffsetDateString(dateOrMilliseconds)) { dateOrMilliseconds = dayjs.utc(dateOrMilliseconds); } else if (timezone.timezone === 'GMT' && !_isUnsafeUTCDateString(dateOrMilliseconds)) { dateOrMilliseconds += ' GMT'; } else { dateOrMilliseconds = dayjs(dateOrMilliseconds); } } return dayjs.tz((_dateOrMilliseconds = dateOrMilliseconds) !== null && _dateOrMilliseconds !== void 0 ? _dateOrMilliseconds : void 0, (_timezone$timezone = timezone.timezone) !== null && _timezone$timezone !== void 0 ? _timezone$timezone : void 0); } function secondsToMilliseconds(timestamp) { return timestamp * 1000; } function millisecondsToSeconds(timestamp) { return timestamp / 1000; } // @ts-ignore { Object.defineProperty(tzDayjsSafeParse, "__esModule", { value: true }); Object.defineProperty(tzDayjsSafeParse, 'tzDayjsSafeParse', { value: tzDayjsSafeParse }); Object.defineProperty(tzDayjsSafeParse, 'default', { value: tzDayjsSafeParse }); Object.defineProperty(tzDayjsSafeParse, '_isUnsafeOffsetDateString', { value: _isUnsafeOffsetDateString }); Object.defineProperty(tzDayjsSafeParse, '_isUnsafeUTCDateString', { value: _isUnsafeUTCDateString }); Object.defineProperty(tzDayjsSafeParse, 'secondsToMilliseconds', { value: secondsToMilliseconds }); Object.defineProperty(tzDayjsSafeParse, 'millisecondsToSeconds', { value: millisecondsToSeconds }); } exports._isUnsafeOffsetDateString = _isUnsafeOffsetDateString; exports._isUnsafeUTCDateString = _isUnsafeUTCDateString; exports.default = tzDayjsSafeParse; exports.millisecondsToSeconds = millisecondsToSeconds; exports.secondsToMilliseconds = secondsToMilliseconds; exports.tzDayjsSafeParse = tzDayjsSafeParse; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=index.umd.development.cjs.map