This repository was archived by the owner on Oct 6, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
183 lines (146 loc) · 5.43 KB
/
index.js
File metadata and controls
183 lines (146 loc) · 5.43 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
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
var moment = require('moment');
var formats = require('./src/supportedFormats');
// Add 5 digits year variations for compatibility
formats.forEach(function(format) {
if (format.indexOf('YYYY') !== -1) {
formats.push(format.replace(/YYYY/, 'YYYYY'));
}
});
module.exports = (function() {
function parse(expression, now, roundUp, timeZone, useTimeZoneForRounding) {
var math, time;
if (expression.substring(0, 3) === 'now') {
math = expression.substring(3);
time = moment.utc(now) || moment.utc();
} else {
var separator = expression.indexOf('||');
// If the math expression doesn't have separator
if (separator === -1) {
// Add timezone
math = '';
time = parseTime(expression, timeZone);
} else {
math = expression.substr(separator + 2);
time = parseTime(expression.substr(0, separator), timeZone);
}
}
// If there is no further expression to process
if (!math || math === '') {
return time.valueOf();
}
return evaluate(math, time, roundUp, timeZone, useTimeZoneForRounding).valueOf();
}
function evaluate(expression, now, roundUp, timeZone, useTimeZoneForRounding) {
var val = 0;
for (var i = 0; i < expression.length;) {
var char = expression[i];
var next;
i += 1;
if (i > expression.length) {
throw new Error('truncated date math [' + expression + ']');
}
if (char === '/') {
// Round result
next = expression[i++];
switch (next) {
case 'y': case 'Y': now = roundDate(now, 'year', roundUp, timeZone, useTimeZoneForRounding); break;
case 'M': now = roundDate(now, 'month', roundUp, timeZone, useTimeZoneForRounding); break;
case 'd': case 'D': now = roundDate(now, 'date', roundUp, timeZone, useTimeZoneForRounding); break;
case 'w': case 'W': now = roundDate(now, 'weekday', roundUp, timeZone, useTimeZoneForRounding); break;
case 'h': case 'H': now = roundDate(now, 'hour', roundUp, timeZone, useTimeZoneForRounding); break;
case 'm': now = roundDate(now, 'minute', roundUp); break;
case 's': case 'S': now = roundDate(now, 'second', roundUp, timeZone, useTimeZoneForRounding); break;
}
} else if (char === '+' || char === '-') {
if (i >= expression.length) {
throw new Error('truncated date math [' + expression + ']');
}
val = 0;
next = expression[i];
while (next >= '0' && next <= '9') {
// Consume the digit
i += 1;
if (i >= expression.length) {
throw new Error('truncated date math [' + expression + ']');
}
val = val * 10 + parseInt(next, 10);
next = expression[i];
}
val = val || 1;
val = (char === '+') ? val : -val;
} else if ((char >= 'a' && char <= 'z') || (char >= 'A' && char <= 'Z')) {
switch (char) {
case 'y': case 'Y': now = calculate(now, val, 'year'); break;
case 'M': now = calculate(now, val, 'month'); break;
case 'd': case 'D': now = calculate(now, val, 'date'); break;
case 'w': case 'W': now = calculate(now, val, 'week'); break;
case 'h': case 'H': now = calculate(now, val, 'hour'); break;
case 'm': now = calculate(now, val, 'minute'); break;
case 's': case 'S': now = calculate(now, val, 'second'); break;
default:
throw new Error('unit [' + char + '] not supported for date math [' + expression + ']');
}
} else {
throw new Error('operator not supported for date math [' + char + ']');
}
}
return now;
}
function calculate(now, offsetVal, unit) {
now[unit](now[unit]() + offsetVal);
return now;
}
function roundDate(now, unit, roundUp, timeZone, useTimeZoneForRounding) {
if (useTimeZoneForRounding) {
now = now.utcOffset(timeZone);
}
switch (unit) {
case 'year': now.month(0); /* falls through */
case 'month': now.date(1); /* falls through */
case 'date': now.hours(0); /* falls through */
case 'weekday': now.hours(0); /* falls through */
case 'hour': now.minutes(0); /* falls through */
case 'minute': now.seconds(0); /* falls through */
case 'second': now.milliseconds(0); /* falls through */
}
if (roundUp) {
if (unit === 'weekday') {
now.weekday(8);
} else {
now[unit](now[unit]() + 1);
}
now = moment.utc(now.valueOf() - 1);
} else {
if (unit === 'weekday') {
now.weekday(1);
}
}
return now;
}
function parseTime(s, timeZone) {
if (s && s.length > 4 && (s >= 0 || s < 0)) {
// Timestamp
if (s.length > 13) {
throw new Error('Bad timestamp in [' + s + ']');
}
return (s.length === 10) ? moment(+s * 1000) : moment(+s);
}
if (!/^[\dTZ\-\+:\.]+$/.test(s)) {
throw new Error('Bad time with format in [' + s + ']');
}
var time = moment.utc(s, formats);
// Ignore the timeZone parameter if the given time already have one
if (!/[\+\-]\d+:\d+|Z$/.test(s) && timeZone) {
var normalizedTimezoneOffset = moment.utc().utcOffset(timeZone).utcOffset();
time.subtract(normalizedTimezoneOffset, 'minute');
}
return time;
}
return {
parse: parse,
evaluate: evaluate,
calculate: calculate,
roundDate: roundDate,
parseTime: parseTime
};
})();