UNPKG

@cainiaofe/cn-utils

Version:

菜鸟前端基础工具库

116 lines (115 loc) 4.15 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dayjsTz = exports.getTimezoneOffset = exports.dateTimestamp = exports.dateFormat = exports.getTimezoneName = exports.getTimezone = exports.getDateFormatByStd = exports.getDateFormatConf = void 0; var tslib_1 = require("tslib"); var locale_1 = require("../locale"); var const_1 = require("../const"); /** * 根据语种信息获取日期格式化配置 * @param lang 语种 * @returns */ var getDateFormatConf = function (lang) { var _a; return tslib_1.__assign(tslib_1.__assign({}, const_1.STANDARD_DATE_FORMAT), (((_a = locale_1.locale[lang]) === null || _a === void 0 ? void 0 : _a.DATE_FORMATS) || null)); }; exports.getDateFormatConf = getDateFormatConf; /** * 将国际标准格式兑换成本地格式 * @param stdFormat * @param lang 语种 * @returns */ function getDateFormatByStd(stdFormat, lang) { var formatConfs = (0, exports.getDateFormatConf)(lang); var formatConfStds = (0, exports.getDateFormatConf)('zh-CN'); var key = Object.keys(formatConfStds).find(function (k) { return formatConfStds[k] === stdFormat; }); if (!key) { return stdFormat; } return formatConfs[key] || stdFormat; } exports.getDateFormatByStd = getDateFormatByStd; /** * 获取系统时区信息 * @returns */ var getTimezone = function () { return this.dayjs.tz.guess(); }; exports.getTimezone = getTimezone; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone * @param lang * @returns */ var getTimezoneName = function (num, timezone, lang) { var options = { timeZone: timezone, timeZoneName: 'long' }; var formatter = new Intl.DateTimeFormat(lang, options); var parts = formatter.formatToParts(num ? +num : new Date()); var zoneNamePart = parts.find(function (part) { return part.type === 'timeZoneName'; }); return zoneNamePart ? zoneNamePart.value : null; }; exports.getTimezoneName = getTimezoneName; /** * 根据当前语种&时区格式化时间戳 * @param this * @param num * @param opts * @returns */ var dateFormat = function (num, opts) { return this.dayjs(num).tz(opts.timezone).format(opts.format); }; exports.dateFormat = dateFormat; /** * 根据当前语种&时区获取时间戳 * @param this * @param dateStr string * @param opts FormatOpts * @returns */ var dateTimestamp = function (dateStr, opts) { if (!(opts === null || opts === void 0 ? void 0 : opts.format)) { // fix-reason: 先以默认时区创建时间 然后转换到北京时区 可能会导致时间发生变化 // return this.dayjs(dateStr).tz(opts?.timezone, true).valueOf(); return this.dayjs.tz(dateStr, opts === null || opts === void 0 ? void 0 : opts.timezone).valueOf(); } // fix-reason: 如果opts?.format不为空,则使用默认时区创建时间后,不强制转成utc再直接转换到北京时区 可能会导致时间发生变化 // return this.dayjs(dateStr, opts?.format).tz(opts?.timezone, true).valueOf(); return this.dayjs.tz(dateStr, opts === null || opts === void 0 ? void 0 : opts.format).utc().tz(opts === null || opts === void 0 ? void 0 : opts.timezone, true).valueOf(); }; exports.dateTimestamp = dateTimestamp; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone */ var getTimezoneOffset = function (num, timezone) { var offset = this.dayjs(num).tz(timezone).utcOffset(); var timeStr = this.dayjs(num).tz(timezone).format(); return { offset: offset, offsetText: timeStr.slice(timeStr.length - 6, timeStr.length), }; }; exports.getTimezoneOffset = getTimezoneOffset; /** * 设置 dayjs 对象时区 * @param this * @param day * @param timezone * @param keepLocalTime 是否保持本地时间 * @returns 新的 dayjs 对象 */ var dayjsTz = function (day, timezone, keepLocalTime) { if (typeof day === 'object' && day.valueOf) { return this.dayjs(day.valueOf()).tz(timezone, keepLocalTime); } return this.dayjs(day).tz(timezone, keepLocalTime); }; exports.dayjsTz = dayjsTz;