This repository has been archived by the owner on Feb 10, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
CalendarUtils.ts
442 lines (371 loc) · 20.2 KB
/
CalendarUtils.ts
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
// Copyright (c) 2022. Heusala Group Oy <info@heusalagroup.fi>. All rights reserved.
import { CalendarDTO, createCalendarDTO } from "./types/CalendarDTO";
import { endsWith } from "./functions/endsWith";
import { filter } from "./functions/filter";
import { find } from "./functions/find";
import { map } from "./functions/map";
import { reduce } from "./functions/reduce";
import { split } from "./functions/split";
import { LogService } from "./LogService";
import { createInternetCalendarLine, InternetCalendarLine, isInternetCalendarLine } from "./types/InternetCalendarLine";
import { createInternetCalendarParam, InternetCalendarParam } from "./types/InternetCalendarParam";
import { CalendarEvent, createCalendarEvent } from "./types/CalendarEvent";
import { momentTz, parseUtc } from "./modules/moment";
import { isArray } from "./types/Array";
const LOG = LogService.createLogger('CalendarUtils');
export type ReadonlyInternetCalendarLineList = readonly InternetCalendarLine[];
export type ReadonlyInternetCalendarLineBlockList = readonly (InternetCalendarLine | ReadonlyInternetCalendarLineList)[];
export type InternetCalendarLineList = InternetCalendarLine[];
export type InternetCalendarLineBlockList = (InternetCalendarLine | InternetCalendarLineList)[];
/**
* See also `TimeService`
*/
export class CalendarUtils {
/**
*
* @param line
*/
public static parseInternetCalendarLine (line : string) : InternetCalendarLine {
const index = line.indexOf(':');
if (index < 0) {
throw new TypeError(`CalendarUtils.parseInternetCalendarLine: No name found`)
}
const nameParams = line.substring(0, index);
const value = line.substring(index+1);
const paramIndex = nameParams.indexOf(';');
if (paramIndex < 0) {
return createInternetCalendarLine(
nameParams,
value
);
} else {
const parts : string[] = split(nameParams, ';');
const name : string = parts.shift() ?? '';
return createInternetCalendarLine(
name,
value,
map(parts, (item : string) : InternetCalendarParam => {
const itemParts = split(item, '=');
const paramName : string = itemParts.shift() ?? '';
const paramValue : string = itemParts.join('=');
return createInternetCalendarParam(paramName, paramValue);
})
);
}
}
/**
* Parses raw Internet Calendar (RFC 5545) text lines.
*
* Lines split on multiple lines will be unfold as one.
*
* @fixme From the RFC: "It is possible for very simple implementations to generate
* improperly folded lines in the middle of a UTF-8 multi-octet
* sequence. For this reason, implementations need to unfold lines
* in such a way to properly restore the original sequence."
*
* @param rows
*/
public static unfoldInternetCalendarLines (rows : readonly string[]) : readonly string[] {
return reduce(
rows,
(list: string[], row: string) : string[] => {
if (row.length) {
// // Remove possible CR
// if ( row[row.length-1] === '\r' ) {
// row = row.substring(0, row.length - 1);
// }
// Detect split lines
if (/^\s/.test(row)) {
if (list.length) {
list[list.length - 1] += row.substring(1);
} else {
throw new TypeError(`Unexpected leading white space at: ` + row);
}
} else {
list.push(row);
}
} else {
LOG.warn(`Warning: Empty line parsed at parseInternetCalendarRows().`);
}
return list;
},
[]
) as readonly string[];
}
public static groupInternetCalendarLines (value : ReadonlyInternetCalendarLineList) : ReadonlyInternetCalendarLineBlockList {
const stack : InternetCalendarLineBlockList[] = [];
return reduce(
value,
(list: InternetCalendarLineBlockList, item: InternetCalendarLine) : InternetCalendarLineBlockList => {
const itemName = item.name;
if (itemName === 'BEGIN') {
const block = [
item
];
stack.push(block);
list.push(block);
LOG.debug(`Started block "${item.value}"`);
return list;
}
if (itemName === 'END') {
const blockName = item.value;
if (stack.length === 0) {
const block = [
item
];
list.push(block);
LOG.warn(`Warning! Ended block "${blockName}" without previous START`);
return list;
}
const lastBlock : InternetCalendarLineBlockList = stack[stack.length - 1];
const lastBegin : InternetCalendarLine | InternetCalendarLineList | undefined = lastBlock.length ? lastBlock[0] : undefined;
if (!isInternetCalendarLine(lastBegin)) {
throw new TypeError(`CalendarUtils.groupInternetCalendarLines: Could not detect BEGIN for END (${blockName})`);
}
if (lastBegin.value !== blockName) {
throw new TypeError(`CalendarUtils.groupInternetCalendarLines: Found wrong block for END (${blockName})`);
}
lastBlock.push(item);
stack.pop();
LOG.debug(`Ended block "${blockName}"`);
return list;
}
if (stack.length >= 1) {
stack[stack.length - 1].push(item);
return list;
}
list.push(item);
return list;
},
[]
) as ReadonlyInternetCalendarLineBlockList;
}
public static parseCalendarDTOFromInternetCalendar (value : string) : CalendarDTO {
const foldRows = split(value, /\r?\n/);
const unfoldRows = CalendarUtils.unfoldInternetCalendarLines(foldRows);
const parsedRows : InternetCalendarLine[] = map(unfoldRows, (item : string) : InternetCalendarLine => CalendarUtils.parseInternetCalendarLine(item))
const grouped : ReadonlyInternetCalendarLineBlockList = CalendarUtils.groupInternetCalendarLines(parsedRows);
// LOG.debug(`grouped = `, grouped);
const eventBlocks : ReadonlyInternetCalendarLineList[] = filter(grouped, (item : InternetCalendarLine | ReadonlyInternetCalendarLineList) : boolean => {
if (isArray(item) && item.length) {
const itemType = item[0].value;
return itemType === 'VEVENT';
} else {
return false;
}
}) as ReadonlyInternetCalendarLineList[];
const events : CalendarEvent[] = map(
eventBlocks,
(item: ReadonlyInternetCalendarLineList) : CalendarEvent => {
return CalendarUtils.parseCalendarEventFromInternetCalendarLines(item);
}
);
return createCalendarDTO(events);
}
public static parseCalendarEventFromInternetCalendarLines (list : ReadonlyInternetCalendarLineList) : CalendarEvent {
// LOG.debug(`parseCalendarEventFromInternetCalendarLines: `, list);
const start : string = CalendarUtils._findCalendarLineAsTime('DTSTART' , list) ?? '';
const end : string = CalendarUtils._findCalendarLineAsTime('DTEND' , list) ?? '';
const repeatRule : string = CalendarUtils._findCalendarLine('RRULE' , list)?.value ?? '';
const stamp : string = CalendarUtils._findCalendarLineAsTime('DTSTAMP' , list) ?? '';
const uid : string = CalendarUtils._findCalendarLine('UID' , list)?.value ?? '';
const created : string = CalendarUtils._findCalendarLineAsTime('CREATED' , list) ?? '';
const description : string = CalendarUtils._findCalendarLine('DESCRIPTION' , list)?.value ?? '';
const lastModified : string = CalendarUtils._findCalendarLineAsTime('LAST-MODIFIED' , list) ?? '';
const location : string = CalendarUtils._findCalendarLine('LOCATION' , list)?.value ?? '';
const sequence : string = CalendarUtils._findCalendarLine('SEQUENCE' , list)?.value ?? '';
const status : string = CalendarUtils._findCalendarLine('STATUS' , list)?.value ?? '';
const summary : string = CalendarUtils._findCalendarLine('SUMMARY' , list)?.value ?? '';
const transparency : string = CalendarUtils._findCalendarLine('TRANSP' , list)?.value ?? '';
// FIXME: Print any keys which were not parsed as a warning line
const event = createCalendarEvent(
start,
end,
repeatRule,
stamp,
uid,
created,
description,
lastModified,
location,
sequence,
status,
summary,
transparency
);
// LOG.debug(`parseCalendarEventFromInternetCalendarLines: event = `, event);
return event;
}
private static _findCalendarLine (name: string, list : ReadonlyInternetCalendarLineList) : InternetCalendarLine | undefined {
return find(
list,
(item : InternetCalendarLine) : boolean => item.name === name
);
}
private static _findCalendarLineAsTime (name: string, list : ReadonlyInternetCalendarLineList) : string | undefined {
const item : InternetCalendarLine | undefined = CalendarUtils._findCalendarLine(name, list);
if (item) {
const valueLine : InternetCalendarParam | undefined = find(
item.params,
(param: InternetCalendarParam) : boolean => param.name === 'VALUE'
);
const tzId : InternetCalendarParam | undefined = find(
item.params,
(param: InternetCalendarParam) : boolean => param.name === 'TZID'
);
if (tzId) {
const tzValue = CalendarUtils.parseWindowsTimeZoneToIANA(tzId.value) ?? tzId.value;
return momentTz(item.value, tzValue).toISOString();
}
if (endsWith(item.value, 'Z')) {
return parseUtc(item.value).toISOString();
}
if (valueLine && valueLine.value === 'DATE') {
return parseUtc(item.value).toISOString();
}
LOG.debug(`Unknown item format: "${item.value}": `, item);
return item.value;
} else {
return undefined;
}
}
public static parseWindowsTimeZoneToIANA (value : string) : string | undefined {
switch(`${value}`.toLowerCase()) {
case "dateline standard time": return "Etc/GMT+12";
case "utc-11": return "Etc/GMT+11";
case "aleutian standard time": return "America/Adak";
case "hawaiian standard time": return "Pacific/Honolulu";
case "marquesas standard time": return "Pacific/Marquesas";
case "alaskan standard time": return "America/Anchorage";
case "utc-09": return "Etc/GMT+9";
case "pacific standard time (mexico)": return "America/Tijuana";
case "utc-08": return "Etc/GMT+8";
case "pacific standard time": return "America/Los_Angeles";
case "us mountain standard time": return "America/Phoenix";
case "mountain standard time (mexico)": return "America/Chihuahua";
case "mountain standard time": return "America/Denver";
case "central america standard time": return "America/Guatemala";
case "central standard time": return "America/Chicago";
case "easter island standard time": return "Pacific/Easter";
case "central standard time (mexico)": return "America/Mexico_City";
case "canada central standard time": return "America/Regina";
case "sa pacific standard time": return "America/Bogota";
case "eastern standard time (mexico)": return "America/Cancun";
case "eastern standard time": return "America/New_York";
case "haiti standard time": return "America/Port-au-Prince";
case "cuba standard time": return "America/Havana";
case "us eastern standard time": return "America/Indianapolis";
case "paraguay standard time": return "America/Asuncion";
case "atlantic standard time": return "America/Halifax";
case "venezuela standard time": return "America/Caracas";
case "central brazilian standard time": return "America/Cuiaba";
case "sa western standard time": return "America/La_Paz";
case "pacific sa standard time": return "America/Santiago";
case "turks and caicos standard time": return "America/Grand_Turk";
case "newfoundland standard time": return "America/St_Johns";
case "tocantins standard time": return "America/Araguaina";
case "e. south america standard time": return "America/Sao_Paulo";
case "sa eastern standard time": return "America/Cayenne";
case "argentina standard time": return "America/Buenos_Aires";
case "greenland standard time": return "America/Godthab";
case "montevideo standard time": return "America/Montevideo";
case "magallanes standard time": return "America/Punta_Arenas";
case "saint pierre standard time": return "America/Miquelon";
case "bahia standard time": return "America/Bahia";
case "utc-02": return "Etc/GMT+2";
case "azores standard time": return "Atlantic/Azores";
case "cape verde standard time": return "Atlantic/Cape_Verde";
case "utc": return "Etc/GMT";
case "morocco standard time": return "Africa/Casablanca";
case "gmt standard time": return "Europe/London";
case "greenwich standard time": return "Atlantic/Reykjavik";
case "w. europe standard time": return "Europe/Berlin";
case "central europe standard time": return "Europe/Budapest";
case "romance standard time": return "Europe/Paris";
case "central european standard time": return "Europe/Warsaw";
case "w. central africa standard time": return "Africa/Lagos";
case "jordan standard time": return "Asia/Amman";
case "gtb standard time": return "Europe/Bucharest";
case "middle east standard time": return "Asia/Beirut";
case "egypt standard time": return "Africa/Cairo";
case "e. europe standard time": return "Europe/Chisinau";
case "syria standard time": return "Asia/Damascus";
case "west bank standard time": return "Asia/Hebron";
case "south africa standard time": return "Africa/Johannesburg";
case "fle standard time": return "Europe/Kiev";
case "israel standard time": return "Asia/Jerusalem";
case "kaliningrad standard time": return "Europe/Kaliningrad";
case "sudan standard time": return "Africa/Khartoum";
case "libya standard time": return "Africa/Tripoli";
case "namibia standard time": return "Africa/Windhoek";
case "arabic standard time": return "Asia/Baghdad";
case "turkey standard time": return "Europe/Istanbul";
case "arab standard time": return "Asia/Riyadh";
case "belarus standard time": return "Europe/Minsk";
case "russian standard time": return "Europe/Moscow";
case "e. africa standard time": return "Africa/Nairobi";
case "iran standard time": return "Asia/Tehran";
case "arabian standard time": return "Asia/Dubai";
case "astrakhan standard time": return "Europe/Astrakhan";
case "azerbaijan standard time": return "Asia/Baku";
case "russia time zone 3": return "Europe/Samara";
case "mauritius standard time": return "Indian/Mauritius";
case "saratov standard time": return "Europe/Saratov";
case "georgian standard time": return "Asia/Tbilisi";
case "caucasus standard time": return "Asia/Yerevan";
case "afghanistan standard time": return "Asia/Kabul";
case "west asia standard time": return "Asia/Tashkent";
case "ekaterinburg standard time": return "Asia/Yekaterinburg";
case "pakistan standard time": return "Asia/Karachi";
case "india standard time": return "Asia/Calcutta";
case "sri lanka standard time": return "Asia/Colombo";
case "nepal standard time": return "Asia/Katmandu";
case "central asia standard time": return "Asia/Almaty";
case "bangladesh standard time": return "Asia/Dhaka";
case "omsk standard time": return "Asia/Omsk";
case "myanmar standard time": return "Asia/Rangoon";
case "se asia standard time": return "Asia/Bangkok";
case "altai standard time": return "Asia/Barnaul";
case "w. mongolia standard time": return "Asia/Hovd";
case "north asia standard time": return "Asia/Krasnoyarsk";
case "n. central asia standard time": return "Asia/Novosibirsk";
case "tomsk standard time": return "Asia/Tomsk";
case "china standard time": return "Asia/Shanghai";
case "north asia east standard time": return "Asia/Irkutsk";
case "singapore standard time": return "Asia/Singapore";
case "w. australia standard time": return "Australia/Perth";
case "taipei standard time": return "Asia/Taipei";
case "ulaanbaatar standard time": return "Asia/Ulaanbaatar";
case "aus central w. standard time": return "Australia/Eucla";
case "transbaikal standard time": return "Asia/Chita";
case "tokyo standard time": return "Asia/Tokyo";
case "north korea standard time": return "Asia/Pyongyang";
case "korea standard time": return "Asia/Seoul";
case "yakutsk standard time": return "Asia/Yakutsk";
case "cen. australia standard time": return "Australia/Adelaide";
case "aus central standard time": return "Australia/Darwin";
case "e. australia standard time": return "Australia/Brisbane";
case "aus eastern standard time": return "Australia/Sydney";
case "west pacific standard time": return "Pacific/Port_Moresby";
case "tasmania standard time": return "Australia/Hobart";
case "vladivostok standard time": return "Asia/Vladivostok";
case "lord howe standard time": return "Australia/Lord_Howe";
case "bougainville standard time": return "Pacific/Bougainville";
case "russia time zone 10": return "Asia/Srednekolymsk";
case "magadan standard time": return "Asia/Magadan";
case "norfolk standard time": return "Pacific/Norfolk";
case "sakhalin standard time": return "Asia/Sakhalin";
case "central pacific standard time": return "Pacific/Guadalcanal";
case "russia time zone 11": return "Asia/Kamchatka";
case "new zealand standard time": return "Pacific/Auckland";
case "utc+12": return "Etc/GMT-12";
case "fiji standard time": return "Pacific/Fiji";
case "chatham islands standard time": return "Pacific/Chatham";
case "utc+13": return "Etc/GMT-13";
case "tonga standard time": return "Pacific/Tongatapu";
case "samoa standard time": return "Pacific/Apia";
case "line islands standard time": return "Pacific/Kiritimati";
}
return undefined;
}
}