umami/lib/date.js

127 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-07-28 08:52:14 +02:00
import moment from 'moment-timezone';
import {
addMinutes,
addHours,
2020-07-31 05:11:43 +02:00
addDays,
2020-07-31 08:08:33 +02:00
addMonths,
2020-07-31 05:11:43 +02:00
subHours,
subDays,
startOfHour,
startOfDay,
2020-07-31 07:40:16 +02:00
startOfWeek,
startOfMonth,
2020-07-31 08:08:33 +02:00
startOfYear,
endOfHour,
endOfDay,
2020-07-31 07:40:16 +02:00
endOfWeek,
endOfMonth,
2020-07-31 08:08:33 +02:00
endOfYear,
differenceInHours,
2020-07-31 07:40:16 +02:00
differenceInCalendarDays,
2020-07-31 08:08:33 +02:00
differenceInMonths,
} from 'date-fns';
2020-07-28 08:52:14 +02:00
export function getTimezone() {
return moment.tz.guess();
}
2020-07-28 08:52:14 +02:00
export function getLocalTime(t) {
return addMinutes(new Date(t), new Date().getTimezoneOffset());
}
export function getDateRange(value) {
const now = new Date();
2020-07-31 08:08:33 +02:00
const { num, unit } = value.match(/^(?<num>[0-9]+)(?<unit>hour|day|week|month|year)$/).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 {
startDate: startOfWeek(now),
endDate: endOfWeek(now),
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':
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-30 08:25:52 +02:00
case 'hour':
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,
};
}
}
const dateFuncs = {
hour: [differenceInHours, addHours, startOfHour],
2020-07-31 07:40:16 +02:00
day: [differenceInCalendarDays, addDays, startOfDay],
2020-07-31 08:08:33 +02:00
month: [differenceInMonths, addMonths, startOfMonth],
};
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;
function findData(t) {
2020-08-12 05:05:40 +02:00
const x = data.find(e => {
console.log(
new Date(e.t),
getLocalTime(new Date(e.t)),
getLocalTime(new Date(e.t)).getTime(),
normalize(new Date(t)).getTime(),
);
2020-08-12 05:05:40 +02:00
return getLocalTime(new Date(e.t)).getTime() === normalize(new Date(t)).getTime();
});
2020-07-31 07:40:16 +02:00
return x?.y || 0;
}
for (let i = 0; i < n; i++) {
2020-07-31 07:40:16 +02:00
const t = add(startDate, i);
const y = findData(t);
2020-08-27 12:42:24 +02:00
arr.push({ ...data[i], t, y });
}
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;
}