-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.js
51 lines (37 loc) · 1.23 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
const moment = require("moment-timezone");
const _ = require("lodash");
module.exports = function (start, end, timezone) {
// get range as moments
start = moment(start);
end = moment(end);
// get zone information
const zone = moment.tz.zone(timezone);
// find timezone offset boundaries for the specified range
const boundaries = [];
_.each(zone.untils, (until, i) => {
const offset = zone.offsets[i];
if (until > start.valueOf()) {
boundaries.push({ until, offset });
if (until > end.valueOf()) {
return false;
}
}
});
// recursively build condition structure
function getCondition(boundaries) {
const boundary = boundaries.shift();
const until = boundary.until;
const offset = boundary.offset * 60 * 1000; // minutes -> milliseconds
if (boundaries.length == 0) {
return { $subtract: ["$time", offset] };
}
return {
$cond: {
if: { $lt: ["$time", new Date(until)] },
then: { $subtract: ["$time", offset] },
else: getCondition(boundaries)
}
}
}
return getCondition(boundaries);
};