UNPKG

dayjs-tz-helper

Version:

Day.js 時區輔助套件,提供安全解析時區日期字串與時間戳轉換功能 (Day.js timezone helper for safe timezone date string parsing and timestamp conversion)

142 lines (137 loc) 6.01 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); /** * 偵測結尾是否為不安全的時區偏移日期字串 * Detect if the string ends with an unsafe timezone offset date string * * 偵測結尾是否為 `.000Z`、`Z`、`+00:00` 等格式 * Detect if the string ends with `.000Z`, `Z`, `+00:00`, etc. * * @param {string} date - 日期字串 / Date string * @returns {boolean} 是否為不安全的偏移日期字串 / Whether it is an unsafe offset date string */ function _isUnsafeOffsetDateString(date) { return /(?:\dZ|\+\d{2}:?\d{2})$/.test(date); } /** * 偵測是否為 UTC 或 GMT 日期字串 * Detect if it is a UTC or GMT date string * * @param {string} date - 日期字串 / Date string * @returns {boolean} 是否為 UTC/GMT 日期字串 / Whether it is a UTC/GMT date string */ function _isUnsafeUTCDateString(date) { return /(?:GMT|UTC)/.test(date); } /** * 安全解析時區日期字串 * Safely parse timezone date string * * 處理多種邊界情況,包括 UTC 偏移、浮點數字串、Unix 時間戳等 * Handle various edge cases including UTC offset, float string, Unix timestamp, etc. * * @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') * * @param {ConfigType} [dateOrMilliseconds] - 日期或毫秒時間戳 / Date or millisecond timestamp * @param {ITimezone | IOptionsTzDayjsSafeParse} [timezoneOrOptions] - 時區設定 / Timezone setting * @returns {dayjs.Dayjs} 解析後的 dayjs 物件 / Parsed dayjs object * * @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, timezoneOrOptions) { var _timezoneOrOptions$ti; if (timezoneOrOptions === null || typeof timezoneOrOptions !== 'object') { timezoneOrOptions = { timezone: timezoneOrOptions }; } if (typeof dateOrMilliseconds === 'number' || parseNumberString.isFloatString(dateOrMilliseconds)) { dateOrMilliseconds = Number(dateOrMilliseconds); if (typeof timezoneOrOptions.minValidTimestamp === 'number') { dateOrMilliseconds = Math.max(dateOrMilliseconds, timezoneOrOptions.minValidTimestamp); } else if (timezoneOrOptions.minValidTimestamp) { dateOrMilliseconds = Math.max(dateOrMilliseconds, 0); } dateOrMilliseconds = (timezoneOrOptions.isUnixTimestampSeconds ? dayjs.unix : dayjs)(dateOrMilliseconds); } else if (typeof dateOrMilliseconds === 'string') { if (_isUnsafeOffsetDateString(dateOrMilliseconds)) { dateOrMilliseconds = dayjs.utc(dateOrMilliseconds); } else if (timezoneOrOptions.timezone === 'GMT' && !_isUnsafeUTCDateString(dateOrMilliseconds)) { dateOrMilliseconds += ' GMT'; } else { dateOrMilliseconds = dayjs(dateOrMilliseconds); } } return dayjs.tz(dateOrMilliseconds !== null && dateOrMilliseconds !== void 0 ? dateOrMilliseconds : void 0, (_timezoneOrOptions$ti = timezoneOrOptions.timezone) !== null && _timezoneOrOptions$ti !== void 0 ? _timezoneOrOptions$ti : void 0); } /** * 秒轉毫秒 * Convert seconds to milliseconds * * @param {number} timestamp - Unix 時間戳(秒)/ Unix timestamp (seconds) * @returns {number} 毫秒時間戳 / Millisecond timestamp */ function secondsToMilliseconds(timestamp) { return timestamp * 1000; } /** * 毫秒轉秒 * Convert milliseconds to seconds * * @param {number} timestamp - 毫秒時間戳 / Millisecond timestamp * @returns {number} Unix 時間戳(秒)/ Unix timestamp (seconds) */ 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