UNPKG

@cainiaofe/cn-utils

Version:

菜鸟前端基础工具库

105 lines (104 loc) 3.56 kB
import { __assign } from "tslib"; import { locale } from '../locale'; import { STANDARD_DATE_FORMAT } from '../const'; /** * 根据语种信息获取日期格式化配置 * @param lang 语种 * @returns */ export var getDateFormatConf = function (lang) { var _a; return __assign(__assign({}, STANDARD_DATE_FORMAT), (((_a = locale[lang]) === null || _a === void 0 ? void 0 : _a.DATE_FORMATS) || null)); }; /** * 将国际标准格式兑换成本地格式 * @param stdFormat * @param lang 语种 * @returns */ export function getDateFormatByStd(stdFormat, lang) { var formatConfs = getDateFormatConf(lang); var formatConfStds = getDateFormatConf('zh-CN'); var key = Object.keys(formatConfStds).find(function (k) { return formatConfStds[k] === stdFormat; }); if (!key) { return stdFormat; } return formatConfs[key] || stdFormat; } /** * 获取系统时区信息 * @returns */ export var getTimezone = function () { return this.dayjs.tz.guess(); }; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone * @param lang * @returns */ export 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; }; /** * 根据当前语种&时区格式化时间戳 * @param this * @param num * @param opts * @returns */ export var dateFormat = function (num, opts) { return this.dayjs(num).tz(opts.timezone).format(opts.format); }; /** * 根据当前语种&时区获取时间戳 * @param this * @param dateStr string * @param opts FormatOpts * @returns */ export 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(); }; /** * 根据时间戳获取当前日期偏移量 * @param this * @param num * @param timezone */ export 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), }; }; /** * 设置 dayjs 对象时区 * @param this * @param day * @param timezone * @param keepLocalTime 是否保持本地时间 * @returns 新的 dayjs 对象 */ export 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); };