This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
date.js
1745 lines (1454 loc) · 48.4 KB
/
date.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
// Copyright 2006 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/**
* @fileoverview Functions and objects for date representation and manipulation.
*
* @author eae@google.com (Emil A Eklund)
*/
goog.provide('goog.date');
goog.provide('goog.date.Date');
goog.provide('goog.date.DateTime');
goog.provide('goog.date.Interval');
goog.provide('goog.date.month');
goog.provide('goog.date.weekDay');
goog.require('goog.asserts');
/** @suppress {extraRequire} */
goog.require('goog.date.DateLike');
goog.require('goog.i18n.DateTimeSymbols');
goog.require('goog.string');
/**
* Constants for weekdays.
* @enum {number}
*/
goog.date.weekDay = {
MON: 0,
TUE: 1,
WED: 2,
THU: 3,
FRI: 4,
SAT: 5,
SUN: 6
};
/**
* Constants for months.
* @enum {number}
*/
goog.date.month = {
JAN: 0,
FEB: 1,
MAR: 2,
APR: 3,
MAY: 4,
JUN: 5,
JUL: 6,
AUG: 7,
SEP: 8,
OCT: 9,
NOV: 10,
DEC: 11
};
/**
* Formats a month/year string.
* Example: "January 2008"
*
* @param {string} monthName The month name to use in the result.
* @param {number} yearNum The numeric year to use in the result.
* @return {string} A formatted month/year string.
*/
goog.date.formatMonthAndYear = function(monthName, yearNum) {
/** @desc Month/year format given the month name and the numeric year. */
var MSG_MONTH_AND_YEAR = goog.getMsg(
'{$monthName} {$yearNum}',
{'monthName': monthName, 'yearNum': String(yearNum)});
return MSG_MONTH_AND_YEAR;
};
/**
* Regular expression for splitting date parts from ISO 8601 styled string.
* Examples: '20060210' or '2005-02-22' or '20050222' or '2005-08'
* or '2005-W22' or '2005W22' or '2005-W22-4', etc.
* For explanation and more examples, see:
* {@link http://en.wikipedia.org/wiki/ISO_8601}
*
* @type {RegExp}
* @private
*/
goog.date.splitDateStringRegex_ = new RegExp(
'^(\\d{4})(?:(?:-?(\\d{2})(?:-?(\\d{2}))?)|' +
'(?:-?(\\d{3}))|(?:-?W(\\d{2})(?:-?([1-7]))?))?$');
/**
* Regular expression for splitting time parts from ISO 8601 styled string.
* Examples: '18:46:39.994' or '184639.994'
*
* @type {RegExp}
* @private
*/
goog.date.splitTimeStringRegex_ =
/^(\d{2})(?::?(\d{2})(?::?(\d{2})(\.\d+)?)?)?$/;
/**
* Regular expression for splitting timezone parts from ISO 8601 styled string.
* Example: The part after the '+' in '18:46:39+07:00'. Or '09:30Z' (UTC).
*
* @type {RegExp}
* @private
*/
goog.date.splitTimezoneStringRegex_ = /Z|(?:([-+])(\d{2})(?::?(\d{2}))?)$/;
/**
* Regular expression for splitting duration parts from ISO 8601 styled string.
* Example: '-P1Y2M3DT4H5M6.7S'
*
* @type {RegExp}
* @private
*/
goog.date.splitDurationRegex_ = new RegExp(
'^(-)?P(?:(\\d+)Y)?(?:(\\d+)M)?(?:(\\d+)D)?' +
'(T(?:(\\d+)H)?(?:(\\d+)M)?(?:(\\d+(?:\\.\\d+)?)S)?)?$');
/**
* Number of milliseconds in a day.
* @type {number}
*/
goog.date.MS_PER_DAY = 24 * 60 * 60 * 1000;
/**
* Returns whether the given year is a leap year.
*
* @param {number} year Year part of date.
* @return {boolean} Whether the given year is a leap year.
*/
goog.date.isLeapYear = function(year) {
// Leap year logic; the 4-100-400 rule
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
};
/**
* Returns whether the given year is a long ISO year.
* See {@link http://www.phys.uu.nl/~vgent/calendar/isocalendar_text3.htm}.
*
* @param {number} year Full year part of date.
* @return {boolean} Whether the given year is a long ISO year.
*/
goog.date.isLongIsoYear = function(year) {
var n = 5 * year + 12 - 4 * (Math.floor(year / 100) - Math.floor(year / 400));
n += Math.floor((year - 100) / 400) - Math.floor((year - 102) / 400);
n += Math.floor((year - 200) / 400) - Math.floor((year - 199) / 400);
return n % 28 < 5;
};
/**
* Returns the number of days for a given month.
*
* @param {number} year Year part of date.
* @param {number} month Month part of date.
* @return {number} The number of days for the given month.
*/
goog.date.getNumberOfDaysInMonth = function(year, month) {
switch (month) {
case goog.date.month.FEB:
return goog.date.isLeapYear(year) ? 29 : 28;
case goog.date.month.JUN:
case goog.date.month.SEP:
case goog.date.month.NOV:
case goog.date.month.APR:
return 30;
}
return 31;
};
/**
* Returns true if the 2 dates are in the same day.
* @param {goog.date.DateLike} date The time to check.
* @param {goog.date.DateLike=} opt_now The current time.
* @return {boolean} Whether the dates are on the same day.
*/
goog.date.isSameDay = function(date, opt_now) {
var now = opt_now || new Date(goog.now());
return date.getDate() == now.getDate() && goog.date.isSameMonth(date, now);
};
/**
* Returns true if the 2 dates are in the same month.
* @param {goog.date.DateLike} date The time to check.
* @param {goog.date.DateLike=} opt_now The current time.
* @return {boolean} Whether the dates are in the same calendar month.
*/
goog.date.isSameMonth = function(date, opt_now) {
var now = opt_now || new Date(goog.now());
return date.getMonth() == now.getMonth() && goog.date.isSameYear(date, now);
};
/**
* Returns true if the 2 dates are in the same year.
* @param {goog.date.DateLike} date The time to check.
* @param {goog.date.DateLike=} opt_now The current time.
* @return {boolean} Whether the dates are in the same calendar year.
*/
goog.date.isSameYear = function(date, opt_now) {
var now = opt_now || new Date(goog.now());
return date.getFullYear() == now.getFullYear();
};
/**
* Static function for week number calculation. ISO 8601 implementation.
*
* @param {number} year Year part of date.
* @param {number} month Month part of date (0-11).
* @param {number} date Day part of date (1-31).
* @param {number=} opt_weekDay Cut off weekday, defaults to Thursday.
* @param {number=} opt_firstDayOfWeek First day of the week, defaults to
* Monday.
* Monday=0, Sunday=6.
* @return {number} The week number (1-53).
*/
goog.date.getWeekNumber = function(
year, month, date, opt_weekDay, opt_firstDayOfWeek) {
var d = new Date(year, month, date);
// Default to Thursday for cut off as per ISO 8601.
var cutoff = goog.isDef(opt_weekDay) ? opt_weekDay : goog.date.weekDay.THU;
// Default to Monday for first day of the week as per ISO 8601.
var firstday = opt_firstDayOfWeek || goog.date.weekDay.MON;
// The d.getDay() has to be converted first to ISO weekday (Monday=0).
var isoday = (d.getDay() + 6) % 7;
// Position of given day in the picker grid w.r.t. first day of week
var daypos = (isoday - firstday + 7) % 7;
// Position of cut off day in the picker grid w.r.t. first day of week
var cutoffpos = (cutoff - firstday + 7) % 7;
// Unix timestamp of the midnight of the cutoff day in the week of 'd'.
// There might be +-1 hour shift in the result due to the daylight saving,
// but it doesn't affect the year.
var cutoffSameWeek =
d.valueOf() + (cutoffpos - daypos) * goog.date.MS_PER_DAY;
// Unix timestamp of January 1 in the year of 'cutoffSameWeek'.
var jan1 = new Date(new Date(cutoffSameWeek).getFullYear(), 0, 1).valueOf();
// Number of week. The round() eliminates the effect of daylight saving.
return Math.floor(
Math.round((cutoffSameWeek - jan1) / goog.date.MS_PER_DAY) / 7) +
1;
};
/**
* @param {T} date1 A datelike object.
* @param {S} date2 Another datelike object.
* @return {T|S} The earlier of them in time.
* @template T,S
*/
goog.date.min = function(date1, date2) {
return date1 < date2 ? date1 : date2;
};
/**
* @param {T} date1 A datelike object.
* @param {S} date2 Another datelike object.
* @return {T|S} The later of them in time.
* @template T,S
*/
goog.date.max = function(date1, date2) {
return date1 > date2 ? date1 : date2;
};
/**
* Creates a DateTime from a datetime string expressed in ISO 8601 format.
*
* @param {string} formatted A date or datetime expressed in ISO 8601 format.
* @return {goog.date.DateTime} Parsed date or null if parse fails.
*/
goog.date.fromIsoString = function(formatted) {
var ret = new goog.date.DateTime(2000);
return goog.date.setIso8601DateTime(ret, formatted) ? ret : null;
};
/**
* Parses a datetime string expressed in ISO 8601 format. Overwrites the date
* and optionally the time part of the given object with the parsed values.
*
* @param {!goog.date.DateTime} dateTime Object whose fields will be set.
* @param {string} formatted A date or datetime expressed in ISO 8601 format.
* @return {boolean} Whether the parsing succeeded.
*/
goog.date.setIso8601DateTime = function(dateTime, formatted) {
formatted = goog.string.trim(formatted);
var delim = formatted.indexOf('T') == -1 ? ' ' : 'T';
var parts = formatted.split(delim);
return goog.date.setIso8601DateOnly_(dateTime, parts[0]) &&
(parts.length < 2 || goog.date.setIso8601TimeOnly_(dateTime, parts[1]));
};
/**
* Sets date fields based on an ISO 8601 format string.
*
* @param {!goog.date.DateTime} d Object whose fields will be set.
* @param {string} formatted A date expressed in ISO 8601 format.
* @return {boolean} Whether the parsing succeeded.
* @private
*/
goog.date.setIso8601DateOnly_ = function(d, formatted) {
// split the formatted ISO date string into its date fields
var parts = formatted.match(goog.date.splitDateStringRegex_);
if (!parts) {
return false;
}
var year = Number(parts[1]);
var month = Number(parts[2]);
var date = Number(parts[3]);
var dayOfYear = Number(parts[4]);
var week = Number(parts[5]);
// ISO weekdays start with 1, native getDay() values start with 0
var dayOfWeek = Number(parts[6]) || 1;
d.setFullYear(year);
if (dayOfYear) {
d.setDate(1);
d.setMonth(0);
var offset = dayOfYear - 1; // offset, so 1-indexed, i.e., skip day 1
d.add(new goog.date.Interval(goog.date.Interval.DAYS, offset));
} else if (week) {
goog.date.setDateFromIso8601Week_(d, week, dayOfWeek);
} else {
if (month) {
d.setDate(1);
d.setMonth(month - 1);
}
if (date) {
d.setDate(date);
}
}
return true;
};
/**
* Sets date fields based on an ISO 8601 week string.
* See {@link http://en.wikipedia.org/wiki/ISO_week_date}, "Relation with the
* Gregorian Calendar". The first week of a new ISO year is the week with the
* majority of its days in the new Gregorian year. I.e., ISO Week 1's Thursday
* is in that year. ISO weeks always start on Monday. So ISO Week 1 can
* contain a few days from the previous Gregorian year. And ISO weeks always
* end on Sunday, so the last ISO week (Week 52 or 53) can have a few days from
* the following Gregorian year.
* Example: '1997-W01' lasts from 1996-12-30 to 1997-01-05. January 1, 1997 is
* a Wednesday. So W01's Monday is Dec.30, 1996, and Sunday is January 5, 1997.
*
* @param {goog.date.DateTime} d Object whose fields will be set.
* @param {number} week ISO week number.
* @param {number} dayOfWeek ISO day of week.
* @private
*/
goog.date.setDateFromIso8601Week_ = function(d, week, dayOfWeek) {
// calculate offset for first week
d.setMonth(0);
d.setDate(1);
var jsDay = d.getDay();
// switch Sunday (0) to index 7; ISO days are 1-indexed
var jan1WeekDay = jsDay || 7;
var THURSDAY = 4;
if (jan1WeekDay <= THURSDAY) {
// was extended back to Monday
var startDelta = 1 - jan1WeekDay; // e.g., Thu(4) ==> -3
} else {
// was extended forward to Monday
startDelta = 8 - jan1WeekDay; // e.g., Fri(5) ==> +3
}
// find the absolute number of days to offset from the start of year
// to arrive close to the Gregorian equivalent (pending adjustments above)
// Note: decrement week multiplier by one because 1st week is
// represented by dayOfWeek value
var absoluteDays = Number(dayOfWeek) + (7 * (Number(week) - 1));
// convert from ISO weekday format to Gregorian calendar date
// note: subtract 1 because 1-indexed; offset should not include 1st of month
var delta = startDelta + absoluteDays - 1;
var interval = new goog.date.Interval(goog.date.Interval.DAYS, delta);
d.add(interval);
};
/**
* Sets time fields based on an ISO 8601 format string.
* Note: only time fields, not date fields.
*
* @param {!goog.date.DateTime} d Object whose fields will be set.
* @param {string} formatted A time expressed in ISO 8601 format.
* @return {boolean} Whether the parsing succeeded.
* @private
*/
goog.date.setIso8601TimeOnly_ = function(d, formatted) {
// first strip timezone info from the end
var parts = formatted.match(goog.date.splitTimezoneStringRegex_);
var offset = 0; // local time if no timezone info
if (parts) {
if (parts[0] != 'Z') {
offset = Number(parts[2]) * 60 + Number(parts[3]);
offset *= parts[1] == '-' ? 1 : -1;
}
offset -= d.getTimezoneOffset();
formatted = formatted.substr(0, formatted.length - parts[0].length);
}
// then work out the time
parts = formatted.match(goog.date.splitTimeStringRegex_);
if (!parts) {
return false;
}
d.setHours(Number(parts[1]));
d.setMinutes(Number(parts[2]) || 0);
d.setSeconds(Number(parts[3]) || 0);
d.setMilliseconds(parts[4] ? Number(parts[4]) * 1000 : 0);
if (offset != 0) {
// adjust the date and time according to the specified timezone
d.setTime(d.getTime() + offset * 60000);
}
return true;
};
/**
* Class representing a date/time interval. Used for date calculations.
* <pre>
* new goog.date.Interval(0, 1) // One month
* new goog.date.Interval(0, 0, 3, 1) // Three days and one hour
* new goog.date.Interval(goog.date.Interval.DAYS, 1) // One day
* </pre>
*
* @param {number|string=} opt_years Years or string representing date part.
* @param {number=} opt_months Months or number of whatever date part specified
* by first parameter.
* @param {number=} opt_days Days.
* @param {number=} opt_hours Hours.
* @param {number=} opt_minutes Minutes.
* @param {number=} opt_seconds Seconds.
* @constructor
* @struct
* @final
*/
goog.date.Interval = function(
opt_years, opt_months, opt_days, opt_hours, opt_minutes, opt_seconds) {
if (goog.isString(opt_years)) {
var type = opt_years;
var interval = /** @type {number} */ (opt_months);
this.years = type == goog.date.Interval.YEARS ? interval : 0;
this.months = type == goog.date.Interval.MONTHS ? interval : 0;
this.days = type == goog.date.Interval.DAYS ? interval : 0;
this.hours = type == goog.date.Interval.HOURS ? interval : 0;
this.minutes = type == goog.date.Interval.MINUTES ? interval : 0;
this.seconds = type == goog.date.Interval.SECONDS ? interval : 0;
} else {
this.years = /** @type {number} */ (opt_years) || 0;
this.months = opt_months || 0;
this.days = opt_days || 0;
this.hours = opt_hours || 0;
this.minutes = opt_minutes || 0;
this.seconds = opt_seconds || 0;
}
};
/**
* Parses an XML Schema duration (ISO 8601 extended).
* @see http://www.w3.org/TR/xmlschema-2/#duration
*
* @param {string} duration An XML schema duration in textual format.
* Recurring durations and weeks are not supported.
* @return {goog.date.Interval} The duration as a goog.date.Interval or null
* if the parse fails.
*/
goog.date.Interval.fromIsoString = function(duration) {
var parts = duration.match(goog.date.splitDurationRegex_);
if (!parts) {
return null;
}
var timeEmpty = !(parts[6] || parts[7] || parts[8]);
var dateTimeEmpty = timeEmpty && !(parts[2] || parts[3] || parts[4]);
if (dateTimeEmpty || timeEmpty && parts[5]) {
return null;
}
var negative = parts[1];
var years = parseInt(parts[2], 10) || 0;
var months = parseInt(parts[3], 10) || 0;
var days = parseInt(parts[4], 10) || 0;
var hours = parseInt(parts[6], 10) || 0;
var minutes = parseInt(parts[7], 10) || 0;
var seconds = parseFloat(parts[8]) || 0;
return negative ?
new goog.date.Interval(
-years, -months, -days, -hours, -minutes, -seconds) :
new goog.date.Interval(years, months, days, hours, minutes, seconds);
};
/**
* Serializes goog.date.Interval into XML Schema duration (ISO 8601 extended).
* @see http://www.w3.org/TR/xmlschema-2/#duration
*
* @param {boolean=} opt_verbose Include zero fields in the duration string.
* @return {?string} An XML schema duration in ISO 8601 extended format,
* or null if the interval contains both positive and negative fields.
*/
goog.date.Interval.prototype.toIsoString = function(opt_verbose) {
var minField = Math.min(
this.years, this.months, this.days, this.hours, this.minutes,
this.seconds);
var maxField = Math.max(
this.years, this.months, this.days, this.hours, this.minutes,
this.seconds);
if (minField < 0 && maxField > 0) {
return null;
}
// Return 0 seconds if all fields are zero.
if (!opt_verbose && minField == 0 && maxField == 0) {
return 'PT0S';
}
var res = [];
// Add sign and 'P' prefix.
if (minField < 0) {
res.push('-');
}
res.push('P');
// Add date.
if (this.years || opt_verbose) {
res.push(Math.abs(this.years) + 'Y');
}
if (this.months || opt_verbose) {
res.push(Math.abs(this.months) + 'M');
}
if (this.days || opt_verbose) {
res.push(Math.abs(this.days) + 'D');
}
// Add time.
if (this.hours || this.minutes || this.seconds || opt_verbose) {
res.push('T');
if (this.hours || opt_verbose) {
res.push(Math.abs(this.hours) + 'H');
}
if (this.minutes || opt_verbose) {
res.push(Math.abs(this.minutes) + 'M');
}
if (this.seconds || opt_verbose) {
res.push(Math.abs(this.seconds) + 'S');
}
}
return res.join('');
};
/**
* Tests whether the given interval is equal to this interval.
* Note, this is a simple field-by-field comparison, it doesn't
* account for comparisons like "12 months == 1 year".
*
* @param {goog.date.Interval} other The interval to test.
* @return {boolean} Whether the intervals are equal.
*/
goog.date.Interval.prototype.equals = function(other) {
return other.years == this.years && other.months == this.months &&
other.days == this.days && other.hours == this.hours &&
other.minutes == this.minutes && other.seconds == this.seconds;
};
/**
* @return {!goog.date.Interval} A clone of the interval object.
*/
goog.date.Interval.prototype.clone = function() {
return new goog.date.Interval(
this.years, this.months, this.days, this.hours, this.minutes,
this.seconds);
};
/**
* Years constant for the date parts.
* @type {string}
*/
goog.date.Interval.YEARS = 'y';
/**
* Months constant for the date parts.
* @type {string}
*/
goog.date.Interval.MONTHS = 'm';
/**
* Days constant for the date parts.
* @type {string}
*/
goog.date.Interval.DAYS = 'd';
/**
* Hours constant for the date parts.
* @type {string}
*/
goog.date.Interval.HOURS = 'h';
/**
* Minutes constant for the date parts.
* @type {string}
*/
goog.date.Interval.MINUTES = 'n';
/**
* Seconds constant for the date parts.
* @type {string}
*/
goog.date.Interval.SECONDS = 's';
/**
* @return {boolean} Whether all fields of the interval are zero.
*/
goog.date.Interval.prototype.isZero = function() {
return this.years == 0 && this.months == 0 && this.days == 0 &&
this.hours == 0 && this.minutes == 0 && this.seconds == 0;
};
/**
* @return {!goog.date.Interval} Negative of this interval.
*/
goog.date.Interval.prototype.getInverse = function() {
return this.times(-1);
};
/**
* Calculates n * (this interval) by memberwise multiplication.
* @param {number} n An integer.
* @return {!goog.date.Interval} n * this.
*/
goog.date.Interval.prototype.times = function(n) {
return new goog.date.Interval(
this.years * n, this.months * n, this.days * n, this.hours * n,
this.minutes * n, this.seconds * n);
};
/**
* Gets the total number of seconds in the time interval. Assumes that months
* and years are empty.
* @return {number} Total number of seconds in the interval.
*/
goog.date.Interval.prototype.getTotalSeconds = function() {
goog.asserts.assert(this.years == 0 && this.months == 0);
return ((this.days * 24 + this.hours) * 60 + this.minutes) * 60 +
this.seconds;
};
/**
* Adds the Interval in the argument to this Interval field by field.
*
* @param {goog.date.Interval} interval The Interval to add.
*/
goog.date.Interval.prototype.add = function(interval) {
this.years += interval.years;
this.months += interval.months;
this.days += interval.days;
this.hours += interval.hours;
this.minutes += interval.minutes;
this.seconds += interval.seconds;
};
/**
* Class representing a date. Defaults to current date if none is specified.
*
* Implements most methods of the native js Date object (except the time related
* ones, {@see goog.date.DateTime}) and can be used interchangeably with it just
* as if goog.date.Date was a synonym of Date. To make this more transparent,
* Closure APIs should accept goog.date.DateLike instead of the real Date
* object.
*
* @param {number|goog.date.DateLike=} opt_year Four digit year or a date-like
* object. If not set, the created object will contain the date
* determined by goog.now().
* @param {number=} opt_month Month, 0 = Jan, 11 = Dec.
* @param {number=} opt_date Date of month, 1 - 31.
* @constructor
* @struct
* @see goog.date.DateTime
*/
goog.date.Date = function(opt_year, opt_month, opt_date) {
/** @protected {!Date} The wrapped date or datetime. */
this.date;
// goog.date.DateTime assumes that only this.date is added in this ctor.
if (goog.isNumber(opt_year)) {
this.date = this.buildDate_(opt_year, opt_month || 0, opt_date || 1);
this.maybeFixDst_(opt_date || 1);
} else if (goog.isObject(opt_year)) {
this.date = this.buildDate_(
opt_year.getFullYear(), opt_year.getMonth(), opt_year.getDate());
this.maybeFixDst_(opt_year.getDate());
} else {
this.date = new Date(goog.now());
var expectedDate = this.date.getDate();
this.date.setHours(0);
this.date.setMinutes(0);
this.date.setSeconds(0);
this.date.setMilliseconds(0);
// In some time zones there is no "0" hour on certain days during DST.
// Adjust here, if necessary. See:
// https://github.com/google/closure-library/issues/34.
this.maybeFixDst_(expectedDate);
}
};
/**
* new Date(y, m, d) treats years in the interval [0, 100) as two digit years,
* adding 1900 to them. This method ensures that calling the date constructor
* as a copy constructor returns a value that is equal to the passed in
* date value by explicitly setting the full year.
* @private
* @param {number} fullYear The full year (including century).
* @param {number} month The month, from 0-11.
* @param {number} date The day of the month.
* @return {!Date} The constructed Date object.
*/
goog.date.Date.prototype.buildDate_ = function(fullYear, month, date) {
var d = new Date(fullYear, month, date);
if (fullYear >= 0 && fullYear < 100) {
// Can't just setFullYear as new Date() can flip over for e.g. month = 13.
d.setFullYear(d.getFullYear() - 1900);
}
return d;
};
/**
* First day of week. 0 = Mon, 6 = Sun.
* @type {number}
* @private
*/
goog.date.Date.prototype.firstDayOfWeek_ =
goog.i18n.DateTimeSymbols.FIRSTDAYOFWEEK;
/**
* The cut off weekday used for week number calculations. 0 = Mon, 6 = Sun.
* @type {number}
* @private
*/
goog.date.Date.prototype.firstWeekCutOffDay_ =
goog.i18n.DateTimeSymbols.FIRSTWEEKCUTOFFDAY;
/**
* @return {!goog.date.Date} A clone of the date object.
*/
goog.date.Date.prototype.clone = function() {
var date = new goog.date.Date(this.date);
date.firstDayOfWeek_ = this.firstDayOfWeek_;
date.firstWeekCutOffDay_ = this.firstWeekCutOffDay_;
return date;
};
/**
* @return {number} The four digit year of date.
*/
goog.date.Date.prototype.getFullYear = function() {
return this.date.getFullYear();
};
/**
* Alias for getFullYear.
*
* @return {number} The four digit year of date.
* @see #getFullYear
*/
goog.date.Date.prototype.getYear = function() {
return this.getFullYear();
};
/**
* @return {goog.date.month} The month of date, 0 = Jan, 11 = Dec.
*/
goog.date.Date.prototype.getMonth = function() {
return /** @type {goog.date.month} */ (this.date.getMonth());
};
/**
* @return {number} The date of month.
*/
goog.date.Date.prototype.getDate = function() {
return this.date.getDate();
};
/**
* Returns the number of milliseconds since 1 January 1970 00:00:00.
*
* @return {number} The number of milliseconds since 1 January 1970 00:00:00.
*/
goog.date.Date.prototype.getTime = function() {
return this.date.getTime();
};
/**
* @return {number} The day of week, US style. 0 = Sun, 6 = Sat.
*/
goog.date.Date.prototype.getDay = function() {
return this.date.getDay();
};
/**
* @return {goog.date.weekDay} The day of week, ISO style. 0 = Mon, 6 = Sun.
*/
goog.date.Date.prototype.getIsoWeekday = function() {
return /** @type {goog.date.weekDay} */ ((this.getDay() + 6) % 7);
};
/**
* @return {number} The day of week according to firstDayOfWeek setting.
*/
goog.date.Date.prototype.getWeekday = function() {
return (this.getIsoWeekday() - this.firstDayOfWeek_ + 7) % 7;
};
/**
* @return {number} The four digit year of date according to universal time.
*/
goog.date.Date.prototype.getUTCFullYear = function() {
return this.date.getUTCFullYear();
};
/**
* @return {goog.date.month} The month of date according to universal time,
* 0 = Jan, 11 = Dec.
*/
goog.date.Date.prototype.getUTCMonth = function() {
return /** @type {goog.date.month} */ (this.date.getUTCMonth());
};
/**
* @return {number} The date of month according to universal time.
*/
goog.date.Date.prototype.getUTCDate = function() {
return this.date.getUTCDate();
};
/**
* @return {number} The day of week according to universal time, US style.
* 0 = Sun, 1 = Mon, 6 = Sat.
*/
goog.date.Date.prototype.getUTCDay = function() {
return this.date.getDay();
};
/**
* @return {number} The hours value according to universal time.
*/
goog.date.Date.prototype.getUTCHours = function() {
return this.date.getUTCHours();
};
/**
* @return {number} The minutes value according to universal time.
*/
goog.date.Date.prototype.getUTCMinutes = function() {
return this.date.getUTCMinutes();
};
/**
* @return {goog.date.weekDay} The day of week according to universal time, ISO
* style. 0 = Mon, 6 = Sun.
*/
goog.date.Date.prototype.getUTCIsoWeekday = function() {
return /** @type {goog.date.weekDay} */ ((this.date.getUTCDay() + 6) % 7);
};
/**
* @return {number} The day of week according to universal time and
* firstDayOfWeek setting.
*/
goog.date.Date.prototype.getUTCWeekday = function() {
return (this.getUTCIsoWeekday() - this.firstDayOfWeek_ + 7) % 7;
};
/**
* @return {number} The first day of the week. 0 = Mon, 6 = Sun.
*/
goog.date.Date.prototype.getFirstDayOfWeek = function() {
return this.firstDayOfWeek_;
};
/**
* @return {number} The cut off weekday used for week number calculations.
* 0 = Mon, 6 = Sun.
*/
goog.date.Date.prototype.getFirstWeekCutOffDay = function() {
return this.firstWeekCutOffDay_;
};
/**
* @return {number} The number of days for the selected month.
*/
goog.date.Date.prototype.getNumberOfDaysInMonth = function() {
return goog.date.getNumberOfDaysInMonth(this.getFullYear(), this.getMonth());
};
/**
* @return {number} The week number.
*/
goog.date.Date.prototype.getWeekNumber = function() {
return goog.date.getWeekNumber(
this.getFullYear(), this.getMonth(), this.getDate(),
this.firstWeekCutOffDay_, this.firstDayOfWeek_);
};
/**
* @return {number} The day of year.
*/
goog.date.Date.prototype.getDayOfYear = function() {
var dayOfYear = this.getDate();
var year = this.getFullYear();
for (var m = this.getMonth() - 1; m >= 0; m--) {
dayOfYear += goog.date.getNumberOfDaysInMonth(year, m);
}