umami/src/lib/date.ts

353 lines
8.2 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,
2024-01-30 21:46:16 +00:00
subMinutes,
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,
2024-01-30 21:46:16 +00:00
differenceInCalendarWeeks,
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,
2024-01-30 21:46:16 +00:00
addWeeks,
2023-08-21 22:53:19 +00:00
subWeeks,
2024-01-30 21:46:16 +00:00
endOfMinute,
2024-08-17 06:42:26 +00:00
isSameDay,
} 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',
};
2024-01-30 21:46:16 +00:00
export const CUSTOM_FORMATS = {
'en-US': {
p: 'ha',
pp: 'h:mm:ss',
},
'fr-FR': {
'M/d': 'd/M',
'MMM d': 'd MMM',
'EEE M/d': 'EEE d/M',
},
};
const DATE_FUNCTIONS = {
minute: {
diff: differenceInMinutes,
add: addMinutes,
sub: subMinutes,
start: startOfMinute,
end: endOfMinute,
},
hour: {
diff: differenceInHours,
add: addHours,
sub: subHours,
start: startOfHour,
end: endOfHour,
},
day: {
diff: differenceInCalendarDays,
add: addDays,
sub: subDays,
start: startOfDay,
end: endOfDay,
},
week: {
diff: differenceInCalendarWeeks,
add: addWeeks,
sub: subWeeks,
start: startOfWeek,
end: endOfWeek,
},
month: {
diff: differenceInCalendarMonths,
add: addMonths,
sub: subMonths,
start: startOfMonth,
end: endOfMonth,
},
year: {
diff: differenceInCalendarYears,
add: addYears,
sub: subYears,
start: startOfYear,
end: endOfYear,
},
2023-07-26 06:59:08 +00:00
};
2020-07-28 06:52:14 +00:00
export function getTimezone() {
return moment.tz.guess();
}
2024-01-30 21:46:16 +00:00
export function parseDateValue(value: string) {
const match = value.match?.(/^(?<num>[0-9-]+)(?<unit>hour|day|week|month|year)$/);
if (!match) return null;
const { num, unit } = match.groups;
return { num: +num, unit };
2020-07-28 06:52:14 +00:00
}
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,
2023-05-29 05:28:11 +00:00
value,
2024-01-30 21:46:16 +00:00
...parseDateValue(value),
offset: 0,
unit: getMinimumUnit(startDate, endDate),
};
}
const now = new Date();
2021-11-21 01:18:25 +00:00
const dateLocale = getDateLocale(locale);
2024-01-30 21:46:16 +00:00
const { num, unit } = parseDateValue(value);
2020-07-30 06:25:52 +00:00
switch (unit) {
2024-03-20 22:29:59 +00:00
case 'hour':
return {
startDate: num ? subHours(startOfHour(now), num - 1) : startOfHour(now),
2024-03-20 22:29:59 +00:00
endDate: endOfHour(now),
offset: 0,
num: num || 1,
unit,
value,
};
2020-07-30 06:25:52 +00:00
case 'day':
return {
startDate: num ? subDays(startOfDay(now), num - 1) : startOfDay(now),
endDate: endOfDay(now),
2024-03-20 22:29:59 +00:00
unit: num ? 'day' : 'hour',
2024-01-30 21:46:16 +00:00
offset: 0,
2024-03-20 22:29:59 +00:00
num: num || 1,
2020-07-31 03:11:43 +00:00
value,
};
2024-03-20 22:29:59 +00:00
case 'week':
return {
startDate: num
? subWeeks(startOfWeek(now, { locale: dateLocale }), num - 1)
: startOfWeek(now, { locale: dateLocale }),
endDate: endOfWeek(now, { locale: dateLocale }),
2024-03-20 22:29:59 +00:00
unit: 'day',
2024-01-30 21:46:16 +00:00
offset: 0,
2024-03-20 22:29:59 +00:00
num: num || 1,
value,
};
case 'month':
return {
startDate: num ? subMonths(startOfMonth(now), num - 1) : startOfMonth(now),
endDate: endOfMonth(now),
2024-03-22 21:35:41 +00:00
unit: num ? 'month' : 'day',
2024-03-20 22:29:59 +00:00
offset: 0,
num: num || 1,
value,
};
case 'year':
return {
startDate: num ? subYears(startOfYear(now), num - 1) : startOfYear(now),
endDate: endOfYear(now),
2024-03-20 22:29:59 +00:00
unit: 'month',
offset: 0,
num: num || 1,
2020-07-31 03:11:43 +00:00
value,
2023-08-21 22:53:19 +00:00
};
}
}
2024-01-30 21:46:16 +00:00
export function getOffsetDateRange(dateRange: DateRange, increment: number) {
const { startDate, endDate, unit, num, offset, value } = dateRange;
2023-08-21 22:53:19 +00:00
2024-01-30 21:46:16 +00:00
const change = num * increment;
const { add } = DATE_FUNCTIONS[unit];
const { unit: originalUnit } = parseDateValue(value) || {};
2023-08-21 22:53:19 +00:00
2024-01-30 21:46:16 +00:00
switch (originalUnit) {
case 'day':
return {
...dateRange,
startDate: addDays(startDate, change),
endDate: addDays(endDate, change),
offset: offset + increment,
};
2023-08-21 22:53:19 +00:00
case 'week':
return {
2024-01-30 21:46:16 +00:00
...dateRange,
startDate: addWeeks(startDate, change),
endDate: addWeeks(endDate, change),
2024-01-30 21:46:16 +00:00
offset: offset + increment,
2023-08-21 22:53:19 +00:00
};
case 'month':
return {
2024-01-30 21:46:16 +00:00
...dateRange,
startDate: addMonths(startDate, change),
endDate: addMonths(endDate, change),
2024-01-30 21:46:16 +00:00
offset: offset + increment,
2023-08-21 22:53:19 +00:00
};
case 'year':
return {
2024-01-30 21:46:16 +00:00
...dateRange,
startDate: addYears(startDate, change),
endDate: addYears(endDate, change),
2024-01-30 21:46:16 +00:00
offset: offset + increment,
};
default:
return {
startDate: add(startDate, change),
endDate: add(endDate, change),
value,
unit,
num,
offset: offset + increment,
};
}
}
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';
2024-03-28 02:56:45 +00:00
} else if (differenceInCalendarMonths(endDate, startDate) <= 6) {
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 = [];
2024-01-30 21:46:16 +00:00
const { diff, add, start } = DATE_FUNCTIONS[unit];
const n = diff(endDate, startDate);
for (let i = 0; i <= n; i++) {
2024-01-30 21:46:16 +00:00
const t = start(add(startDate, i));
2024-07-09 06:41:40 +00:00
const y = data.find(({ x }) => start(new Date(x)).getTime() === t.getTime())?.y || 0;
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 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)));
}
2024-01-30 21:46:16 +00:00
export function getLocalTime(t: string | number | Date) {
return addMinutes(new Date(t), new Date().getTimezoneOffset());
}
export function getDateLength(startDate: Date, endDate: Date, unit: string | number) {
const { diff } = DATE_FUNCTIONS[unit];
return diff(endDate, startDate) + 1;
}
2024-05-24 02:35:29 +00:00
export function getCompareDate(compare: string, startDate: Date, endDate: Date) {
if (compare === 'yoy') {
return { startDate: subYears(startDate, 1), endDate: subYears(endDate, 1) };
}
const diff = differenceInMinutes(endDate, startDate);
return { startDate: subMinutes(startDate, diff), endDate: subMinutes(endDate, diff) };
}
2024-08-17 06:42:26 +00:00
export function getDayOfWeekAsDate(dayOfWeek: number) {
const startOfWeekDay = startOfWeek(new Date());
const daysToAdd = [0, 1, 2, 3, 4, 5, 6].indexOf(dayOfWeek);
let currentDate = addDays(startOfWeekDay, daysToAdd);
// Ensure we're not returning a past date
if (isSameDay(currentDate, startOfWeekDay)) {
currentDate = addDays(currentDate, 7);
}
return currentDate;
}