@aplus-frontend/utils
Version:
Utils for Aplus frontend team.
146 lines (145 loc) • 4.75 kB
JavaScript
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
dayjs.extend(utc);
dayjs.extend(timezone);
const userTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone;
var ZoneMap = /* @__PURE__ */ ((ZoneMap2) => {
ZoneMap2["RJ"] = "Atlantic/Reykjavik";
ZoneMap2["PR"] = "Europe/Paris";
ZoneMap2["VD"] = "Atlantic/Cape_Verde";
ZoneMap2["CP"] = "Europe/Copenhagen";
ZoneMap2["BS"] = "America/Sao_Paulo";
ZoneMap2["MS"] = "Europe/Moscow";
ZoneMap2["SP"] = "America/Sao_Paulo";
ZoneMap2["DB"] = "Asia/Dubai";
ZoneMap2["NY"] = "America/New_York";
ZoneMap2["KR"] = "Asia/Karachi";
ZoneMap2["CG"] = "America/Chicago";
ZoneMap2["TM"] = "Asia/Thimphu";
ZoneMap2["DV"] = "America/Denver";
ZoneMap2["SU"] = "Asia/Seoul";
ZoneMap2["LA"] = "America/Los_Angeles";
ZoneMap2["SH"] = "Asia/Shanghai";
ZoneMap2["ALS"] = "America/Juneau";
return ZoneMap2;
})(ZoneMap || {});
function getTimeFormatToZone(timeStamp, zoneAlias = "LA", format = "YYYY-MM-DD HH:mm:ss") {
try {
if (!timeStamp || !Number.isInteger(timeStamp)) {
return "--";
}
const zone = ZoneMap[zoneAlias] ? ZoneMap[zoneAlias] : zoneAlias;
const dateTime = dayjs(timeStamp).tz(zone).format(format);
if (!dateTime) {
console.error(`错误的时间格式化: ${format}`);
return "--";
}
return dateTime;
} catch (error) {
console.error(error);
return "--";
}
}
function getUTCTimeFormat(timeStamp, utc2 = -7, format = "YYYY-MM-DD HH:mm:ss") {
try {
if (!timeStamp || !Number.isInteger(timeStamp)) {
return "--";
}
if (!Number.isInteger(utc2) || utc2 < -12 || utc2 > 14) {
throw new Error(`Invalid UTC offset: ${utc2}`);
}
if ([-7].includes(utc2)) {
return getTimeFormatToZone(timeStamp, "LA", format);
}
if (utc2 === 8) {
return getTimeFormatToZone(timeStamp, "SH", format);
}
return dayjs(timeStamp).utcOffset(utc2).format(format);
} catch (error) {
console.error(error);
return null;
}
}
function getUTCTimeFormatWithZone(timeStamp, utc2 = -7, format = "YYYY-MM-DD HH:mm:ss") {
const dateTime = getUTCTimeFormat(timeStamp, utc2, format);
const utcStr = utc2 > 0 ? `+${utc2}` : `${utc2}`;
if (!timeStamp || !Number.isInteger(timeStamp)) {
return "--";
} else {
return `${dateTime} (UTC${utcStr})`;
}
}
function getUtcTimestamp(...args) {
let time = 0;
let targetZoneAlias = "LA";
let currentZoneAlias = userTimezone;
let returnDayjs = false;
if (args.length === 1 && typeof args[0] === "object" && !dayjs.isDayjs(args[0])) {
const { time: t, targetZoneAlias: tza, currentZoneAlias: cza } = args[0];
time = t;
targetZoneAlias = tza || targetZoneAlias;
currentZoneAlias = cza || currentZoneAlias;
returnDayjs = args[0].returnDayjs || returnDayjs;
} else {
time = args[0];
targetZoneAlias = args[1] || targetZoneAlias;
currentZoneAlias = args[2] || currentZoneAlias;
returnDayjs = false;
}
const targetZone = ZoneMap[targetZoneAlias] ? ZoneMap[targetZoneAlias] : targetZoneAlias;
const currentZone = ZoneMap[currentZoneAlias] ? ZoneMap[currentZoneAlias] : currentZoneAlias;
if (!time || !targetZone) {
return null;
}
if (dayjs.isDayjs(time)) {
time = time.format("YYYY-MM-DD HH:mm:ss.SSS");
}
const localTimeFormat = dayjs.tz(time, currentZone).format("YYYY-MM-DD HH:mm:ss.SSS");
if (!dayjs(localTimeFormat).isValid()) {
return null;
}
try {
let targetTime;
if (returnDayjs) {
targetTime = dayjs.tz(localTimeFormat, targetZone);
} else {
targetTime = dayjs.tz(localTimeFormat, targetZone).valueOf();
}
return targetTime;
} catch {
return null;
}
}
function getDateIntervalUnit(params) {
var _a, _b, _c, _d;
const { timezone: timezone2 = "LA", step = -3, unit = "month" } = params ?? {};
const formatTime = getUtcTimestamp({
time: getTimeFormatToZone(Date.now(), timezone2),
targetZoneAlias: timezone2,
returnDayjs: true
});
if (!formatTime) {
return null;
}
if (step > 0) {
return [
(_a = formatTime == null ? void 0 : formatTime.startOf("day")) == null ? void 0 : _a.valueOf(),
(_b = formatTime == null ? void 0 : formatTime.add(step, unit).endOf("day")) == null ? void 0 : _b.valueOf()
];
} else {
return [
(_c = formatTime == null ? void 0 : formatTime.add(step, unit).startOf("day")) == null ? void 0 : _c.valueOf(),
(_d = formatTime == null ? void 0 : formatTime.endOf("day")) == null ? void 0 : _d.valueOf()
];
}
}
export {
ZoneMap,
getDateIntervalUnit,
getTimeFormatToZone,
getUTCTimeFormat,
getUTCTimeFormatWithZone,
getUtcTimestamp,
userTimezone
};