2020-07-28 08:52:14 +02:00
|
|
|
import moment from 'moment-timezone';
|
2020-07-29 04:04:45 +02:00
|
|
|
import {
|
|
|
|
addMinutes,
|
|
|
|
addHours,
|
2020-07-31 05:11:43 +02:00
|
|
|
addDays,
|
2020-07-31 08:08:33 +02:00
|
|
|
addMonths,
|
2020-09-13 10:38:14 +02:00
|
|
|
addYears,
|
2020-07-31 05:11:43 +02:00
|
|
|
subHours,
|
|
|
|
subDays,
|
2020-10-09 08:26:05 +02:00
|
|
|
startOfMinute,
|
2020-07-31 05:11:43 +02:00
|
|
|
startOfHour,
|
2020-07-29 04:04:45 +02:00
|
|
|
startOfDay,
|
2020-07-31 07:40:16 +02:00
|
|
|
startOfWeek,
|
|
|
|
startOfMonth,
|
2020-07-31 08:08:33 +02:00
|
|
|
startOfYear,
|
2020-07-29 04:04:45 +02:00
|
|
|
endOfHour,
|
|
|
|
endOfDay,
|
2020-07-31 07:40:16 +02:00
|
|
|
endOfWeek,
|
|
|
|
endOfMonth,
|
2020-07-31 08:08:33 +02:00
|
|
|
endOfYear,
|
2020-10-09 08:26:05 +02:00
|
|
|
differenceInMinutes,
|
2020-07-29 04:04:45 +02:00
|
|
|
differenceInHours,
|
2020-07-31 07:40:16 +02:00
|
|
|
differenceInCalendarDays,
|
2020-09-13 10:26:54 +02:00
|
|
|
differenceInCalendarMonths,
|
2020-09-13 10:38:14 +02:00
|
|
|
differenceInCalendarYears,
|
2021-02-27 07:41:05 +01:00
|
|
|
format,
|
2020-07-29 04:04:45 +02:00
|
|
|
} from 'date-fns';
|
2021-11-21 02:18:25 +01:00
|
|
|
import { getDateLocale } from 'lib/lang';
|
2020-07-28 08:52:14 +02:00
|
|
|
|
|
|
|
export function getTimezone() {
|
2020-07-29 04:04:45 +02:00
|
|
|
return moment.tz.guess();
|
|
|
|
}
|
|
|
|
|
2020-07-28 08:52:14 +02:00
|
|
|
export function getLocalTime(t) {
|
|
|
|
return addMinutes(new Date(t), new Date().getTimezoneOffset());
|
|
|
|
}
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2021-03-10 10:40:59 +01:00
|
|
|
export function getDateRange(value, locale = 'en-US') {
|
2020-07-28 10:17:45 +02:00
|
|
|
const now = new Date();
|
2021-11-21 02:18:25 +01:00
|
|
|
const dateLocale = getDateLocale(locale);
|
2020-07-28 10:17:45 +02:00
|
|
|
|
2021-12-20 07:49:08 +01:00
|
|
|
const match = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/);
|
|
|
|
|
|
|
|
if (!match) return;
|
|
|
|
|
|
|
|
const { num, unit } = match.groups;
|
2020-07-31 07:40:16 +02:00
|
|
|
|
|
|
|
if (+num === 1) {
|
|
|
|
switch (unit) {
|
|
|
|
case 'day':
|
|
|
|
return {
|
|
|
|
startDate: startOfDay(now),
|
|
|
|
endDate: endOfDay(now),
|
|
|
|
unit: 'hour',
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
case 'week':
|
|
|
|
return {
|
2021-11-05 01:09:03 +01:00
|
|
|
startDate: startOfWeek(now, { locale: dateLocale }),
|
|
|
|
endDate: endOfWeek(now, { locale: dateLocale }),
|
2020-07-31 07:40:16 +02:00
|
|
|
unit: 'day',
|
|
|
|
value,
|
|
|
|
};
|
|
|
|
case 'month':
|
|
|
|
return {
|
|
|
|
startDate: startOfMonth(now),
|
|
|
|
endDate: endOfMonth(now),
|
|
|
|
unit: 'day',
|
|
|
|
value,
|
|
|
|
};
|
2020-07-31 08:08:33 +02:00
|
|
|
case 'year':
|
|
|
|
return {
|
|
|
|
startDate: startOfYear(now),
|
|
|
|
endDate: endOfYear(now),
|
|
|
|
unit: 'month',
|
|
|
|
value,
|
|
|
|
};
|
2020-07-31 07:40:16 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-30 08:25:52 +02:00
|
|
|
|
|
|
|
switch (unit) {
|
|
|
|
case 'day':
|
2020-07-28 10:17:45 +02:00
|
|
|
return {
|
2020-07-31 07:40:16 +02:00
|
|
|
startDate: subDays(startOfDay(now), num - 1),
|
|
|
|
endDate: endOfDay(now),
|
2020-07-30 08:25:52 +02:00
|
|
|
unit,
|
2020-07-31 05:11:43 +02:00
|
|
|
value,
|
2020-07-28 10:17:45 +02:00
|
|
|
};
|
2020-07-30 08:25:52 +02:00
|
|
|
case 'hour':
|
2020-07-28 10:17:45 +02:00
|
|
|
return {
|
2020-07-31 07:40:16 +02:00
|
|
|
startDate: subHours(startOfHour(now), num - 1),
|
|
|
|
endDate: endOfHour(now),
|
2020-07-30 08:25:52 +02:00
|
|
|
unit,
|
2020-07-31 05:11:43 +02:00
|
|
|
value,
|
2020-07-28 10:17:45 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-07-29 04:04:45 +02:00
|
|
|
|
2020-09-13 10:26:54 +02:00
|
|
|
export function getDateRangeValues(startDate, endDate) {
|
2020-09-13 20:33:57 +02:00
|
|
|
let unit = 'year';
|
2020-09-17 01:28:54 +02:00
|
|
|
if (differenceInHours(endDate, startDate) <= 48) {
|
2020-09-13 20:33:57 +02:00
|
|
|
unit = 'hour';
|
2020-09-13 10:26:54 +02:00
|
|
|
} else if (differenceInCalendarDays(endDate, startDate) <= 90) {
|
2020-09-13 20:33:57 +02:00
|
|
|
unit = 'day';
|
2020-09-13 10:38:14 +02:00
|
|
|
} else if (differenceInCalendarMonths(endDate, startDate) <= 24) {
|
2020-09-13 20:33:57 +02:00
|
|
|
unit = 'month';
|
2020-09-13 10:26:54 +02:00
|
|
|
}
|
2020-09-13 20:33:57 +02:00
|
|
|
|
|
|
|
return { startDate: startOfDay(startDate), endDate: endOfDay(endDate), unit };
|
2020-09-13 10:26:54 +02:00
|
|
|
}
|
|
|
|
|
2020-09-23 17:22:40 +02:00
|
|
|
export function getDateFromString(str) {
|
|
|
|
const [ymd, hms] = str.split(' ');
|
|
|
|
const [year, month, day] = ymd.split('-');
|
|
|
|
|
|
|
|
if (hms) {
|
|
|
|
const [hour, min, sec] = hms.split(':');
|
|
|
|
|
|
|
|
return new Date(year, month - 1, day, hour, min, sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new Date(year, month - 1, day);
|
|
|
|
}
|
|
|
|
|
2020-07-29 04:04:45 +02:00
|
|
|
const dateFuncs = {
|
2020-10-09 08:26:05 +02:00
|
|
|
minute: [differenceInMinutes, addMinutes, startOfMinute],
|
2020-07-29 04:04:45 +02:00
|
|
|
hour: [differenceInHours, addHours, startOfHour],
|
2020-07-31 07:40:16 +02:00
|
|
|
day: [differenceInCalendarDays, addDays, startOfDay],
|
2020-09-13 10:26:54 +02:00
|
|
|
month: [differenceInCalendarMonths, addMonths, startOfMonth],
|
2020-09-13 10:38:14 +02:00
|
|
|
year: [differenceInCalendarYears, addYears, startOfYear],
|
2020-07-29 04:04:45 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export function getDateArray(data, startDate, endDate, unit) {
|
|
|
|
const arr = [];
|
|
|
|
const [diff, add, normalize] = dateFuncs[unit];
|
2020-07-31 07:40:16 +02:00
|
|
|
const n = diff(endDate, startDate) + 1;
|
2020-07-29 04:04:45 +02:00
|
|
|
|
|
|
|
function findData(t) {
|
2020-08-12 05:05:40 +02:00
|
|
|
const x = data.find(e => {
|
2020-09-23 17:22:40 +02:00
|
|
|
return normalize(getDateFromString(e.t)).getTime() === t.getTime();
|
2020-08-12 05:05:40 +02:00
|
|
|
});
|
2020-07-31 07:40:16 +02:00
|
|
|
|
|
|
|
return x?.y || 0;
|
2020-07-29 04:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < n; i++) {
|
2020-08-30 08:17:32 +02:00
|
|
|
const t = normalize(add(startDate, i));
|
2020-07-29 04:04:45 +02:00
|
|
|
const y = findData(t);
|
|
|
|
|
2020-08-27 12:42:24 +02:00
|
|
|
arr.push({ ...data[i], t, y });
|
2020-07-29 04:04:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return arr;
|
|
|
|
}
|
2020-08-27 22:46:05 +02:00
|
|
|
|
|
|
|
export function getDateLength(startDate, endDate, unit) {
|
|
|
|
const [diff] = dateFuncs[unit];
|
|
|
|
return diff(endDate, startDate) + 1;
|
|
|
|
}
|
2021-02-27 07:41:05 +01:00
|
|
|
|
|
|
|
export const customFormats = {
|
|
|
|
'en-US': {
|
|
|
|
p: 'ha',
|
|
|
|
pp: 'h:mm:ss',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
export function dateFormat(date, str, locale = 'en-US') {
|
|
|
|
return format(date, customFormats?.[locale]?.[str] || str, {
|
2021-11-21 02:18:25 +01:00
|
|
|
locale: getDateLocale(locale),
|
2021-02-27 07:41:05 +01:00
|
|
|
});
|
|
|
|
}
|