-
Notifications
You must be signed in to change notification settings - Fork 9
/
rrule-tests.js
147 lines (118 loc) · 4.52 KB
/
rrule-tests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
const { RRule } = require('rrule');
const keys = require('./server/v1/config/keys');
const moment = require('moment-timezone');
console.log('current date: ', new Date());
function returnDatesArrayByRepeatRule(event, startDate, endDate) {
const rule = new RRule(createRRule(event));
try {
let eventDuration = event.end_time_utc - event.start_time_utc;
// rule.between requires that our start date and end date be adjusted by the difference between the station's timezone offset and our server timezone's offset
// this isn't documented, so far I've found it out by trial and error
// prior to 6/9/2020, I was subtracting an hour from adjustedStartDate and adding an hour to adjustedEndDate because I thought it was a DST quirk with rrule
// the combineDayandTime function is also responsible for what gets returned on the front end: and maybe that is why this is so complicated? Maybe it could be easier if it wasn't done that way.
// tested with:
// 1. Go to the dashboard while testing locally in Central Time and be sure the most recent preceding show appears in the Show Schedule on the right
// 2. Do the same with the server running in UTC
// 3. Go into a Show Builder for Morning Sound Alternative and Afternoon Sound Alternative. Ensure traffic events show in the first and last hour of the program.
// 4. Do the same with the server running in UTC
// 5. On Show Calendar, go to the DST cutoff for the year and be sure the show times are correct before and after the cutoff
// 6. Do the same with the server running in UTC
let minutesOffset =
moment.tz.zone(keys.stationTimeZone).utcOffset(moment(event.end_time_utc)) -
new Date().getTimezoneOffset();
let adjustedStartDate = new Date(
moment(startDate).add(
-1 * eventDuration - 1 * 1000 * minutesOffset * 60,
'milliseconds',
),
); //between searches on START times, and we want to get anything in progress in this date range, so subtract the event duration from the start time
let adjustedEndDate = new Date(
moment(endDate).add(minutesOffset, 'minutes')
);
console.log(minutesOffset, adjustedStartDate, adjustedEndDate)
let events = rule.between(adjustedStartDate, adjustedEndDate);
console.log('events OG test', events);
events = rule.between(new Date('2023-11-05T02:00:00.000Z'), new Date('2023-11-05T03:59:00.000Z'));
console.log('events test 2', events);
// undo the minutes offset
for (let i = 0; i < events.length; i++) {
events[i] = moment(events[i])
.subtract(minutesOffset, 'minute')
.format();
}
return events;
} catch (e) {
console.log('Error in returnDatesArrayByRepeatRule');
console.log(e);
return null;
}
}
function createRRule(event) {
const { replace_event_date } = event;
const {
frequency,
repeat_start_date,
repeat_end_date,
interval,
count,
byweekday,
bymonth,
bysetpos,
bymonthday,
} = event.repeat_rule;
let newRRule = {};
if (frequency) {
newRRule.freq = frequency;
}
if (repeat_start_date != null) {
newRRule.dtstart = new Date(repeat_start_date);
} else if (replace_event_date != null) {
newRRule.dtstart = new Date(replace_event_date);
}
if (repeat_end_date != null) {
newRRule.until = new Date(repeat_end_date);
} else if (replace_event_date != null) {
newRRule.until = new Date(replace_event_date);
}
if (count) {
newRRule.count = count;
}
if (interval) {
newRRule.interval = interval;
}
if (byweekday) {
newRRule.byweekday = byweekday.map(day => {
return RRule[day];
});
}
if (bymonth) {
newRRule.bymonth = [bymonth];
}
if (bysetpos) {
newRRule.bysetpos = [bysetpos];
}
if (bymonthday) {
newRRule.bymonthday = [bymonthday];
}
newRRule.tzid = keys.stationTimeZone;
return newRRule;
}
console.log('testing https://comrad.kgnu.org/api/v1/events/shows?startDate=2023-11-05T02:00:00.000Z&endDate=2023-11-05T02:59:00.000Z');
let events = returnDatesArrayByRepeatRule({
"repeat_rule": {
byweekday: [
'SU'
],
frequency: 2,
repeat_start_date: '2023-11-05T02:00:00.000Z',
repeat_end_date: '2023-11-05T03:00:00.000Z',
count: null,
interval: 1,
bymonth: null,
bysetpos: null,
bymonthday: null
},
start_time_utc: '2023-11-05T02:00:00Z',
end_time_utc: '2023-11-05T03:00:00Z'
}, '2023-11-05T02:00:00.000Z', '2023-11-05T02:59:00.000Z');
console.log('events ... should have one:', events);