-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
75 lines (69 loc) · 2.08 KB
/
generator.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
// Generate iCalendar format from parsed events
/*
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//hacksw/handcal//NONSGML v1.0//EN
BEGIN:VEVENT
UID:[email protected] (required)
DTSTAMP:19970714T170000Z (required - DATE-TIME that iCalendar object was created.)
DTSTART:19970714T170000Z (required)
DTEND:19970715T035959Z (this or DURATION is required)
DURATION:PT5H30M (5 hours, 30 minutes - both minutes and hours are optional)
SUMMARY:Bastille Day Party (optional)
DESCRIPTION: (optional)
END:VEVENT
END:VCALENDAR
*/
function generateCal(parsedEvents, domain = "example.com", tz = "PST") {
var sb = [];
sb.push('BEGIN:VCALENDAR');
parsedEvents.forEach((event, n) => {
sb.push('BEGIN:VEVENT');
sb.push('UID:' + mkID(event.date, n, domain));
sb.push('DTSTAMP:' + mkISO8601Time(new Date()));
sb.push('DTSTART:' + mkStartTime(event, tz));
sb.push('DURATION:PT1H');
sb.push('SUMMARY:' + event.who.trim());
sb.push('LOCATION:' + event.where.trim());
sb.push('END:VEVENT');
});
sb.push('END:VCALENDAR');
return sb.join("\n");
}
function mkID(date, count, domain) {
return [
date.replace(' ',''),
'.E',
count,
'@',
domain
].join("");
}
function mkISO8601Time(date) {
return date.toISOString().slice(0,19).replace(/[-: ]/g, "") + 'Z';
}
function mkStartTime(event, tz) {
var now = new Date();
var dateString = [
now.getYear() + 1900,
' ',
event.date.replace(/Mon|Tue|Wed|Thu|Fri|Sat|Sun/,''),
' ',
event.time.replace(/-.*/,''), // Ignore end time
' ',
now.toString().indexOf("Daylight") == -1 ? tz : tz.replace("S", "D")
].join("");
var date = Date.parse(dateString);
if (!date) {
console.error("dateString", dateString);
console.error("date", date);
return dateString;
}
date = new Date(date);
if (date.getHours() == 21) {
console.error("Adjusting midnight");
date.setDate(date.getDate() + 1);
}
return mkISO8601Time(date);
}
exports.generateCal = generateCal;