umami/src/lib/date.ts

334 lines
7.5 KiB
TypeScript
Raw Normal View History

2020-07-28 06:52:14 +00:00
import moment from 'moment-timezone';
import {
addMinutes,
addHours,
2020-07-31 03:11:43 +00:00
addDays,
2020-07-31 06:08:33 +00:00
addMonths,
2020-09-13 08:38:14 +00:00
addYears,
2020-07-31 03:11:43 +00:00
subHours,
subDays,
subMonths,
subYears,
2020-10-09 06:26:05 +00:00
startOfMinute,
2020-07-31 03:11:43 +00:00
startOfHour,
startOfDay,
2020-07-31 05:40:16 +00:00
startOfWeek,
startOfMonth,
2020-07-31 06:08:33 +00:00
startOfYear,
endOfHour,
endOfDay,
2020-07-31 05:40:16 +00:00
endOfWeek,
endOfMonth,
2020-07-31 06:08:33 +00:00
endOfYear,
2020-10-09 06:26:05 +00:00
differenceInMinutes,
differenceInHours,
2020-07-31 05:40:16 +00:00
differenceInCalendarDays,
2020-09-13 08:26:54 +00:00
differenceInCalendarMonths,
2020-09-13 08:38:14 +00:00
differenceInCalendarYears,
2021-02-27 06:41:05 +00:00
format,
2023-07-26 06:59:08 +00:00
max,
min,
isDate,
2023-08-21 22:53:19 +00:00
subWeeks,
} from 'date-fns';
2021-11-21 01:18:25 +00:00
import { getDateLocale } from 'lib/lang';
2023-12-09 08:35:54 +00:00
import { DateRange } from 'lib/types';
2020-07-28 06:52:14 +00:00
2023-08-21 22:53:19 +00:00
export const TIME_UNIT = {
minute: 'minute',
hour: 'hour',
day: 'day',
week: 'week',
month: 'month',
year: 'year',
};
2023-07-26 06:59:08 +00:00
const dateFuncs = {
minute: [differenceInMinutes, addMinutes, startOfMinute],
hour: [differenceInHours, addHours, startOfHour],
day: [differenceInCalendarDays, addDays, startOfDay],
month: [differenceInCalendarMonths, addMonths, startOfMonth],
year: [differenceInCalendarYears, addYears, startOfYear],
};
2020-07-28 06:52:14 +00:00
export function getTimezone() {
return moment.tz.guess();
}
2023-12-09 08:35:54 +00:00
export function getLocalTime(t: string | number | Date) {
2020-07-28 06:52:14 +00:00
return addMinutes(new Date(t), new Date().getTimezoneOffset());
}
2023-12-09 08:35:54 +00:00
export function parseDateRange(value: string | object, locale = 'en-US'): DateRange {
if (typeof value === 'object') {
2023-12-09 08:35:54 +00:00
return value as DateRange;
2023-05-29 05:28:11 +00:00
}
2023-07-25 22:17:50 +00:00
if (value === 'all') {
return {
startDate: new Date(0),
endDate: new Date(1),
value,
};
}
2023-05-29 05:28:11 +00:00
if (value?.startsWith?.('range')) {
2023-07-26 16:55:54 +00:00
const [, startTime, endTime] = value.split(':');
2023-05-29 05:28:11 +00:00
2023-07-26 16:55:54 +00:00
const startDate = new Date(+startTime);
const endDate = new Date(+endTime);
2023-05-29 05:28:11 +00:00
return {
startDate,
endDate,
unit: getMinimumUnit(startDate, endDate),
2023-05-29 05:28:11 +00:00
value,
};
}
const now = new Date();
2021-11-21 01:18:25 +00:00
const dateLocale = getDateLocale(locale);
2023-05-29 05:28:11 +00:00
const match = value?.match?.(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
2021-12-20 06:49:08 +00:00
2023-05-29 05:28:11 +00:00
if (!match) return null;
2021-12-20 06:49:08 +00:00
const { num, unit } = match.groups;
2023-12-09 08:35:54 +00:00
const selectedUnit = { num: +num, unit };
2020-07-31 05:40:16 +00:00
if (+num === 1) {
switch (unit) {
case 'day':
return {
startDate: startOfDay(now),
endDate: endOfDay(now),
unit: 'hour',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
2020-07-31 05:40:16 +00:00
};
case 'week':
return {
2021-11-05 00:09:03 +00:00
startDate: startOfWeek(now, { locale: dateLocale }),
endDate: endOfWeek(now, { locale: dateLocale }),
2020-07-31 05:40:16 +00:00
unit: 'day',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
2020-07-31 05:40:16 +00:00
};
case 'month':
return {
startDate: startOfMonth(now),
endDate: endOfMonth(now),
unit: 'day',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
2020-07-31 05:40:16 +00:00
};
2020-07-31 06:08:33 +00:00
case 'year':
return {
startDate: startOfYear(now),
endDate: endOfYear(now),
unit: 'month',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
2020-07-31 06:08:33 +00:00
};
2020-07-31 05:40:16 +00:00
}
}
2020-07-30 06:25:52 +00:00
if (+num === -1) {
switch (unit) {
case 'day':
return {
startDate: subDays(startOfDay(now), 1),
endDate: subDays(endOfDay(now), 1),
unit: 'hour',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
case 'week':
return {
startDate: subDays(startOfWeek(now, { locale: dateLocale }), 7),
endDate: subDays(endOfWeek(now, { locale: dateLocale }), 1),
unit: 'day',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
case 'month':
return {
startDate: subMonths(startOfMonth(now), 1),
endDate: subMonths(endOfMonth(now), 1),
unit: 'day',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
case 'year':
return {
startDate: subYears(startOfYear(now), 1),
endDate: subYears(endOfYear(now), 1),
unit: 'month',
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
}
}
2020-07-30 06:25:52 +00:00
switch (unit) {
case 'day':
return {
2023-12-09 08:35:54 +00:00
startDate: subDays(startOfDay(now), +num - 1),
2020-07-31 05:40:16 +00:00
endDate: endOfDay(now),
2020-07-30 06:25:52 +00:00
unit,
2020-07-31 03:11:43 +00:00
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
2020-07-30 06:25:52 +00:00
case 'hour':
return {
2023-12-09 08:35:54 +00:00
startDate: subHours(startOfHour(now), +num - 1),
2020-07-31 05:40:16 +00:00
endDate: endOfHour(now),
2020-07-30 06:25:52 +00:00
unit,
2020-07-31 03:11:43 +00:00
value,
2023-08-21 22:53:19 +00:00
selectedUnit,
};
}
}
2023-12-09 08:35:54 +00:00
export function incrementDateRange(
value: { startDate: any; endDate: any; selectedUnit: any },
increment: number,
) {
2023-08-21 22:53:19 +00:00
const { startDate, endDate, selectedUnit } = value;
const { num, unit } = selectedUnit;
const sub = Math.abs(num) * increment;
2023-08-21 22:53:19 +00:00
switch (unit) {
2023-08-22 19:32:36 +00:00
case 'hour':
return {
...value,
startDate: subHours(startDate, sub),
endDate: subHours(endDate, sub),
value: 'range',
};
2023-08-21 22:53:19 +00:00
case 'day':
return {
...value,
startDate: subDays(startDate, sub),
endDate: subDays(endDate, sub),
value: 'range',
};
case 'week':
return {
...value,
startDate: subWeeks(startDate, sub),
endDate: subWeeks(endDate, sub),
value: 'range',
};
case 'month':
return {
...value,
startDate: subMonths(startDate, sub),
endDate: subMonths(endDate, sub),
value: 'range',
};
case 'year':
return {
...value,
startDate: subYears(startDate, sub),
endDate: subYears(endDate, sub),
value: 'range',
};
}
}
2023-12-09 08:35:54 +00:00
export function getAllowedUnits(startDate: Date, endDate: Date) {
2023-07-25 22:17:50 +00:00
const units = ['minute', 'hour', 'day', 'month', 'year'];
2023-07-26 06:59:08 +00:00
const minUnit = getMinimumUnit(startDate, endDate);
2023-10-13 21:07:55 +00:00
const index = units.indexOf(minUnit === 'year' ? 'month' : minUnit);
2023-07-25 22:17:50 +00:00
2023-07-26 06:59:08 +00:00
return index >= 0 ? units.splice(index) : [];
2023-07-25 22:17:50 +00:00
}
2023-12-09 08:35:54 +00:00
export function getMinimumUnit(startDate: number | Date, endDate: number | Date) {
2023-07-25 22:17:50 +00:00
if (differenceInMinutes(endDate, startDate) <= 60) {
return 'minute';
} else if (differenceInHours(endDate, startDate) <= 48) {
return 'hour';
2023-10-03 22:50:33 +00:00
} else if (differenceInCalendarMonths(endDate, startDate) <= 12) {
2023-07-25 22:17:50 +00:00
return 'day';
2020-09-13 08:38:14 +00:00
} else if (differenceInCalendarMonths(endDate, startDate) <= 24) {
2023-07-25 22:17:50 +00:00
return 'month';
2020-09-13 08:26:54 +00:00
}
2023-07-25 22:17:50 +00:00
return 'year';
}
2023-12-09 08:35:54 +00:00
export function getDateFromString(str: string) {
2020-09-23 15:22:40 +00:00
const [ymd, hms] = str.split(' ');
const [year, month, day] = ymd.split('-');
if (hms) {
const [hour, min, sec] = hms.split(':');
2023-12-09 08:35:54 +00:00
return new Date(+year, +month - 1, +day, +hour, +min, +sec);
2020-09-23 15:22:40 +00:00
}
2023-12-09 08:35:54 +00:00
return new Date(+year, +month - 1, +day);
2020-09-23 15:22:40 +00:00
}
2023-12-09 08:35:54 +00:00
export function getDateArray(data: any[], startDate: Date, endDate: Date, unit: string) {
const arr = [];
const [diff, add, normalize] = dateFuncs[unit];
2020-07-31 05:40:16 +00:00
const n = diff(endDate, startDate) + 1;
2023-12-09 08:35:54 +00:00
function findData(date: Date) {
2023-04-10 16:44:25 +00:00
const d = data.find(({ x }) => {
return normalize(getDateFromString(x)).getTime() === date.getTime();
2020-08-12 03:05:40 +00:00
});
2020-07-31 05:40:16 +00:00
return d?.y || 0;
}
for (let i = 0; i < n; i++) {
const t = normalize(add(startDate, i));
const y = findData(t);
arr.push({ x: t, y });
}
return arr;
}
2020-08-27 20:46:05 +00:00
2023-12-09 08:35:54 +00:00
export function getDateLength(startDate: Date, endDate: Date, unit: string | number) {
2020-08-27 20:46:05 +00:00
const [diff] = dateFuncs[unit];
return diff(endDate, startDate) + 1;
}
2021-02-27 06:41:05 +00:00
2023-08-21 22:53:19 +00:00
export const CUSTOM_FORMATS = {
2021-02-27 06:41:05 +00:00
'en-US': {
p: 'ha',
pp: 'h:mm:ss',
},
2022-11-07 18:38:43 +00:00
'fr-FR': {
'M/d': 'd/M',
'MMM d': 'd MMM',
'EEE M/d': 'EEE d/M',
},
2021-02-27 06:41:05 +00:00
};
2023-12-09 08:35:54 +00:00
export function formatDate(date: string | number | Date, str: string, locale = 'en-US') {
2023-08-15 05:36:18 +00:00
return format(
typeof date === 'string' ? new Date(date) : date,
2023-08-21 22:53:19 +00:00
CUSTOM_FORMATS?.[locale]?.[str] || str,
2023-08-15 05:36:18 +00:00
{
locale: getDateLocale(locale),
},
);
2021-02-27 06:41:05 +00:00
}
2023-07-26 06:59:08 +00:00
2023-12-09 08:35:54 +00:00
export function maxDate(...args: Date[]) {
2023-07-26 06:59:08 +00:00
return max(args.filter(n => isDate(n)));
}
2023-12-09 08:35:54 +00:00
export function minDate(...args: any[]) {
2023-07-26 06:59:08 +00:00
return min(args.filter(n => isDate(n)));
}