-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
254 lines (240 loc) · 6.79 KB
/
index.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
"use strict";
const Regex24Hour = /^(0?[1,2]?[0-9])\W*?([0-5]?[0-9])\W*?([0-5]?[0-9])?$/i;
const Regex12Hour =
/^(0?[1-9]|1[0-2])\W*?([0-5]?[0-9])\W*?([0-5]?[0-9])?(?:\s*?|\W*?)([AP]M)$/i;
const RegExHour =
/^(?:(\d*?)\s*?hours?)?\s*?(?:(\d*?)\s*?minutes?)?\s*?(?:(\d*?)\s*?seconds?)?$/i;
const timeArithmetic = {
/**
* Convert 24-hour format to 12-hour object.
*
* @param {string} hour 24 hour format to convert
* @return {object} { hour, minute, second, meridiem }
*/
to12Hour: function to12Hour(hour) {
if (typeof hour === "string") hour = timeArithmetic.get24Hour(hour);
return {
hour: ((hour.hour + 11) % 12) + 1,
minute: hour.minute,
second: hour.second,
meridiem: hour.hour + 1 ? (hour.hour < 12 ? "am" : "pm") : NaN,
};
},
to24Hour: function to24Hour(hour) {
/**
* Convert 12-hour format to 24-hour object.
*
* @param {string} hour 12 hour format to convert
* @return {object} { hour, minute, second }
*/
if (typeof hour === "string") hour = timeArithmetic.get12Hour(hour);
return {
hour: hour.meridiem
? (hour.meridiem.toLowerCase() === "am" ? 0 : 12) + (hour.hour % 12)
: NaN,
minute: hour.minute,
second: hour.second,
meridiem: null,
};
},
get24Hour: (_24hour) => {
/**
* get obejct from 24-hour format.
*
* @param {string} str 24 hour format
* @return {object} { hour, minute, second }
*/
if (typeof _24hour === "string") {
let matches = _24hour.trim().match(Regex24Hour) || [];
if (!matches.length)
return {
hour: NaN,
minute: NaN,
second: NaN,
meridiem: null,
};
return {
hour: parseInt(matches[1]) || 0,
minute: parseInt(matches[2]) || 0,
second: parseInt(matches[3]) || 0,
meridiem: null,
};
}
return (
("0" + String(_24hour.hour)).slice(-2, 3) +
":" +
("0" + String(_24hour.minute)).slice(-2, 3) +
":" +
("0" + String(_24hour.second)).slice(-2, 3)
);
},
get12Hour: (_12hour) => {
/**
* get obejct from 12-hour format.
*
* @param {string} str 12 hour format
* @return {object} { hour, minute, second, meridiem }
*/
if (typeof _12hour === "string") {
let matches = _12hour.trim().match(Regex12Hour) || [];
if (!matches.length)
return {
hour: NaN,
minute: NaN,
second: NaN,
meridiem: null,
};
return {
hour: parseInt(matches[1]) || 0,
minute: parseInt(matches[2]) || 0,
second: parseInt(matches[3]) || 0,
meridiem: (matches[4] || "").toLowerCase() || null,
};
}
return (
("0" + String(_12hour.hour)).slice(-2, 3) +
":" +
("0" + String(_12hour.minute)).slice(-2, 3) +
":" +
("0" + String(_12hour.second)).slice(-2, 3) +
" " +
_12hour.meridiem
);
},
getHour: (hour) => {
/**
* get obejct from hours minutes seconds format.
*
* @param {string} str hours minutes seconds format
* @return {object} { hour, minute, second}
*/
if (typeof hour === "string") {
hour = hour.trim();
let matches =
hour.match(RegExHour) ||
hour.match(Regex12Hour) ||
hour.match(Regex24Hour) ||
[];
if (!matches.length)
hour = {
hour: NaN,
minute: NaN,
second: NaN,
meridiem: null,
};
else
hour = {
hour: parseInt(matches[1]) || 0,
minute: parseInt(matches[2]) || 0,
second: parseInt(matches[3]) || 0,
meridiem: (matches[4] || "").toLowerCase() || null,
};
}
return {
hour: hour.hour || 0,
minute: hour.minute || 0,
second: hour.second || 0,
meridiem: hour.meridiem || null,
};
},
_add24Hour: (_24hour, hour) => {
hour = hour.meridiem ? timeArithmetic.to24Hour(hour) : hour;
let _hours =
_24hour.hour +
hour.hour +
Math.floor((_24hour.minute + hour.minute) / 60);
let _minutes = Math.floor(
(_24hour.minute +
hour.minute +
Math.floor((_24hour.second + hour.second) / 60)) %
60
);
let _seconds = Math.floor((_24hour.second + hour.second) % 60);
return {
day: Math.floor(_hours / 24),
hour: Math.floor(_hours % 24),
minute: _minutes,
second: _seconds,
meridiem: null,
};
},
_add12Hour: (_12hour, hour) => {
let _24hour = timeArithmetic.to24Hour(_12hour);
if (_24hour) {
_24hour = timeArithmetic._add24Hour(_24hour, hour);
let _12hour = timeArithmetic.to12Hour(_24hour);
_12hour.day = _24hour.day;
return _12hour;
}
return null;
},
addHour: (_hour, hour) => {
if (Regex24Hour.test(_hour))
return timeArithmetic._add24Hour(
timeArithmetic.get24Hour(_hour),
timeArithmetic.getHour(hour)
);
else if (Regex12Hour.test(_hour))
return timeArithmetic._add12Hour(
timeArithmetic.get12Hour(_hour),
timeArithmetic.getHour(hour)
);
else return null;
},
_diff24Hour: (_24hour, hour) => {
let _hours = 0;
let _minutes = 0;
let _seconds = _24hour.second - hour.second;
if (_seconds < 0) {
_seconds += 60;
_minutes = -1;
}
_minutes += _24hour.minute - hour.minute;
if (_minutes < 0) {
_minutes += 60;
_hours = -1;
}
_hours += _24hour.hour - hour.hour;
if (_hours < 0) _hours += 24;
return {
hour: _hours,
minute: _minutes,
second: _seconds,
meridiem: null,
};
},
_diff12Hour: (_12hour, hour) => {
let _24hour = timeArithmetic.to24Hour(_12hour);
let _dif24 = timeArithmetic._diff24Hour(_24hour, hour);
if (_24hour) return timeArithmetic.to12Hour(_dif24);
return null;
},
diffHour: (_hour, hour) => {
if (Regex24Hour.test(_hour))
return timeArithmetic._diff24Hour(
timeArithmetic.get24Hour(_hour),
timeArithmetic.getHour(hour)
);
else if (Regex12Hour.test(_hour))
return timeArithmetic._diff12Hour(
timeArithmetic.get12Hour(_hour),
timeArithmetic.getHour(hour)
);
else return null;
},
compareHour: (hours1, hours2) => {
let _hours1 = timeArithmetic.getHour(hours1);
let _hours2 = timeArithmetic.getHour(hours2);
_hours1 = _hours1.meridiem ? timeArithmetic.to24Hour(_hours1) : _hours1;
_hours2 = _hours2.meridiem ? timeArithmetic.to24Hour(_hours2) : _hours2;
return {
eq: _hours1.hour == _hours2.hour,
gt: _hours1.hour > _hours2.hour,
gte: _hours1.hour >= _hours2.hour,
lt: _hours1.hour < _hours2.hour,
lte: _hours1.hour <= _hours2.hour,
ne: _hours1.hour != _hours2.hour,
};
},
};
module.exports = timeArithmetic;