This repository has been archived by the owner on Jan 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathTimePeriod.js
352 lines (249 loc) · 6.84 KB
/
TimePeriod.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
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
/**
This class is used to parse time period strings and check various date variables if they are within the time period.
A time period is a comma-separated list of starting and ending time pairs. Each pair is dash-separated.
Spaces don't matter.
For example, those are valid time periods:
* 10:00-15:00
* 11:00-23:30
* 10:00-15:00
* 10:00-12:00,12:30-14:00
* 10:15-10:45, 11:00-11:30, 12:00-12:30
You can omit minutes if you want to:
* 10-15
* 10-12,12:30-14
The time period pair is not considered inclusive: the beginning minute matches the time period while
the ending **does not.** In other words, for time period "10:00-12:00", the "10:00" and "10:59" will
match, while "12:00" is not.
Example:
var t = new TimePeriod('9:30-10:00,15-17');
if (!t.isValid) {
console.log("oops?");
return;
}
t.isHourMinuteIn(9,30); // true
t.isHourMinuteIn(9,23); // false
t.isHourMinuteIn(9,33); // true
t.isHourMinuteIn(9,59); // true
t.isHourMinuteIn(10,00); // false
t.isHourMinuteIn(15,10); // true
t.isHourMinuteIn(16,30); // true
t.isHourMinuteIn(23,00); // false
t.isUnixtimeIn(1322575298); // 16:01, true
t.isMinuteIn(9*60+30); // 9:30, true
*/
function TimePeriod(str) {
this.firstMinute=null;
this.lastMinute=null;
this._periodString = (str || '').replace(/ +/g,'');
if (!str || str==='') {
this._periods=[];
this.isValid=true;
} else {
this._parse();
}
this._baseUnixtime=0;
}
TimePeriod.prototype._parse = function() {
var i, m;
this.isValid=true;
this._periods=[];
this.firstMinute=null;
this.lastMinute=null;
var periods = this._periodString.split(',');
for(i=0;i<periods.length;i++) {
this._parseSinglePeriod(periods[i]);
}
for(m=0;m<1440;m++) {
if (this._periods[m]) {
if (this.firstMinute==null) {
this.firstMinute=m;
}
this.lastMinute=m;
}
}
};
TimePeriod.prototype._setUnixtime = function(unixtime) {
var d = Date.parseUnixtime(unixtime);
d.clearTime();
this._baseUnixtime = d.unixtime();
};
/**
Check if the given unixtime matches the period.
@param {Integer} unixtime unixtime to check
@return {Boolean} true if it matches
*/
TimePeriod.prototype.isUnixtimeIn = function(unixtime) {
if (!this._baseUnixtime) {
this._setUnixtime(unixtime);
}
if (unixtime - this._baseUnixtime > 86400) {
this._setUnixtime(unixtime);
}
var seconds = unixtime - this._baseUnixtime;
var minute = parseInt(seconds/60);
return this.isMinuteIn(minute);
};
TimePeriod.prototype._parseSinglePeriod = function(periodString) {
if (periodString.match(/-$/)) {
this.isValid=false;
return;
}
var times = periodString.split('-');
var startMinute=TimePeriod.timeToMinute(times[0]);
if (startMinute && times.length<=1) { //single hours
var _times = times[0].split(':');
times[1]=(parseInt(_times[0])+1)+":00";
}
var endMinute = TimePeriod.timeToMinute(times[1]);
if (endMinute===undefined || startMinute===undefined) {
this.isValid=false;
return;
}
if (isNaN(endMinute) || isNaN(startMinute)) {
this.isValid=false;
return;
}
if (endMinute<=startMinute) {
this.isValid=false;
return;
}
var i=0;
for(i=startMinute;i<endMinute;i++) {
this._periods[i]=true;
}
};
/**
Check if given date matches the period.
@param {Date} date date to check
@return {Boolean} true if it matches
*/
TimePeriod.prototype.isDateIn = function(date) {
return this.isHourMinuteIn(date.getHours(), date.getMinutes());
};
/**
Check if given hour and minute matches the period.
@param {Integer} hour hour to match
@param {Integer} minute minute to match
@return {Boolean} true if it matches
*/
TimePeriod.prototype.isHourMinuteIn = function(hour,minute) {
return this.isMinuteIn(hour*60+minute);
};
/**
Check if given day minute matches the period. Minutes are zero-based and calculated since 00:00,
so 9:45 becomes 9*60+45=585.
@param {Integer} minute minute to check
@return {Boolean} true if it matches
*/
TimePeriod.prototype.isMinuteIn = function(minute) {
return this._periods[minute];
};
/**
Returns first minute of the whole time period or 0 if none found.
@return {Integer}
*/
TimePeriod.prototype.getFirstMinute = function() {
var i;
for(i=0;i<=1440;i++) {
if (this._periods[i]) {
return i;
}
}
return 0;
};
/**
Returns last minute of the whole time period or 1440-1 if none found.
@return {Integer}
*/
TimePeriod.prototype.getLastMinute = function() {
var lastMinute=null;
var i;
for(i=0;i<=1440;i++) {
if (this._periods[i]) {
lastMinute = i;
}
}
return lastMinute==null?1339:lastMinute;
};
/**
Manually enable or disable a certain minute in the time period.
@param {Integer} m minute to set.
@param {Boolean} enabled true of this minute must be enabled, false if this minute must be disabled.
Example:
var t = new TimePeriod('10:00-10:30');
t.setMinute(10*60+20, false);
t.normalize(); // 10-10:19, 10:21-10:30
*/
TimePeriod.prototype.setMinute = function(m, enabled) {
this._periods[m] = enabled? true: false;
};
/**
Return the clean, canonical string for the time period.
@return {String}
*/
TimePeriod.prototype.normalize = function() {
if (!this.isValid) {
return '';
}
var
firstMinute = this.getFirstMinute(),
lastMinute = this.getLastMinute(),
_periods=[],
_periodsStrings=[],
currentPeriodOpen, m;
for(m=firstMinute;m<=lastMinute;m++) {
if (this._periods[m]) {
if (!currentPeriodOpen) {
currentPeriodOpen=m;
}
} else {
if (currentPeriodOpen) {
_periods.push([currentPeriodOpen,m-1]);
currentPeriodOpen=undefined;
}
}
}
if (currentPeriodOpen) {
_periods.push([currentPeriodOpen, lastMinute]);
}
_periods.forEach(function(period) {
_periodsStrings.push(TimePeriod.minuteToTime(period[0])+"-"+TimePeriod.minuteToTime(period[1]+1));
});
return _periodsStrings.join(',').replace(/:00/g,'');
};
/**
Helper function. Will return day minute for time string supplied.
@param {String} timeString time, like "11:30"
@return {Integer} the day minute; for example, 570 for "9:30".
*/
TimePeriod.timeToMinute = function(timeString) {
if (timeString===undefined) {
return undefined;
}
if (!timeString.match(/^(\d+):(\d+)$/) && !timeString.match(/^(\d+)$/)) {
return undefined;
}
var times = timeString.split(':');
// make sure it is not an octal number
times[0] = times[0].replace(/^\0+/, '');
if (times[1]) {
times[1] = times[1].replace(/^\0+/, '');
}
times[0] = parseInt(times[0]);
times[1] = parseInt(times[1] || 0);
if (times[1]>59) {
return undefined;
}
return times[0]*60+times[1];
};
/**
Helper function. Will return time string for day minute.
@param {Integer} m day minute.
@return {String} time string; for example, "9:30" for 570.
*/
TimePeriod.minuteToTime = function(m) {
// just to make sure it is not an octal number
m = parseInt(m.toString().replace(/^\0+/, ''));
return parseInt(m/60) + ':' +(parseInt(m%60)).pad(2);
};
module.exports = TimePeriod;