-
Notifications
You must be signed in to change notification settings - Fork 6
/
timeSolver.js
371 lines (366 loc) · 14.4 KB
/
timeSolver.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* timeSolver.js
*
* @description A small date time tool in JavaScript, see: https://github.com/sean1093/timeSolver/ for details
* @version v1.2.0
* @author Sean Chou
* @license [https://github.com/sean1093/timeSolver/blob/master/LICENSE] [Licensed under MIT]
*/
(function () {
'use strict';
//public
var timeSolver = {
add: function(d, c, t) {
t = _t(t);
d = _v(d);
c = (c === undefined) ? 0 : c;
var result = null;
switch(t) {
case 0:
result = new Date(d.setMilliseconds(d.getMilliseconds()+c));
break;
case 1:
result = new Date(d.setSeconds(d.getSeconds()+c));
break;
case 2:
result = new Date(d.setMinutes(d.getMinutes()+c));
break;
case 3:
result = new Date(d.setHours(d.getHours()+c));
break;
case 4:
result = new Date(d.setDate(d.getDate()+c));
break;
case 5:
result = new Date(d.setMonth(d.getMonth()+c));
break;
case 6:
result = new Date(d.setFullYear(d.getFullYear()+c));
break;
default:
console.error(_errorMsg[0]);
break;
}
return result;
},
subtract: function(d, c, t) {
t = _t(t);
d = _v(d);
c = (c === undefined) ? 0 : c;
var result = null;
switch(t) {
case 0:
result = new Date(d.setMilliseconds(d.getMilliseconds()-c));
break;
case 1:
result = new Date(d.setSeconds(d.getSeconds()-c));
break;
case 2:
result = new Date(d.setMinutes(d.getMinutes()-c));
break;
case 3:
result = new Date(d.setHours(d.getHours()-c));
break;
case 4:
result = new Date(d.setDate(d.getDate()-c));
break;
case 5:
result = new Date(d.setMonth(d.getMonth()-c));
break;
case 6:
result = new Date(d.setFullYear(d.getFullYear()-c));
break;
default:
console.error(_errorMsg[0]);
break;
}
return result;
},
equal: function(d1, d2) { //return true or false
d1 = _v(d1);
d2 = _v(d2);
return d1.toString() === d2.toString();
},
between: function(d1, d2, t) {
t = _t(t);
d1 = _v(d1);
d2 = _v(d2);
var result = d2.getTime() - d1.getTime();
var base = 1;
switch(t) {
case 0:
base = 1;
break;
case 1:
base = 1000;
break;
case 2:
base = 60000;
break;
case 3:
base = 3600000;
break;
case 4:
base = 86400000;
break;
case 5:
base = 2629800000;
break;
case 6:
base = 31557600000;
break;
default:
console.error(_errorMsg[0]);
break;
}
return result / base;
},
after: function(d1, d2, t) { //if d1 after d2 or not
return (this.between(d1, d2, t) > 0) ? false : true;
},
afterToday: function(d1) { //if d1 after today or not
return this.after(d1, new Date(), 'd');
},
before: function(d1, d2, t) { //if d1 before d2 or not
return (this.between(d1, d2, t) > 0) ? true : false;
},
beforeToday: function(d1) { //if d1 before today or not
return this.before(d1, new Date(), 'd');
},
getString: function(d, f) { //get date string in given format
f = (f === undefined)? 'YYYYMMDD' : f.toUpperCase();
d = _v(d);
var result = null;
var year = d.getFullYear();
var month = _appendZero(d.getMonth() + 1);
var date = _appendZero(d.getDate());
var hour = _appendZero(d.getHours());
var min = _appendZero(d.getMinutes());
var sec = _appendZero(d.getSeconds());
var millsec = _appendZero(d.getMilliseconds());
var YYYY = year.toString();
var MM = month.toString();
var DD = date.toString();
var YYYYMMDD = YYYY + MM + DD;
var HHMMSS = hour.toString() + ':' + min.toString() + ':' + sec.toString();
var HHMMSSS = HHMMSS + '.' + millsec.toString();
var dateString = {
0: YYYY,
1: YYYY + MM,
2: YYYYMMDD,
3: YYYY + '/' + MM + '/' + DD,
4: YYYY + '-' + MM + '-' + DD,
5: YYYY + '.' + MM + '.' + DD,
6: MM + DD + YYYY,
7: DD + MM + YYYY,
8: MM + '/' + DD + '/' + YYYY,
9: MM + '-' + DD + '-' + YYYY,
10: MM + '.' + DD + '.' + YYYY,
11: YYYY + '/' + MM + '/' + DD + ' ' + HHMMSS,
12: YYYY + '/' + MM + '/' + DD + ' ' + HHMMSSS,
13: YYYY + '-' + MM + '-' + DD + ' ' + HHMMSS,
14: YYYY + '-' + MM + '-' + DD + ' ' + HHMMSSS,
15: YYYY + '.' + MM + '.' + DD + ' ' + HHMMSS,
16: YYYY + '.' + MM + '.' + DD + ' ' + HHMMSS,
17: YYYYMMDD + ' ' + HHMMSS,
18: YYYYMMDD + ' ' + HHMMSSS,
19: MM + '/' + DD + '/' + YYYY + ' ' + HHMMSS,
20: MM + '/' + DD + '/' + YYYY + ' ' + HHMMSSS,
21: MM + '-' + DD + '-' + YYYY + ' ' + HHMMSS,
22: MM + '-' + DD + '-' + YYYY + ' ' + HHMMSSS,
23: MM + '.' + DD + '.' + YYYY + ' ' + HHMMSS,
24: MM + '.' + DD + '.' + YYYY + ' ' + HHMMSSS,
25: HHMMSS,
26: HHMMSSS
}
return dateString[_f[f]] ? dateString[_f[f]] : _errorMsg[0];
},
getAbbrWeek: function(d) { //return abbr. weekday name
return _v(d) !== null ? _v(d).toString().substring(0, 3) : new Error(_errorMsg[1]);
},
getFullWeek: function(d) { //return full weekday name
return _w[_v(d).getDay()];
},
getAbbrMonth: function(d) { //return abbr. month name
return _v(d) !== null ? _v(d).toString().substring(3, 7) : new Error(_errorMsg[1]);
},
getFullMonth: function(d) { //return full month name
return _m[_v(d).getMonth()];
},
isValid: function(st, f) { //input date string and return true/ false
var result = true;
if(f === undefined) {
if(new Date(st) == 'Invalid Date') {
result = false;
}
}
else{
f = f.toUpperCase();
switch(_f[f]) {
case 3:
if (!_r.a.test(st)) {
result = false;
}
break;
case 4:
if (!_r.b.test(st)) {
result = false;
}
break;
case 5:
if (!_r.c.test(st)) {
result = false;
}
break;
case 11:
var str = st.split(' ');
if (!_r.a.test(str[0]) || !_r.t.test(str[1])) {
result = false;
}
break;
case 13:
var str = st.split(' ');
if (!_r.b.test(str[0]) || !_r.t.test(str[1])) {
result = false;
}
break;
case 15:
var str = st.split(' ');
if (!_r.c.test(str[0]) || !_r.t.test(str[1])) {
result = false;
}
break;
case 25:
var str = st.split(' ');
if (!_r.t.test(str[1])) {
result = false;
}
break;
default:
console.error(_errorMsg[0]);
result = null;
break;
}
}
return result;
},
getQuarterByMonth: function(m) { // input month and return quarter
if(1 <= m && m <= 3) return 1;
else if(4 <= m && m <= 6) return 2;
else if(7 <= m && m <= 9) return 3;
else if(10 <= m && m <= 12) return 4;
else return null;
},
getFirstMonthByQuarter: function(q) { // input quarter and return this quarter's first month
if(q == 1) return 1;
else if(q == 2) return 4;
else if(q == 3) return 7;
else if(q == 4) return 10;
else return null;
},
timeArray: [],
timeLookMax: 0,
timeLookTotal: 0,
timeLookStart: function() {
this.timeArray.length = 0;
this.timeLookMax = 0;
this.timeLookTotal = 0;
this.timeArray.push({label: 'start', time: new Date(), interval: 0});
},
timeLook: function(label) {
var last = this.timeArray[this.timeArray.length-1];
var now = new Date();
var interval = this.between(last.time, now, 'S');
this.timeLookTotal += interval;
this.timeLookMax = interval > this.timeLookMax ? interval : this.timeLookMax;
this.timeArray.push({label: label, time: new Date(), interval: interval});
},
timeLookReport: function() {
var titleStyle = 'font-weight: bold; color: #3F51B5';
var reportStyle = 'color: #2962FF';
var infoStyle = 'color: #4CAF50';
var maxStyle = 'color: #ff0000';
var now = new Date();
console.log('%c=================================', reportStyle);
console.log('%c[timeSolver] Time Look Report', titleStyle);
for(var i = 1; i < timeSolver.timeArray.length; i++) {
var label = timeSolver.timeArray[i].label;
var interval = timeSolver.timeArray[i].interval;
var style = this.timeLookMax == interval ? maxStyle : reportStyle;
console.log('%c['+ interval +'s] '+Math.round((interval/this.timeLookTotal)*100) +'% '+label , style);
}
var end = new Date();
console.log('%c[timeSolver] Spend '+this.between(now, end, 'S')+'s to create this report', infoStyle);
console.log('%c[timeSolver] For more information: https://github.com/sean1093/timeSolver#timelook', infoStyle);
console.log('%c=================================', reportStyle);
}
};
//private
var _m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var _w = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var _r = {
a: /^(\d{4})([/])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,
b: /^(\d{4})([-])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,
c: /^(\d{4})([.])((1|3|5|7|8|0[13578]|1[02])\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[01])|(4|6|9|0[469]|11)\2([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0])|(02|2)\2([1-9]|0[1-9]|1[0-9]|2[0-8]))$/,
t: /^([01]?[0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/
};
var _f = {
'YYYY': 0,
'YYYYMM': 1,
'YYYYMMDD': 2,
'YYYY/MM/DD': 3,
'YYYY-MM-DD': 4,
'YYYY.MM.DD': 5,
'MMDDYYYY': 6,
'DDMMYYYY': 7,
'MM/DD/YYYY': 8,
'MM-DD-YYYY': 9,
'MM.DD.YYYY': 10,
'YYYY/MM/DD HH:MM:SS': 11,
'YYYY/MM/DD HH:MM:SS.SSS': 12,
'YYYY-MM-DD HH:MM:SS': 13,
'YYYY-MM-DD HH:MM:SS.SSS': 14,
'YYYY.MM.DD HH:MM:SS': 15,
'YYYY.MM.DD HH:MM:SS.SSS': 16,
'YYYYMMDD HH:MM:SS': 17,
'YYYYMMDD HH:MM:SS.SSS': 18,
'MM/DD/YYYY HH:MM:SS': 19,
'MM/DD/YYYY HH:MM:SS.SSS': 20,
'MM-DD-YYYY HH:MM:SS': 21,
'MM-DD-YYYY HH:MM:SS.SSS': 22,
'MM.DD.YYYY HH:MM:SS': 23,
'MM.DD.YYYY HH:MM:SS.SSS': 24,
'HH:MM:SS': 25,
'HH:MM:SS.SSS': 26
}
var _errorMsg = {
0: '[timeSolver] Input Type Error',
1: '[timeSolver] Invalid Date'
}
var _v = function(d) {
var returnDate = typeof d != 'object' ? new Date(d) : d
if(returnDate == 'Invalid Date') {
console.error(_errorMsg[1]);
return null;
}
return returnDate;
};
var _t = function(t) {
t = (t === undefined)? 'MILLISECOND' : t.toUpperCase();
if(t == 'MILLISECOND' || t == 'MILL') t = 0;
else if(t == 'SECOND' || t == 'S') t = 1;
else if(t == 'MINUTE' || t == 'MIN') t = 2;
else if(t == 'HOUR' || t == 'H') t = 3;
else if(t == 'DAY' || t == 'D') t = 4;
else if(t == 'MONTH' || t == 'M') t = 5;
else if(t == 'YEAR' || t == 'Y') t = 6;
return t;
};
var _appendZero = function(s) {
return s < 10 ? '0'+s : s;
};
if (typeof module !== 'undefined' && typeof module.exports !== 'undefined')
module.exports = timeSolver;
else
window.timeSolver = timeSolver;
})();