-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcomplement.js
More file actions
70 lines (59 loc) · 1.58 KB
/
complement.js
File metadata and controls
70 lines (59 loc) · 1.58 KB
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
const moment = require('moment-timezone');
const complement = (timeSpan, busy) => {
const timePoints = [];
const freeTimes = [];
timePoints.push(timeSpan.start);
busy.forEach((t) => {
timePoints.push(t.start);
timePoints.push(t.end);
});
timePoints.push(timeSpan.end);
for (let i = 0; i < timePoints.length; i = i + 2) {
freeTimes.push({start: timePoints[i], end: timePoints[i + 1]});
}
return freeTimes;
};
const convertMS = (isoTime) => {
const time = new Date(isoTime);
return time.getTime();
};
const convertToISO = (ms) => {
return moment(ms).tz('America/Los_Angeles').format();
};
const timeInterval = {
"start": "2017-07-19T12:00:00-07:00",
"end": "2017-07-26T12:00:00-07:00"
};
console.log('Time interval is: \n', timeInterval);
let busyTimes = [
{
"start": "2017-07-20T12:00:00-07:00",
"end": "2017-07-20T13:00:00-07:00"
},
{
"start": "2017-07-20T17:30:00-07:00",
"end": "2017-07-20T19:00:00-07:00"
},
{
"start": "2017-07-20T21:00:00-07:00",
"end": "2017-07-20T21:00:00-07:00"
},
{
"start": "2017-07-24T17:30:00-07:00",
"end": "2017-07-24T19:00:00-07:00"
}
];
console.log('Busy times are: \n', busyTimes);
busyTimes = busyTimes.map((t) => {
t.start = convertMS(t.start);
t.end = convertMS(t.end);
return t;
});
timeInterval.start = convertMS(timeInterval.start);
timeInterval.end = convertMS(timeInterval.end);
const finalTimes = complement(timeInterval, busyTimes).map((t) => {
t.start = convertToISO(t.start);
t.end = convertToISO(t.end);
return t;
});
console.log('Free times are: \n', finalTimes);