-
Notifications
You must be signed in to change notification settings - Fork 4
/
chronomouse.js
601 lines (414 loc) · 12.3 KB
/
chronomouse.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
var arrays = require('./codearrays')
var usCodes = arrays.usCodes
var countryCodes = arrays.countryCodes
var canadaCodes = arrays.canadaCodes
//Build Local Info object
function getLocalInfo(text,options,callback) {
if (!text) {
return;
}
var localInfo = new Object();
var text = cleanText(text.toString());
var text = standardizeCountry(text);
localInfo['text'] = text;
//set Options
setOptions(localInfo,options);
//Check for US
checkUS(localInfo);
//Add Type (Area or Country)
addType(localInfo);
//Add Location and Country Info
addLocation(localInfo);
//Add Current Time for Area Codes
addTime(localInfo);
if (typeof callback == "function") {
callback(localInfo);
return;
}
return localInfo;
}
// Correct 00 and replace for +
function standardizeCountry(x) {
if (x.substring(0,2) === "00") {
x = x.replace("00","+");
}
return x;
}
function setOptions(object,options){
//Set Defaults
object['options'] = {};
object['options']['military'] = true;
object['options']['zone_display'] = "offset";
//Only accept validated options
var submittedOptions = options;
var submittedOptions = validateOptions(options);
if (!submittedOptions) {
return;
}
if(submittedOptions['military'] == false || submittedOptions['military'] == "false") {
object['options']['military'] = false;
}
if(submittedOptions['zone_display'] === "name") {
object['options']['zone_display'] = "name";
}
}
//Remove invalid options
function validateOptions(options){
if (typeof(options) !== "object" || options.length < 1){
return false;
}
var submittedOptions = Object.keys(options);
var availableOptions = ['military','format','zone_display'];
for (i=0;i<submittedOptions.length;i++) {
var valid = false;
for(x=0;x<availableOptions.length;x++){
if(submittedOptions[i] == availableOptions[x]){
valid = true;
break;
}
}
if(!valid){
delete options[submittedOptions[i]];
}
}
return options;
}
//Check for US to process as area
function checkUS(object) {
var text = object['text'];
var beginning = text.substring(0,2);
if (text !== "+1") {
if (beginning === "+1") {
text = text.replace("+1", "");
}
if (text.substring(0,1) == "1"){
text = text.substring(1,text.length);
}
if (text.length < 3) {
object['valid'] = false;
}
}
object['text'] = text;
}
//Detect if Area Code or Country Code
function addType(object) {
var text = object['text'];
var beg = text.substring(0,1);
var beg2 = text.substring(0,2);
var type = "";
object['time'] = {};
if (beg === "+" || beg2 === "00") {
object['text'] = text.substring(0,5);
object['time']['zone'] = false;
type = "country";
}
else {
if (text.length < 3) {
object['valid'] = false;
}
object['text'] = text.substring(0,3);
object['time']['zone'] = false;
type = "area";
}
object['type'] = type;
}
function addLocation(object) {
var type = object['type'];
var text = object['text'];
var location = false;
switch(type) {
case "area":{
location = getLoc(object);
while (!location && text.length > 3){
text = text.substring(0,text.length-1);
object['text'] = text;
location = getLoc(object);
}
if (location) {
chooseUsCanada(object);
}
break;
}
case "country":{
object['country_info'] = getCountryInfo(object);
location = object['country_info']['name'];
while (!location && text.length > 2){
text = text.substring(0,text.length-1);
object['text'] = text;
object['country_info'] = getCountryInfo(object);
location = object['country_info']['name']
}
break;
}
}
if(!location) {
object['valid'] = false;
}
object['location'] = location;
}
//Linear Search US Time Zones
function getLoc(object) {
var text = object['text'];
var location = false;
var zone_display_key = object['options']['zone_display'];
for (i=0;i<usCodes.length-1;i++) {
for (x=0;x<usCodes[i]['codes'].length;x++) {
if (text === usCodes[i]['codes'][x]) {
var location = usCodes[i]['codes'][x+1];
object['time']['zone'] = usCodes[i][zone_display_key];
object['offset'] = usCodes[i]['offset'];
object['dst'] = usCodes[i]['dst'];
break;
}
}
}
if(location) {
return location;
}
var tollFree = usCodes[usCodes.length-1];
for (i=0;i<tollFree['codes'].length;i++) {
if (text === tollFree['codes'][i]) {
var location = 'Toll Free or Other';
object['time']['zone'] = "Toll Free or Other";
object['offset'] = tollFree['offset'];
object['dst'] = tollFree['dst'];
break;
}
}
return location;
}
//For US/Canada Area codes, add correct Country
function chooseUsCanada(object){
var code = object['text'];
var usIndex = getCountryInfo({text:'+1'}).index;
object['country_info'] = countryCodes[usIndex+1];
for (i=0;i<canadaCodes.length;i++) {
if (code === canadaCodes[i]) {
object['country_info'] = countryCodes[usIndex+2];
break;
}
}
}
function getCountryInfo(object) {
var text = object['text'];
var code = text.substring(1,text.length);
var country_info = [];
var countryIndex = false;
for (i=0;i<countryCodes.length;i++) {
if (code === countryCodes[i]['code']) {
var country_info = countryCodes[i];
object['offset'] = countryCodes[i]['offset'];
object['dst'] = countryCodes[i]['dst'];
countryIndex = i;
break;
}
}
country_info['index'] = countryIndex;
return country_info;
}
//Finds hour difference from GMT for specific time zone
function addTime(object) {
if (object['valid'] == false) {
object['time'] = false;
return;
}
object['dstnow'] = false;
var type = object['type'];
var zone = object['time']['zone'];
var offset = object['offset'];
var location = object['location'];
var isDlsAffected = object['dst'];
var zone_display = object['options']['zone_display'];
if (zone === "Toll Free or Other" || !location) {
object['time'] = false;
return;
}
var date = new Date();
var hour = date.getHours();
var mins = date.getMinutes();
var utcOffset = date.getTimezoneOffset()/60;
var utcHour = hour+utcOffset;
var hour2 = false;
var military = object['options']['military'];
//Check for Daylight Savings
var dst = 0;
if(isDlsAffected && checkDst(object)) {
dst += 1;
}
//Dual Time Zones
if (offset.length > 1) {
hour = utcHour + offset[1] + dst;
hour2 = utcHour + offset[0] + dst;
var time = formatTime(hour,mins,offset[1],military);
var time2 = formatTime(hour2,mins,offset[0],military);
object['time']['hour2'] = time2.hour;
object['time']['meridian2'] = time2.meridian;
if (object['time']['meridian2']){
meridian2 = " "+time2.meridian;
}else{
meridian2 = "";
}
object['time']['display2'] = time2.hour.toString()+":"+time.mins.toString()+meridian2;
//Format Zone Display
if (zone_display == "offset" || type == "country"){
object['time']['zone'] = formatOffsetZoneDisplay(offset[1]+dst,object);
object['time']['zone2'] = formatOffsetZoneDisplay(offset[0]+dst,object);
}
}
else {
hour = utcHour + offset + dst;
var time = formatTime(hour,mins,offset,military);
//Format Zone Display
if (zone_display == "offset" || type == "country"){
object['time']['zone'] = formatOffsetZoneDisplay(offset+dst,object);
}
}
object['time']['hour'] = time.hour;
object['time']['mins'] = time.mins;
object['time']['meridian'] = time.meridian;
if (object['time']['meridian']){
meridian = " "+time.meridian;
}else{
meridian = "";
}
object['time']['display'] = time.hour.toString()+":"+time.mins.toString()+meridian;
}
function checkDst(object) {
var date = new Date();
var year = date.getFullYear();
var dst = false;
var country_info = object['country_info'];
var start_month = country_info['start_month'];
var start_week = country_info['start_week'];
var start_offset = country_info['start_offset'];
var start_day = country_info['start_day'];
var end_month = country_info['end_month'];
var end_week = country_info['end_week'];
var end_day = country_info['end_day'];
var startDate = nthWeekdayOfMonth(start_day,start_week,new Date(year,start_month));
var endDate = nthWeekdayOfMonth(end_day,end_week,new Date(year,end_month));
var middleOfYear = new Date(year,5);
if(start_offset) {//Only affects Jerusalem and Jordan
var day = startDate.getDate();
var day = day + start_offset;
if(day > 31){
day = day - 31;
start_month = start_month + 1;
}
startDate = new Date(year,start_month,day);
}
if(startDate < middleOfYear) {
if (date >= startDate && date < endDate) {
dst = true;
}
}else{
if (date < startDate || date >= endDate) {
dst = true;
}
}
object['dstnow'] = dst;
return dst;
}
function nthWeekdayOfMonth(weekday, n, date) {
var count = 0;
var result = new Date(date.getFullYear(), date.getMonth(), 1);
while (true) {
if (result.getDay() === weekday) {
if (++count == n) {
break;
}
}
result.setDate(result.getDate() + 1);
}
return result;
}
function formatOffsetZoneDisplay(offset,object){
var offset = offset.toString();
if (offset > 0){
offset = "+"+offset;
}
if (offset == 0){
offset = "";
}
var output = "GMT"+offset;
return output;
}
function formatTime(hour,mins,offset,military) {
var meridian = false;
//Format Mins
//Handle irregular offsets
var partialOffset = (offset % 1);
if(partialOffset < 0 || partialOffset === (-0)) {
partialOffset *= (-1);
}
if (partialOffset > 0) {
offsetMinutes = partialOffset * 60;
mins = mins + offsetMinutes;
hour = hour - partialOffset;
}
if (mins < 0) {
mins = 60 + mins;
hour -= 1;
}
if (mins > 60) {
mins = mins-60;
hour += 1;
}
mins = mins.toString();
if (mins.length == 1) {
mins = "0"+mins;
}
if (mins == 0) {
mins = "00";
}
if (mins == 60) {
mins = "00";
hour += 1;
}
//Format Hour
if (hour >= 24) {
hour = hour-24;
}
if (hour < 0) {
hour = hour+24;
}
if (hour < 10 && military) {
hour = "0"+hour.toString();
}
//12-hour Format
if(!military) {
if (hour > 11) {
meridian = "PM";
}else {
meridian = "AM";
}
if (hour == 0 || hour == 23) {
hour = "12";
}
if (hour > 12 && hour < 24) {
hour = hour-12;
}
if (hour < 0) {
hour = hour+12;
}
}
var result = {
hour: hour.toString(),
mins: mins.toString(),
meridian: meridian
};
return result;
}
//Allow only numerical and "+" characters
function cleanText(x) {
var text = x;
text = text.replace(/\s/g, '');
text = text.replace(/[^+0-9]/g, '');
if (text.length > 5) {
text = text.substring(0,5);
}
return text;
}
module.exports = {
getLocalInfo
}