-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper.js
67 lines (59 loc) · 1.78 KB
/
helper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import dateformat from 'dateformat';
import {
endOfISOWeek,
endOfMonth,
startOfISOWeek,
startOfMonth,
startOfYear,
endOfYear,
subDays,
subMonths,
subYears
} from 'date-fns';
import { fromZonedTime } from 'date-fns-tz';
import Logger from './logging.cjs';
const log = new Logger('helper');
const dateMask = 'yyyy-mm-dd';
const getStartAndEndFromInterval = (interval, seconds) => {
let divider = seconds ? 1000 : 1;
// https://date-fns.org/v3.3.1/docs/startOfWeek
// interval: tw | lw | tm | lm
let now = fromZonedTime(new Date(), 'Europe/Berlin');
let start, end;
switch (interval) {
case 'tw':
start = startOfISOWeek(now);
end = now;
break;
case 'lw':
start = startOfISOWeek(now);
start = subDays(start, 7);
end = endOfISOWeek(start);
break;
case 'tm':
start = startOfMonth(now);
end = now;
break;
case 'lm':
start = startOfMonth(now);
start = subMonths(start, 1);
end = endOfMonth(start);
break;
case 'ty':
start = startOfYear(now);
end = now;
break;
case 'ly':
start = startOfYear(now);
start = subYears(start, 1);
end = endOfYear(start);
break;
}
let formattedStart = dateformat(start, dateMask);
let formattedEnd = dateformat(end, dateMask);
log.debug('using start:', formattedStart, 'and end:', formattedEnd);
let timeStart = parseInt(start.getTime() / divider);
let timeEnd = parseInt(end.getTime() / divider);
return [timeStart, timeEnd, formattedStart, formattedEnd];
};
export { getStartAndEndFromInterval, dateMask };