UNPKG

@cainiaofe/cn-utils

Version:

菜鸟前端基础工具库

114 lines (113 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dayjsTz = exports.getTimezoneName = exports.getTimezoneOffset = exports.dateTimestamp = exports.dateFormat = exports.getTimezone = exports.getDateFormatByStd = exports.getDateFormatConf = void 0; var tslib_1 = require("tslib"); // import * as i18nDefault from './default'; var exec_1 = require("../common/exec"); var utils_1 = require("../common/utils"); tslib_1.__exportStar(require("./type"), exports); /** * 根据语种信息获取日期格式化配置 * @param {TLanguage | boolean=} lang 语种 * @returns {DateFormatConf | null} 格式化对象 */ function getDateFormatConf(lang) { lang = (0, utils_1.getLang)(lang); return (0, exec_1.exec)('getDateFormatConf', {}, [lang]); } exports.getDateFormatConf = getDateFormatConf; /** * 将国际标准格式兑换成本地格式 * @param {StdFormat} stdFormat 标准格式 * @param {TLanguage | boolean=} lang 语种 * @returns {string | null} */ function getDateFormatByStd(stdFormat, lang) { lang = (0, utils_1.getLang)(lang); return (0, exec_1.exec)('getDateFormatByStd', {}, [stdFormat, lang]); } exports.getDateFormatByStd = getDateFormatByStd; /** * 获取系统时区信息 * @returns {Timezone | null} */ function getTimezone() { return (0, exec_1.exec)('getTimezone', {}, []); } exports.getTimezone = getTimezone; /** * 根据当前语种&时区格式化时间戳 * @param num Timestamp * @param timezone Timezone * @param format string | FormatAutoOpts * @returns */ function dateFormat(num, timezone, format) { if (typeof format === 'object') { var period = format.period || 's'; var lang = (0, utils_1.getLang)(format.lang); format = getDateFormatConf(lang)[period]; } var opts = { timezone: timezone || getTimezone(), format: format, }; return (0, exec_1.exec)('dateFormat', {}, [num, opts]); } exports.dateFormat = dateFormat; /** * 根据当前语种&时区获取时间戳 * @param dateStr string * @param timezone FormatOpts * @param format string | FormatAutoOpts, * @returns */ function dateTimestamp(dateStr, timezone, format) { if (typeof format === 'object') { var period = format.period || 's'; var lang = (0, utils_1.getLang)(format.lang); format = getDateFormatConf(lang)[period]; } var opts = { timezone: timezone || getTimezone(), format: format, }; return (0, exec_1.exec)('dateTimestamp', {}, [dateStr, opts]); } exports.dateTimestamp = dateTimestamp; /** * 根据时间戳获取当前日期偏移量 * @param num * @param timezone * @returns */ function getTimezoneOffset(num, timezone) { timezone = timezone || getTimezone(); return (0, exec_1.exec)('getTimezoneOffset', {}, [num, timezone]); } exports.getTimezoneOffset = getTimezoneOffset; /** * 根据时间戳&时区&语种获取时区名称 * @param num * @param timezone * @param lang * @returns */ function getTimezoneName(num, timezone, lang) { timezone = timezone || getTimezone(); lang = (0, utils_1.getLang)(lang); return (0, exec_1.exec)('getTimezoneName', {}, [num, timezone, lang]); } exports.getTimezoneName = getTimezoneName; /** * 实例化时区 dayjs 对象 * @param day * @param timezone * @param keepLocalTime * @returns 新的 dayjs 对象 */ function dayjsTz(day, timezone, keepLocalTime) { timezone = timezone || getTimezone(); return (0, exec_1.exec)('dayjsTz', {}, [day, timezone, keepLocalTime]); } exports.dayjsTz = dayjsTz;