{"version":3,"file":"index.umd.production.min.cjs","sources":["../src/index.ts"],"sourcesContent":["/// <reference types=\"dayjs/plugin/timezone.d.ts\" />\n/// <reference types=\"dayjs/plugin/utc.d.ts\" />\n\nimport dayjs, { type ConfigType } from 'dayjs';\nimport utc from 'dayjs/plugin/utc';\nimport timezone from 'dayjs/plugin/timezone';\nimport { isFloatString } from '@lazy-num/parse-number-string';\n\ndayjs.extend(utc);\ndayjs.extend(timezone);\n\n/**\n * 偵測結尾是否為不安全的時區偏移日期字串\n * Detect if the string ends with an unsafe timezone offset date string\n *\n * 偵測結尾是否為 `.000Z`、`Z`、`+00:00` 等格式\n * Detect if the string ends with `.000Z`, `Z`, `+00:00`, etc.\n *\n * @param {string} date - 日期字串 / Date string\n * @returns {boolean} 是否為不安全的偏移日期字串 / Whether it is an unsafe offset date string\n */\nexport function _isUnsafeOffsetDateString(date: string)\n{\n\treturn /(?:\\dZ|\\+\\d{2}:?\\d{2})$/.test(date)\n}\n\n/**\n * 偵測是否為 UTC 或 GMT 日期字串\n * Detect if it is a UTC or GMT date string\n *\n * @param {string} date - 日期字串 / Date string\n * @returns {boolean} 是否為 UTC/GMT 日期字串 / Whether it is a UTC/GMT date string\n */\nexport function _isUnsafeUTCDateString(date: string)\n{\n\treturn /(?:GMT|UTC)/.test(date)\n}\n\n/**\n * 時區類型 - 支援 IANA 時區資料庫中的時區名稱\n * Timezone type - supports timezone names from IANA database\n *\n * Day.js 透過 Internationalization API 支援時區，無需在程式碼中包含額外的時區資料。\n * Day.js supports timezone via the Internationalization API, no extra timezone data needed in code bundle.\n *\n * @see https://day.js.org/docs/en/timezone/timezone\n * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones\n */\nexport type ITimezone = string | 'GMT' | 'Asia/Taipei' | 'Asia/Tokyo' | 'America/New_York';\n\n/**\n * tzDayjsSafeParse 選項介面\n * tzDayjsSafeParse options interface\n */\nexport interface IOptionsTzDayjsSafeParse\n{\n\t/**\n\t * 時區設定\n\t * Timezone setting\n\t *\n\t * Day.js 透過 Internationalization API 支援時區，無需在程式碼中包含額外的時區資料。\n\t * Day.js supports timezone via the Internationalization API, no extra timezone data needed in code bundle.\n\t *\n\t * @see https://day.js.org/docs/en/timezone/timezone\n\t * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones\n\t */\n\ttimezone?: ITimezone,\n\t/**\n\t * 最小有效時間戳\n\t * Minimum valid timestamp\n\t *\n\t * 若設為 `true` 或 `0`，代表 `1970-01-01T00:00:00.000Z`\n\t * If set to `true` or `0`, it represents `1970-01-01T00:00:00.000Z`\n\t *\n\t * 僅在輸入為數字時生效\n\t * Only works when the input is a number\n\t */\n\tminValidTimestamp?: number | boolean,\n\t/**\n\t * 是否將輸入時間戳視為 Unix 時間戳（秒）\n\t * Whether to use input timestamp as Unix Timestamp (seconds)\n\t *\n\t * 相當於 `dayjs.unix(1318781876.721)` 或 `dayjs(timestamp * 1000)`\n\t * Equivalent to `dayjs.unix(1318781876.721)` or `dayjs(timestamp * 1000)`\n\t *\n\t * 僅在輸入為數字時生效\n\t * Only works when the input is a number\n\t *\n\t * @see https://day.js.org/docs/en/parse/unix-timestamp\n\t */\n\tisUnixTimestampSeconds?: boolean,\n}\n\n/**\n * 安全解析時區日期字串\n * Safely parse timezone date string\n *\n * 處理多種邊界情況，包括 UTC 偏移、浮點數字串、Unix 時間戳等\n * Handle various edge cases including UTC offset, float string, Unix timestamp, etc.\n *\n * @example\n * tzDayjsSafeParse('2023-03-31T04:00:00.000Z')\n * tzDayjsSafeParse('2023-03-31T12:00:00+08:00')\n * tzDayjsSafeParse('2023-03-31T04:00:00.800Z')\n * tzDayjsSafeParse('2023-03-31T04:00:00+00:00')\n * // => 2023-03-31T04:00:00Z\n * // => 1680235200\n * @example\n * tzDayjsSafeParse('Fri, 31 Mar 2023 04:00:00', 'GMT')\n *\n * @param {ConfigType} [dateOrMilliseconds] - 日期或毫秒時間戳 / Date or millisecond timestamp\n * @param {ITimezone | IOptionsTzDayjsSafeParse} [timezoneOrOptions] - 時區設定 / Timezone setting\n * @returns {dayjs.Dayjs} 解析後的 dayjs 物件 / Parsed dayjs object\n *\n * @see https://github.com/iamkun/dayjs/issues/2300\n * @see https://github.com/iamkun/dayjs/issues/2303\n * @see https://day.js.org/docs/en/timezone/timezone\n * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones\n */\nexport function tzDayjsSafeParse(dateOrMilliseconds?: ConfigType, timezoneOrOptions?: ITimezone | IOptionsTzDayjsSafeParse)\n{\n\tif (timezoneOrOptions === null || typeof timezoneOrOptions !== 'object')\n\t{\n\t\ttimezoneOrOptions = {\n\t\t\ttimezone: timezoneOrOptions,\n\t\t} as IOptionsTzDayjsSafeParse\n\t}\n\n\tif (typeof dateOrMilliseconds === 'number' || isFloatString(dateOrMilliseconds))\n\t{\n\t\tdateOrMilliseconds = Number(dateOrMilliseconds);\n\n\t\tif (typeof timezoneOrOptions.minValidTimestamp === 'number')\n\t\t{\n\t\t\tdateOrMilliseconds = Math.max(dateOrMilliseconds, timezoneOrOptions.minValidTimestamp);\n\t\t}\n\t\telse if (timezoneOrOptions.minValidTimestamp)\n\t\t{\n\t\t\tdateOrMilliseconds = Math.max(dateOrMilliseconds, 0);\n\t\t}\n\n\t\tdateOrMilliseconds = (timezoneOrOptions.isUnixTimestampSeconds ? dayjs.unix : dayjs)(dateOrMilliseconds);\n\t}\n\telse if (typeof dateOrMilliseconds === 'string')\n\t{\n\t\tif (_isUnsafeOffsetDateString(dateOrMilliseconds))\n\t\t{\n\t\t\t/**\n\t\t\t * 修復結尾為 `.000Z`、`Z`、`+00:00` 時的解析錯誤\n\t\t\t * Fix parsing error when string ends with `.000Z`, `Z`, or `+00:00`\n\t\t\t */\n\t\t\tdateOrMilliseconds = dayjs.utc(dateOrMilliseconds)\n\t\t}\n\t\telse if (timezoneOrOptions.timezone === 'GMT' && !_isUnsafeUTCDateString(dateOrMilliseconds))\n\t\t{\n\t\t\tdateOrMilliseconds += ' GMT';\n\t\t}\n\t\telse\n\t\t{\n\t\t\tdateOrMilliseconds = dayjs(dateOrMilliseconds);\n\t\t}\n\t}\n\n\treturn dayjs.tz(dateOrMilliseconds ?? void 0, timezoneOrOptions.timezone ?? void 0)\n}\n\n/**\n * 秒轉毫秒\n * Convert seconds to milliseconds\n *\n * @param {number} timestamp - Unix 時間戳（秒）/ Unix timestamp (seconds)\n * @returns {number} 毫秒時間戳 / Millisecond timestamp\n */\nexport function secondsToMilliseconds(timestamp: number)\n{\n\treturn timestamp * 1000\n}\n\n/**\n * 毫秒轉秒\n * Convert milliseconds to seconds\n *\n * @param {number} timestamp - 毫秒時間戳 / Millisecond timestamp\n * @returns {number} Unix 時間戳（秒）/ Unix timestamp (seconds)\n */\nexport function millisecondsToSeconds(timestamp: number)\n{\n\treturn timestamp / 1000\n}\n\nexport default tzDayjsSafeParse\n\n// @ts-ignore\nif (process.env.TSDX_FORMAT !== 'esm')\n{\n\tObject.defineProperty(tzDayjsSafeParse, \"__esModule\", { value: true });\n\n\tObject.defineProperty(tzDayjsSafeParse, 'tzDayjsSafeParse', { value: tzDayjsSafeParse });\n\tObject.defineProperty(tzDayjsSafeParse, 'default', { value: tzDayjsSafeParse });\n\n\tObject.defineProperty(tzDayjsSafeParse, '_isUnsafeOffsetDateString', { value: _isUnsafeOffsetDateString });\n\tObject.defineProperty(tzDayjsSafeParse, '_isUnsafeUTCDateString', { value: _isUnsafeUTCDateString });\n\n\tObject.defineProperty(tzDayjsSafeParse, 'secondsToMilliseconds', { value: secondsToMilliseconds });\n\tObject.defineProperty(tzDayjsSafeParse, 'millisecondsToSeconds', { value: millisecondsToSeconds });\n\n}\n"],"names":["_isUnsafeOffsetDateString","date","test","_isUnsafeUTCDateString","tzDayjsSafeParse","dateOrMilliseconds","timezoneOrOptions","_timezoneOrOptions$ti","timezone","isFloatString","Number","minValidTimestamp","Math","max","isUnixTimestampSeconds","dayjs","unix","utc","tz","secondsToMilliseconds","timestamp","millisecondsToSeconds","extend","Object","defineProperty","value"],"mappings":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