UNPKG

dayjs-tz-helper

Version:
1 lines 6.64 kB
{"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 * detect end with `.000Z` , `Z` , `+00:00`\n */\nexport function _isUnsafeOffsetDateString(date: string)\n{\n\treturn /(?:\\dZ|\\+\\d{2}:?\\d{2})$/.test(date)\n}\n\nexport function _isUnsafeUTCDateString(date: string)\n{\n\treturn /(?:GMT|UTC)/.test(date)\n}\n\n/**\n * Day.js supports time zone via the Internationalization API in supported environments. By using the native API, no extra bytes of timezone data need to be included in code bundle.\n * The list of all time zone names can be found in the IANA database.\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\nexport interface IOptionsTzDayjsSafeParse\n{\n\t/**\n\t * Day.js supports time zone via the Internationalization API in supported environments. By using the native API, no extra bytes of timezone data need to be included in code bundle.\n\t * The list of all time zone names can be found in the IANA database.\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 * Minimum valid timestamp.\n\t * If set to `true` or `0`, it represents `1970-01-01T00:00:00.000Z`.\n\t *\n\t * Only works when the input is a number.\n\t */\n\tminValidTimestamp?: number | boolean,\n\t/**\n\t * If set to `true`, use the input timestamp as Unix Timestamp (seconds).\n\t * Equivalent to `dayjs.unix(1318781876.721)` or `dayjs(timestamp * 1000)`.\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 * @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 * @see https://github.com/iamkun/dayjs/issues/2300\n * @see https://github.com/iamkun/dayjs/issues/2303\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 function tzDayjsSafeParse(dateOrMilliseconds?: ConfigType, timezone?: ITimezone | IOptionsTzDayjsSafeParse)\n{\n\tif (timezone === null || typeof timezone !== 'object')\n\t{\n\t\ttimezone = {\n\t\t\ttimezone,\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 timezone.minValidTimestamp === 'number')\n\t\t{\n\t\t\tdateOrMilliseconds = Math.max(dateOrMilliseconds, timezone.minValidTimestamp);\n\t\t}\n\t\telse if (timezone.minValidTimestamp)\n\t\t{\n\t\t\tdateOrMilliseconds = Math.max(dateOrMilliseconds, 0);\n\t\t}\n\n\t\tdateOrMilliseconds = (timezone.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 * for fix bug when parse end with `.000Z` , `Z` , `+00:00`\n\t\t\t */\n\t\t\tdateOrMilliseconds = dayjs.utc(dateOrMilliseconds)\n\t\t}\n\t\telse if (timezone.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\t\treturn dayjs.tz(dateOrMilliseconds ?? void 0, timezone.timezone ?? void 0)\n}\n\nexport function secondsToMilliseconds(timestamp: number)\n{\n\treturn timestamp * 1000\n}\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","timezone","_dateOrMilliseconds","_timezone$timezone","isFloatString","Number","minValidTimestamp","Math","max","isUnixTimestampSeconds","dayjs","unix","utc","tz","secondsToMilliseconds","timestamp","millisecondsToSeconds","extend","Object","defineProperty","value"],"mappings":";;;;EAcM,SAAUA,0BAA0BC;IAEzC,OAAO,0BAA0BC,KAAKD;AACvC;EAEM,SAAUE,uBAAuBF;IAEtC,OAAO,cAAcC,KAAKD;AAC3B;EA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