forked from trepo/gedcomx-date-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GedcomXDate.js
2249 lines (1920 loc) · 54 KB
/
GedcomXDate.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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.GedcomXDate = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var util = require('util'),
Simple = require('./simple.js');
/**
* An approximate GedcomX Date.
* Inherits from Simple.
*/
function Approximate() {
if(arguments.length > 0) {
if(arguments[0].length < 1 || arguments[0].charAt(0) != 'A') {
throw new Error('Invalid Approximate Date');
}
try {
Simple.call(this, arguments[0].substr(1));
} catch(e) {
throw new Error(e.message+' in Approximate Date');
}
} else {
Simple.call(this);
}
}
util.inherits(Approximate, Simple);
/**
* An approximate date is approximate.
*/
Approximate.prototype.isApproximate = function() {
return true;
}
/**
* Output Formalized approximate string.
*/
Approximate.prototype.toFormalString = function() {
return 'A'+Approximate.super_.prototype.toFormalString.call(this);
}
module.exports = Approximate;
},{"./simple.js":6,"util":12}],2:[function(require,module,exports){
/**
* A gedcomX Duration
*/
function Duration(str) {
// There must be at least a P
if(str.length < 1 || str.charAt(0) != 'P') {
throw new Error('Invalid Duration');
}
var duration = str.substr(1);
if(duration.length < 1) {
throw new Error('Invalid Duration');
}
// 5.3.2 allows for NON normalized durations
// We assume that if there is a space, it is non-normalized
if(/\s/.test(duration)) {
throw new Error('Non normalized durations not implemented');
//this._parseNonNormalized(duration);
} else {
this._parseNormalized(duration);
}
}
/**
* Parse a normalized duration.
*/
Duration.prototype._parseNormalized = function(str) {
var duration = str.split(''),
currentNum = '',
inTime = false,
seen = [],
valid = ['Y', 'Mo', 'D', 'T', 'H', 'Mi', 'S'];
for(var x in duration) {
var character = duration[x];
if(/[0-9]/.test(character)) {
currentNum += character+'';
continue;
}
switch(character) {
case 'Y':
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid years');
}
if(seen.indexOf('Y') != -1) {
throw new Error('Invalid Duration: duplicate years');
}
if(valid.indexOf('Y') == -1) {
throw new Error('Invalid Duration: years out of order');
}
this._years = parseInt(currentNum, 10);
seen.push('Y');
valid = valid.slice(valid.indexOf('Y')+1);
currentNum = '';
break;
case 'M':
if(inTime) {
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid minutes');
}
if(seen.indexOf('Mi') != -1) {
throw new Error('Invalid Duration: duplicate minutes');
}
if(valid.indexOf('Mi') == -1) {
throw new Error('Invalid Duration: minutes out of order');
}
this._minutes = parseInt(currentNum, 10);
seen.push('Mi');
valid = valid.slice(valid.indexOf('Mi')+1);
currentNum = '';
} else {
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid months');
}
if(seen.indexOf('Mo') != -1) {
throw new Error('Invalid Duration: duplicate months');
}
if(valid.indexOf('Mo') == -1) {
throw new Error('Invalid Duration: months out of order');
}
this._months = parseInt(currentNum, 10);
valid = valid.slice(valid.indexOf('Mo')+1);
seen.push('Mo');
currentNum = '';
}
break;
case 'D':
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid days');
}
if(seen.indexOf('D') != -1) {
throw new Error('Invalid Duration: duplicate days');
}
if(valid.indexOf('D') == -1) {
throw new Error('Invalid Duration: days out of order');
}
this._days = parseInt(currentNum, 10);
seen.push('D');
valid = valid.slice(valid.indexOf('D')+1);
currentNum = '';
break;
case 'H':
if(!inTime) {
throw new Error('Invalid Duration: Missing T before hours');
}
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid hours');
}
if(seen.indexOf('H') != -1) {
throw new Error('Invalid Duration: duplicate hours');
}
if(valid.indexOf('H') == -1) {
throw new Error('Invalid Duration: hours out of order');
}
this._hours = parseInt(currentNum, 10);
seen.push('H');
valid = valid.slice(valid.indexOf('H')+1);
currentNum = '';
break;
case 'S':
if(!inTime) {
throw new Error('Invalid Duration: Missing T before seconds');
}
if(currentNum.length < 1) {
throw new Error('Invalid Duration: invalid seconds');
}
if(seen.indexOf('S') != -1) {
throw new Error('Invalid Duration: duplicate seconds');
}
// Note that you cannot have seconds out of order because it is last
this._seconds = parseInt(currentNum, 10);
seen.push('S');
valid = [];
currentNum = '';
break;
case 'T':
if(seen.indexOf('T') != -1) {
throw new Error('Invalid Duration: duplicate T');
}
inTime = true;
seen.push('T');
valid = valid.slice(valid.indexOf('T')+1);
break;
default:
throw new Error('Invalid Duration: Unknown Letter '+character);
}
}
// If there is leftover we have an invalid
if(currentNum != '') {
throw new Error('Invalid Duration: No letter after '+currentNum);
}
}
/**
* Return the string recurring.
*/
Duration.prototype.getType = function() {
return 'duration';
}
/**
* A duration is never approximate.
*/
Duration.prototype.isApproximate = function() {
return false;
}
/**
* Return the years as a number or undefined.
*/
Duration.prototype.getYears = function() {
return this._years;
}
/**
* Return the months as a number or undefined.
*/
Duration.prototype.getMonths = function() {
return this._months;
}
/**
* Return the days as a number or undefined.
*/
Duration.prototype.getDays = function() {
return this._days;
}
/**
* Return the hours as a number or undefined.
*/
Duration.prototype.getHours = function() {
return this._hours;
}
/**
* Return the minutes as a number or undefined.
*/
Duration.prototype.getMinutes = function() {
return this._minutes;
}
/**
* Return the seconds as a number or undefined.
*/
Duration.prototype.getSeconds = function() {
return this._seconds;
}
/**
* Output Formalized duration string.
*/
Duration.prototype.toFormalString = function() {
var duration = 'P';
if(this._years) {
duration += this._years+'Y';
}
if(this._months) {
duration += this._months+'M';
}
if(this._days) {
duration += this._days+'D';
}
if(this._hours || this._minutes || this._seconds) {
duration += 'T';
if(this._hours) {
duration += this._hours+'H';
}
if(this._minutes) {
duration += this._minutes+'M';
}
if(this._seconds) {
duration += this._seconds+'S';
}
}
return duration;
}
module.exports = Duration;
},{}],3:[function(require,module,exports){
var GedUtil = require('./util.js'),
Simple = require('./simple.js'),
Approximate = require('./approximate.js'),
Recurring = require('./recurring.js'),
Range = require('./range.js');
/**
* A GedcomX Date.
* This will parse the passed in string and return
* the appropriate GedcomX Date object.
*/
function GedcomXDate(str) {
if(str == '') {
throw new Error('Invalid Date');
}
if(str.charAt(0) == 'R') {
return new Recurring(str);
} else if(/\//.test(str)) {
return new Range(str);
} else if(str.charAt(0) == 'A') {
return new Approximate(str);
} else {
return new Simple(str);
}
}
/**
* The version of this library.
*/
GedcomXDate.version = '0.3.2';
/**
* Expose addDuration.
*/
GedcomXDate.addDuration = GedUtil.addDuration;
/**
* Expose multiplyDuration.
*/
GedcomXDate.multiplyDuration = GedUtil.multiplyDuration;
/**
* Expose getDuration.
*/
GedcomXDate.getDuration = GedUtil.getDuration;
/**
* Expose daysInMonth.
*/
GedcomXDate.daysInMonth = GedUtil.daysInMonth;
/**
* Expose now.
*/
GedcomXDate.now = GedUtil.now;
/**
* Expose fromJSDate.
*/
GedcomXDate.fromJSDate = GedUtil.fromJSDate;
/**
* Expose compare.
*/
GedcomXDate.compare = GedUtil.compare;
module.exports = GedcomXDate;
},{"./approximate.js":1,"./range.js":4,"./recurring.js":5,"./simple.js":6,"./util.js":8}],4:[function(require,module,exports){
var GedUtil = require('./util.js'),
Simple = require('./simple.js'),
Duration = require('./duration.js'),
Approximate = require('./approximate.js');
/**
* A GedcomX Range.
* It will place a Singel date at this.start and this.end,
* as well as a Duration at this.duration.
*/
function Range(str) {
var range = str;
// If range starts with A, its approximate
if(range.charAt(0) == 'A') {
this._approximate = true;
range = str.substr(1);
}
var parts = range.split('/');
if(parts.length != 2 || (!parts[0] && !parts[1])) {
throw new Error('Invalid Date Range');
}
if(parts[0]) {
try {
this.start = new Simple(parts[0]);
} catch(e) {
throw new Error(e.message+' in Range Start Date');
}
}
if(parts[1]) {
if(parts[1].charAt(0) == 'P') {
if(!this.start) {
throw new Error('A Range may not end with a duration if missing a start date');
}
try {
this.duration = new Duration(parts[1]);
} catch(e) {
throw new Error(e.message+' in Range End Date');
}
// Use duration and calculate end date
this.end = GedUtil.addDuration(this.start, this.duration);
} else {
try {
this.end = new Simple(parts[1]);
} catch(e) {
throw new Error(e.message+' in Range End Date');
}
if(this.start) {
this.duration = GedUtil.getDuration(this.start, this.end);
}
}
}
}
/**
* Return the string range.
*/
Range.prototype.getType = function() {
return 'range';
}
/**
* Return true if range is approximate.
*/
Range.prototype.isApproximate = function() {
if(this._approximate) {
return true;
} else {
return false;
}
}
/**
* Return the start date or undefined.
*/
Range.prototype.getStart = function() {
return this.start;
}
/**
* Return the end date or undefined.
*/
Range.prototype.getDuration = function() {
return this.duration;
}
/**
* Return the duration or undefined.
*/
Range.prototype.getEnd = function() {
return this.end;
}
/**
* Output Formalized range string.
*/
Range.prototype.toFormalString = function() {
var range = '';
if(this._approximate) {
range += 'A';
}
if(this.start) {
range += this.start.toFormalString();
}
range += '/';
if(this.duration) {
range += this.duration.toFormalString();
} else if(this.end) {
range += this.end.toFormalString();
}
return range;
}
module.exports = Range;
},{"./approximate.js":1,"./duration.js":2,"./simple.js":6,"./util.js":8}],5:[function(require,module,exports){
var util = require('util'),
GedUtil = require('./util.js'),
Range = require('./range.js');
/**
* A GedcomX Recurring Date.
*/
function Recurring(str) {
var parts = str.split('/');
if(str.charAt(0) != 'R' || parts.length != 3) {
throw new Error('Invalid Recurring Date');
}
// We must have start and end. error if both aren't set
if(!parts[1] || !parts[2]) {
throw new Error('Recurring must have a start and end');
}
var countNum = parts[0].substr(1);
// Validate count is a number if set
if(countNum) {
if(!(/^[0-9]+$/.test(countNum))) {
throw new Error('Invalid recurrence count: not a number')
}
this.count = parseInt(countNum, 10);
if(this.count < 0) throw new Error('Invalid recurrence count');
}
Range.call(this, parts[1]+'/'+parts[2]);
// If we have a count, replace end with the actual end date or undefined.
delete this.end;
if(this.count) {
this.end = this.getNth(this.count);
}
}
util.inherits(Recurring, Range);
/**
* Return the string recurring.
*/
Recurring.prototype.getType = function() {
return 'recurring';
}
/**
* Return the count or Infinity.
*/
Recurring.prototype.getCount = function() {
if(this.count == undefined) {
return Infinity;
} else {
return this.count;
}
}
/**
* Returns the nth instance of this recurring date.
*/
Recurring.prototype.getNth = function(multiplier) {
var duration = GedUtil.multiplyDuration(this.duration, multiplier);
return GedUtil.addDuration(this.start, duration);
}
/**
* Output Formalized range string.
*/
Recurring.prototype.toFormalString = function() {
var range = Recurring.super_.prototype.toFormalString.call(this);
if(this.count) {
return 'R'+this.count+'/'+range;
} else {
return 'R/'+range;
}
}
module.exports = Recurring;
},{"./range.js":4,"./util.js":8,"util":12}],6:[function(require,module,exports){
var GlobalUtil = require('./util-global.js');
/**
* The simplest representation of a date.
*/
function Simple() {
// If arguments passed in, try and parse the simple date
if(arguments.length > 0) {
this._parse(arguments[0]);
} else {
// Note that all dates and times are UTC internally!
var date = new Date();
this._year = date.getUTCFullYear();
this._month = date.getUTCMonth();
this._day = date.getUTCDay();
this._hours = date.getUTCHours();
this._minutes = date.getUTCMinutes();
this._seconds = date.getUTCSeconds();
this._tzHours = 0;
this._tzMinutes = 0;
}
}
/**
* Parse a simple date.
* This function also does strict validation.
*/
Simple.prototype._parse = function(str) {
var end = str.length,
offset = 0;
// There is a minimum length of 5 characters
if(str.length < 5) throw new Error('Invalid Date');
// Extract and validate year
var year = str.substr(offset,5);
if(year.match(/^[+-][0-9]{4}$/) === null) {
throw new Error('Invalid Date: Malformed year');
}
this._year = parseInt(year, 10);
offset += 5;
if(offset == end) {
return;
}
// If there is time
if(str.charAt(offset) == 'T') {
return this._parseTime(str.substr(offset+1));
}
if(str.charAt(offset) != '-') {
throw new Error('Invalid Date: Malformed year-month separator');
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed month');
}
// Extract and validate month
var month = str.substr(offset+1,2);
if(month.match(/^(0[1-9]|1[0-2])$/) === null) {
throw new Error('Invalid Date: Malformed month');
}
this._month = parseInt(month, 10);
offset += 3;
if(offset == end) {
return;
}
// If there is time
if(str.charAt(offset) == 'T') {
return this._parseTime(str.substr(offset+1));
}
if(str.charAt(offset) != '-') {
throw new Error('Invalid Date: Malformed month-day separator');
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed day');
}
// Extract and validate day
var day = str.substr(offset+1,2);
var daysInMonth = GlobalUtil.daysInMonth(this._month, this._year);
switch(daysInMonth) {
case 31:
if(day.match(/^(0[1-9]|[1-2][0-9]|3[0-1])$/) === null) {
throw new Error('Invalid Date: Malformed day (31 in month '+this._month+')');
}
break;
case 30:
if(day.match(/^(0[1-9]|[1-2][0-9]|30)$/) === null) {
throw new Error('Invalid Date: Malformed day (30 in month '+this._month+')');
}
break;
case 29:
if(day.match(/^(0[1-9]|1[0-9]|2[0-9])$/) === null) {
throw new Error('Invalid Date: Malformed day (29 in month '+this._month+' - leapyear)');
}
break;
case 28:
if(day.match(/^(0[1-9]|1[0-9]|2[0-8])$/) === null) {
throw new Error('Invalid Date: Malformed day (28 in month '+this._month+')');
}
break;
}
this._day = parseInt(day, 10);
offset += 3;
if(offset == end) return;
// If there is time
if(str.charAt(offset) == 'T') {
return this._parseTime(str.substr(offset+1));
} else {
throw new Error('Invalid Date');
}
}
/**
* Parse the time component.
*/
Simple.prototype._parseTime = function(str) {
var offset = 0,
end = str.length,
flag24 = false;
// Always initialize the Timezone to the local offset.
// It may be overridden if set
var tempDate = new Date(),
tempOffset = tempDate.getTimezoneOffset();
this._tzHours = tempOffset/60;
this._tzMinutes = tempOffset%60;
// There is a minimum length of 2 characters
if(str.length < 2) throw new Error('Invalid Date: Malformed hours');
// Extract and validate hours
var hours = str.substr(offset,2);
if(hours.match(/^([0-1][0-9]|2[0-3])$/) === null) {
if(hours == '24') {
flag24 = true;
} else {
throw new Error('Invalid Date: Malformed hours');
}
}
this._hours = parseInt(hours, 10);
offset += 2;
if(offset == end) {
return;
}
// If there is timezone offset
if(str.charAt(offset) == '+' || str.charAt(offset) == '-' || str.charAt(offset) == 'Z') {
return this._parseTimezone(str.substr(offset));
}
if(str.charAt(offset) != ':') {
throw new Error('Invalid Date: Malformed hour-minute separator');
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed minutes');
}
var minutes = str.substr(offset+1,2);
if(minutes.match(/^[0-5][0-9]$/) === null) {
throw new Error('Invalid Date: Malformed minutes');
}
if(flag24 && minutes != '00') {
throw new Error('Invalid Date: Hour of 24 requires 00 minutes');
}
this._minutes = parseInt(minutes, 10);
offset += 3;
if(offset == end) {
return;
}
// If there is timezone offset
if(str.charAt(offset) == '+' || str.charAt(offset) == '-' || str.charAt(offset) == 'Z') {
return this._parseTimezone(str.substr(offset));
}
if(str.charAt(offset) != ':') {
throw new Error('Invalid Date: Malformed minute-second separator');
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed seconds');
}
var seconds = str.substr(offset+1,2);
if(seconds.match(/^[0-5][0-9]$/) === null) {
throw new Error('Invalid Date: Malformed seconds');
}
if(flag24 && seconds != '00') {
throw new Error('Invalid Date: Hour of 24 requires 00 seconds');
}
this._seconds = parseInt(seconds, 10);
offset += 3;
if(offset == end) {
return;
}
// If there is timezone offset
if(str.charAt(offset) == '+' || str.charAt(offset) == '-' || str.charAt(offset) == 'Z') {
return this._parseTimezone(str.substr(offset));
} else {
throw new Error('Invalid Date: Malformed Time');
}
}
/**
* Parse the timezone component.
*/
Simple.prototype._parseTimezone = function(str) {
var offset = 0,
end = str.length;
// If Z we're done
if(str.charAt(0) == 'Z') {
if(str.length == 1) {
this._tzHours = 0;
this._tzMinutes = 0;
return;
} else {
throw new Error('Invalid Date: malformed timezone');
}
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed timezone');
}
var tzHours = str.substr(offset,3);
if(tzHours.match(/^[+-]([0-1][0-9]|2[0-3])$/) === null) {
throw new Error('Invalid Date: Malformed timezone hours');
}
this._tzHours = parseInt(tzHours, 10);
// set tz minutes to clear out default local tz offset
this._tzMinutes = 0;
offset += 3;
if(offset == end) {
return;
}
if(str.charAt(offset) != ':') {
throw new Error('Invalid Date: Malformed timezone hour-minute separator');
}
if(end-offset < 3) {
throw new Error('Invalid Date: Malformed timezone minutes');
}
var tzMinutes = str.substr(offset+1,2);
if(tzMinutes.match(/^[0-5][0-9]$/) === null) {
throw new Error('Invalid Date: Malformed timezone minutes');
}
this._tzMinutes = parseInt(tzMinutes, 10);
offset += 3;
if(offset == end) {
return;
} else {
throw new Error('Invalid Date: Malformed timezone');
}
}
/**
* Return the string single.
*/
Simple.prototype.getType = function() {
return 'single';
}
/**
* A simple date is not approximate.
*/
Simple.prototype.isApproximate = function() {
return false;
}
/**
* Return the year as a number.
*/
Simple.prototype.getYear = function() {
return this._year;
}
/**
* Return the month as a number.
*/
Simple.prototype.getMonth = function() {
return this._month;
}
/**
* Return the day as a number.
*/
Simple.prototype.getDay = function() {
return this._day;
}
/**
* Return the hours as a number.
*/
Simple.prototype.getHours = function() {
return this._hours;
}
/**
* Return the minutes as a number.
*/
Simple.prototype.getMinutes = function() {
return this._minutes;
}
/**
* Return the seconds as a number.
*/
Simple.prototype.getSeconds = function() {
return this._seconds;
}
/**
* Return the timezone hours as a number.
*/
Simple.prototype.getTZHours = function() {
return this._tzHours;
}
/**
* Return the timezone minutes as a number.
*/
Simple.prototype.getTZMinutes = function() {
return this._tzMinutes;
}
/**
* Output Formalized simple string.
*/
Simple.prototype.toFormalString = function() {
var simple = '';
if(this._year >= 0) {
simple += '+'+('0000'+this._year).substr(-4,4);
} else {
simple += '-'+('0000'+Math.abs(this._year)).substr(-4,4);
}
if(this._month) {
simple += '-'+('00'+this._month).substr(-2,2);
}
if(this._day) {
simple += '-'+('00'+this._day).substr(-2,2);
}
if(this._hours != undefined || this._minutes != undefined || this._seconds != undefined) {
simple += 'T';
}
if(this._hours != undefined) {
simple += ('00'+this._hours).substr(-2,2);
}
if(this._minutes != undefined) {
simple += ':'+('00'+this._minutes).substr(-2,2);
}
if(this._seconds != undefined) {
simple += ':'+('00'+this._seconds).substr(-2,2);
}
if(this._hours != undefined || this._minutes != undefined || this._seconds != undefined) {
if(this._tzHours === 0 || this._tzMinutes === 0) {
simple += 'Z';
} else {
if(this._tzHours != undefined) {
if(this._tzHours >= 0) {
simple += '+';
} else {
simple += '-';
}
simple += ('00'+Math.abs(this._tzHours)).substr(-2,2);
}
if(this._tzMinutes != undefined) {
simple += ':'+('00'+this._tzMinutes).substr(-2,2);
}
}
}
return simple;
}
module.exports = Simple;
},{"./util-global.js":7}],7:[function(require,module,exports){
module.exports = {
daysInMonth: daysInMonth
}
/**
* Return the number of days in a month,
* taking leapyear into account.