2023-09-16 00:03:09 +08:00
|
|
|
import dayjs from 'dayjs';
|
|
|
|
import utc from 'dayjs/plugin/utc';
|
|
|
|
import timezone from 'dayjs/plugin/timezone';
|
2024-05-02 01:25:55 +08:00
|
|
|
import relativeTime from 'dayjs/plugin/relativeTime';
|
2023-10-30 00:14:59 +08:00
|
|
|
import advancedFormat from 'dayjs/plugin/advancedFormat';
|
2024-01-24 13:26:42 +00:00
|
|
|
import type { DateUnit } from '@tianji/shared';
|
2023-10-30 00:14:59 +08:00
|
|
|
|
2023-09-16 00:03:09 +08:00
|
|
|
dayjs.extend(utc);
|
|
|
|
dayjs.extend(timezone);
|
2024-05-02 01:25:55 +08:00
|
|
|
dayjs.extend(relativeTime);
|
2023-10-30 00:14:59 +08:00
|
|
|
dayjs.extend(advancedFormat);
|
2023-09-16 00:03:09 +08:00
|
|
|
|
2023-10-29 01:24:46 +08:00
|
|
|
export type { DateUnit };
|
2023-09-16 00:03:09 +08:00
|
|
|
|
|
|
|
function createDateUnitFn(unit: DateUnit) {
|
|
|
|
return {
|
|
|
|
diff: (end: dayjs.ConfigType, start: dayjs.ConfigType) =>
|
|
|
|
dayjs(end).diff(start, unit),
|
|
|
|
add: (date: dayjs.ConfigType, n: number) => dayjs(date).add(n, unit),
|
|
|
|
normalize: (date: dayjs.ConfigType) => dayjs(date).startOf(unit),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getDateArray(
|
|
|
|
data: { x: string; y: number }[],
|
|
|
|
startDate: dayjs.ConfigType,
|
|
|
|
endDate: dayjs.ConfigType,
|
|
|
|
unit: DateUnit
|
|
|
|
) {
|
|
|
|
const arr = [];
|
|
|
|
const { diff, add, normalize } = createDateUnitFn(unit);
|
|
|
|
const n = diff(endDate, startDate) + 1;
|
|
|
|
|
|
|
|
function findData(date: dayjs.Dayjs) {
|
|
|
|
const d = data.find(({ x }) => {
|
|
|
|
return normalize(dayjs(x)).unix() === date.unix();
|
|
|
|
});
|
|
|
|
|
|
|
|
return d?.y || 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
|
|
|
const t = normalize(add(startDate, i));
|
|
|
|
const y = findData(t);
|
|
|
|
|
|
|
|
arr.push({ x: formatDate(t), y });
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDate(val: dayjs.ConfigType) {
|
|
|
|
return dayjs(val).format('YYYY-MM-DD HH:mm:ss');
|
|
|
|
}
|
|
|
|
|
|
|
|
export function formatDateWithUnit(val: dayjs.ConfigType, unit: DateUnit) {
|
|
|
|
if (unit === 'minute') {
|
|
|
|
return dayjs(val).format('HH:mm');
|
|
|
|
} else if (unit === 'hour') {
|
|
|
|
return dayjs(val).format('HA');
|
|
|
|
} else if (unit === 'day') {
|
|
|
|
return dayjs(val).format('MMM DD');
|
|
|
|
} else if (unit === 'month') {
|
|
|
|
return dayjs(val).format('MMM');
|
|
|
|
} else if (unit === 'year') {
|
|
|
|
return dayjs(val).format('YYYY');
|
|
|
|
}
|
|
|
|
|
|
|
|
return formatDate(val);
|
|
|
|
}
|
2024-10-20 22:47:22 +08:00
|
|
|
|
|
|
|
function formatOffset(offset: number) {
|
|
|
|
const sign = offset >= 0 ? '+' : '-';
|
|
|
|
const absOffset = Math.abs(offset);
|
|
|
|
const hours = String(Math.floor(absOffset / 60)).padStart(2, '0');
|
|
|
|
const minutes = String(absOffset % 60).padStart(2, '0');
|
|
|
|
|
|
|
|
return `${sign}${hours}:${minutes}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getTimezoneList() {
|
|
|
|
const timezones = Intl.supportedValuesOf('timeZone');
|
|
|
|
|
|
|
|
return timezones.map((timezone) => {
|
|
|
|
const offset = dayjs().tz(timezone).utcOffset();
|
|
|
|
|
|
|
|
return {
|
|
|
|
label: `${timezone} (${formatOffset(offset)})`,
|
|
|
|
value: timezone,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|