Debian Codegraph @

closure-library/closure/goog/date/date_test.js

raw
1 // Copyright 2006 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.dateTest');
16 goog.setTestOnly('goog.dateTest');
17
18 goog.require('goog.array');
19 goog.require('goog.date');
20 goog.require('goog.date.Date');
21 goog.require('goog.date.DateTime');
22 goog.require('goog.date.Interval');
23 goog.require('goog.date.month');
24 goog.require('goog.date.weekDay');
25 goog.require('goog.i18n.DateTimeSymbols');
26 goog.require('goog.testing.ExpectedFailures');
27 goog.require('goog.testing.jsunit');
28 goog.require('goog.userAgent');
29 goog.require('goog.userAgent.platform');
30 goog.require('goog.userAgent.product');
31 goog.require('goog.userAgent.product.isVersion');
32
33 var expectedFailures;
34
35 function shouldRunTests() {
36 // Test disabled in Chrome-vista due to flakiness. See b/2753939.
37 if (goog.userAgent.product.CHROME && goog.userAgent.WINDOWS &&
38 goog.userAgent.platform.VERSION == '6.0') {
39 return false;
40 }
41
42 return true;
43 }
44
45 function setUpPage() {
46 expectedFailures = new goog.testing.ExpectedFailures();
47 }
48
49 function tearDown() {
50 expectedFailures.handleTearDown();
51 }
52
53
54 /**
55 * Unit test for Closure's 'goog.date'.
56 */
57 function testIsLeapYear() {
58 var f = goog.date.isLeapYear;
59
60 assertFalse('Year 1900 was not a leap year (the 100 rule)', f(1900));
61 assertFalse('Year 1901 was not a leap year (the 100 rule)', f(1901));
62 assertTrue('Year 1904 was a leap year', f(1904));
63 assertFalse('Year 1999 was not a leap year', f(1999));
64 assertTrue('Year 2000 was a leap year (the 400 rule)', f(2000));
65 assertTrue('Year 2004 was a leap year', f(2004));
66 assertFalse('Year 2006 was not a leap year', f(2006));
67 }
68
69
70 function testIsLongIsoYear() {
71 var f = goog.date.isLongIsoYear;
72
73 // see http://en.wikipedia.org/wiki/ISO_week_date#The_leap_year_cycle
74 assertFalse('1902 was not an ISO leap year', f(1902));
75 assertTrue('1903 was an ISO leap year', f(1903));
76 assertFalse('1904 was not an ISO leap year', f(1904));
77 assertTrue('1981 was an ISO leap year', f(1981));
78 assertTrue('1987 was an ISO leap year', f(1987));
79 assertFalse('2000 was not an ISO leap year', f(2000));
80 }
81
82
83 function testGetNumberOfDaysInMonth() {
84 var f = goog.date.getNumberOfDaysInMonth;
85
86 assertEquals('January has 31 days', f(2006, goog.date.month.JAN, 2000), 31);
87 assertEquals('February has 28 days', f(2006, goog.date.month.FEB), 28);
88 assertEquals(
89 'February has 29 days (leap year)', f(2008, goog.date.month.FEB), 29);
90 assertEquals('March has 31 days', f(2006, goog.date.month.MAR), 31);
91 assertEquals('April has 30 days', f(2006, goog.date.month.APR), 30);
92 assertEquals('May has 31 days', f(2006, goog.date.month.MAY), 31);
93 assertEquals('June has 30 days', f(2006, goog.date.month.JUN), 30);
94 assertEquals('July has 31 days', f(2006, goog.date.month.JUL), 31);
95 assertEquals('August has 31 days', f(2006, goog.date.month.AUG), 31);
96 assertEquals('September has 30 days', f(2006, goog.date.month.SEP), 30);
97 assertEquals('October has 31 days', f(2006, goog.date.month.OCT), 31);
98 assertEquals('November has 30 days', f(2006, goog.date.month.NOV), 30);
99 assertEquals('December has 31 days', f(2006, goog.date.month.DEC), 31);
100 }
101
102
103 function testIsSameDay() {
104 assertTrue(
105 'Dates are on the same day',
106 goog.date.isSameDay(
107 new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
108
109 assertFalse(
110 'Days are different',
111 goog.date.isSameDay(
112 new Date('2009/02/01 12:45:12'), new Date('2009/02/02 01:15:49')));
113
114 assertFalse(
115 'Months are different',
116 goog.date.isSameDay(
117 new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
118
119 assertFalse(
120 'Years are different',
121 goog.date.isSameDay(
122 new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
123
124 assertFalse(
125 'Wrong millennium',
126 goog.date.isSameDay(
127 new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
128 }
129
130
131 function testIsSameMonth() {
132 assertTrue(
133 'Dates are on the same day',
134 goog.date.isSameMonth(
135 new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
136
137 assertTrue(
138 'Dates are in the same month',
139 goog.date.isSameMonth(
140 new Date('2009/02/01 12:45:12'), new Date('2009/02/10 01:15:49')));
141
142 assertFalse(
143 'Month is different',
144 goog.date.isSameMonth(
145 new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
146
147 assertFalse(
148 'Year is different',
149 goog.date.isSameMonth(
150 new Date('2008/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
151
152 assertFalse(
153 'Wrong millennium',
154 goog.date.isSameMonth(
155 new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
156 }
157
158
159 function testIsSameYear() {
160 assertTrue(
161 'Dates are on the same day',
162 goog.date.isSameYear(
163 new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
164
165 assertTrue(
166 'Only days are different',
167 goog.date.isSameYear(
168 new Date('2009/02/01 12:45:12'), new Date('2009/02/11 01:15:49')));
169
170 assertTrue(
171 'Only months are different',
172 goog.date.isSameYear(
173 new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
174
175 assertFalse(
176 'Years are different',
177 goog.date.isSameYear(
178 new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
179
180 assertFalse(
181 'Years are different',
182 goog.date.isSameYear(
183 new Date('2009/02/01 12:45:12'), new Date('2008/02/01 01:15:49')));
184 }
185
186
187 function testGetWeekNumber() {
188 var f = goog.date.getWeekNumber;
189
190 // Test cases from http://en.wikipedia.org/wiki/ISO_week_date#Examples
191 assertEquals(
192 '2005-01-01 is the week 53 of the previous year', 53,
193 f(2005, goog.date.month.JAN, 1));
194 assertEquals(
195 '2005-01-02 is the week 53 of the previous year', 53,
196 f(2005, goog.date.month.JAN, 2));
197 assertEquals(
198 '2005-12-31 is the week 52', 52, f(2005, goog.date.month.DEC, 31));
199 assertEquals('2007-01-01 is the week 1', 1, f(2007, goog.date.month.JAN, 1));
200 assertEquals(
201 '2007-12-30 is the week 52', 52, f(2007, goog.date.month.DEC, 30));
202 assertEquals(
203 '2007-12-31 is the week 1 of the following year', 1,
204 f(2007, goog.date.month.DEC, 31));
205 assertEquals('2008-01-01 is the week 1', 1, f(2008, goog.date.month.JAN, 1));
206 assertEquals(
207 '2008-12-28 is the week 52', 52, f(2008, goog.date.month.DEC, 28));
208 assertEquals(
209 '2008-12-29 is the week 1 of the following year', 1,
210 f(2008, goog.date.month.DEC, 29));
211 assertEquals(
212 '2008-12-31 is the week 1 of the following year', 1,
213 f(2008, goog.date.month.DEC, 31));
214 assertEquals('2009-01-01 is the week 1', 1, f(2009, goog.date.month.JAN, 1));
215 assertEquals(
216 '2009-12-31 is the week 53 of the previous year', 53,
217 f(2009, goog.date.month.DEC, 31));
218 assertEquals(
219 '2010-01-01 is the week 53 of the previous year', 53,
220 f(2010, goog.date.month.JAN, 1));
221 assertEquals(
222 '2010-01-03 is the week 53 of the previous year', 53,
223 f(2010, goog.date.month.JAN, 3));
224 assertEquals('2010-01-04 is the week 1', 1, f(2010, goog.date.month.JAN, 4));
225
226 assertEquals(
227 '2006-01-01 is in week 52 of the following year', 52,
228 f(2006, goog.date.month.JAN, 1));
229 assertEquals('2006-01-02 is in week 1', 1, f(2006, goog.date.month.JAN, 2));
230 assertEquals(
231 '2006-10-16 is in week 42', 42, f(2006, goog.date.month.OCT, 16));
232 assertEquals(
233 '2006-10-19 is in week 42', 42, f(2006, goog.date.month.OCT, 19));
234 assertEquals(
235 '2006-10-22 is in week 42', 42, f(2006, goog.date.month.OCT, 22));
236 assertEquals(
237 '2006-10-23 is in week 43', 43, f(2006, goog.date.month.OCT, 23));
238 assertEquals(
239 '2008-12-29 is in week 1 of the following year', 1,
240 f(2008, goog.date.month.DEC, 29));
241 assertEquals(
242 '2010-01-03 is in week 53 of the previous year', 53,
243 f(2010, goog.date.month.JAN, 3));
244
245 assertEquals('2008-02-01 is in week 5', 5, f(2008, goog.date.month.FEB, 1));
246 assertEquals('2008-02-04 is in week 6', 6, f(2008, goog.date.month.FEB, 4));
247
248 // Tests for different cutoff days.
249 assertEquals(
250 '2006-01-01 is in week 52 of the prev. year (cutoff=Monday)', 52,
251 f(2006, goog.date.month.JAN, 1, goog.date.weekDay.MON));
252 assertEquals(
253 '2006-01-01 is in week 1 (cutoff=Sunday)', 1,
254 f(2006, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
255 assertEquals(
256 '2006-12-31 is in week 52 (cutoff=Monday)', 52,
257 f(2006, goog.date.month.DEC, 31, goog.date.weekDay.MON));
258 assertEquals(
259 '2006-12-31 is in week 53 (cutoff=Sunday)', 53,
260 f(2006, goog.date.month.DEC, 31, goog.date.weekDay.SUN));
261 assertEquals(
262 '2007-01-01 is in week 1 (cutoff=Monday)', 1,
263 f(2007, goog.date.month.JAN, 1, goog.date.weekDay.MON));
264 assertEquals(
265 '2007-01-01 is in week 1 (cutoff=Sunday)', 1,
266 f(2007, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
267 assertEquals(
268 '2015-01-01 is in week 52 of the previous year (cutoff=Monday)', 52,
269 f(2015, goog.date.month.JAN, 1, goog.date.weekDay.MON));
270
271 // Tests for leap year 2000.
272 assertEquals('2000-02-27 is in week 8', 8, f(2000, goog.date.month.FEB, 27));
273 assertEquals('2000-02-28 is in week 9', 9, f(2000, goog.date.month.FEB, 28));
274 assertEquals('2000-02-29 is in week 9', 9, f(2000, goog.date.month.FEB, 29));
275 assertEquals('2000-03-01 is in week 9', 9, f(2000, goog.date.month.MAR, 1));
276 assertEquals('2000-03-05 is in week 9', 9, f(2000, goog.date.month.MAR, 5));
277 assertEquals('2000-03-06 is in week 10', 10, f(2000, goog.date.month.MAR, 6));
278
279 // Check that week number is strictly incremented by 1.
280 var dt = new goog.date.Date(2008, goog.date.month.JAN, 1);
281 for (var i = 0; i < 52; ++i) {
282 var expected_week = i + 1;
283 assertEquals(
284 dt.toUTCIsoString(true) + ' is in week ' + expected_week, expected_week,
285 dt.getWeekNumber());
286 dt.add(new goog.date.Interval(goog.date.Interval.DAYS, 7));
287 }
288 }
289
290
291 function testFormatMonthAndYear() {
292 var f = goog.date.formatMonthAndYear;
293 assertEquals(
294 'January 2008',
295 f(goog.i18n.DateTimeSymbols.MONTHS[goog.date.month.JAN], 2008));
296 assertEquals(
297 'Jun 2007',
298 f(goog.i18n.DateTimeSymbols.SHORTMONTHS[goog.date.month.JUN], 2007));
299 }
300
301
302 //=== tests for goog.date.Date ===
303 function testIsDateLikeWithGoogDate() {
304 var jsDate = new Date();
305 var googDate = new goog.date.Date();
306 var string = 'foo';
307 var number = 1;
308 var nullVar = null;
309 var notDefined;
310
311 assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
312 assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
313 assertFalse('string should not be date-like', goog.isDateLike(string));
314 assertFalse('number should not be date-like', goog.isDateLike(number));
315 assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
316 assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
317 }
318
319
320 function testDateConstructor() {
321 var date = new goog.date.Date(2001, 2, 3);
322 assertEquals(2001, date.getFullYear());
323 assertEquals(2, date.getMonth());
324 assertEquals(3, date.getDate());
325
326 date = new goog.date.Date(2001);
327 assertEquals(2001, date.getFullYear());
328 assertEquals(0, date.getMonth());
329 assertEquals(1, date.getDate());
330
331 date = new goog.date.Date(new Date(2001, 2, 3, 4, 5, 6, 7));
332 assertEquals(2001, date.getFullYear());
333 assertEquals(2, date.getMonth());
334 assertEquals(3, date.getDate());
335 assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
336
337 goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
338 date = new goog.date.Date();
339 assertEquals(2001, date.getFullYear());
340 assertEquals(2, date.getMonth());
341 assertEquals(3, date.getDate());
342 assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
343 }
344
345
346 function testDateConstructor_yearBelow100() {
347 var date = new goog.date.Date(14, 7, 19);
348 assertEquals(
349 'Date constructor should respect passed in full year', 14,
350 date.getFullYear());
351
352 var copied = new goog.date.Date(date);
353 assertEquals(
354 'Copying a should return identical date', date.getTime(),
355 copied.getTime());
356 assertEquals(
357 'Full year should be left intact by copying', 14, copied.getFullYear());
358
359 // Test boundaries.
360 assertEquals(-1, new goog.date.Date(-1, 0, 1).getFullYear());
361 assertEquals(
362 'There is no year zero, but JS dates accept it', 0,
363 new goog.date.Date(0, 0, 1).getFullYear());
364 assertEquals(1, new goog.date.Date(1, 0, 1).getFullYear());
365 assertEquals(99, new goog.date.Date(99, 0, 1).getFullYear());
366 assertEquals(100, new goog.date.Date(100, 0, 1).getFullYear());
367 }
368
369
370 function testDateConstructor_flipOver() {
371 var date = new goog.date.Date(2012, 12, 1);
372 assertEquals('20130101', date.toIsoString());
373
374 date = new goog.date.Date(12, 12, 1);
375 assertEquals('130101', date.toIsoString());
376 }
377
378
379 function testDateToIsoString() {
380 var d = new goog.date.Date(2006, goog.date.month.JAN, 1);
381 assertEquals('1 Jan 2006 is 20060101', d.toIsoString(), '20060101');
382
383 d = new goog.date.Date(2007, goog.date.month.JUN, 12);
384 assertEquals('12 Jun 2007 is 20070612', d.toIsoString(), '20070612');
385
386 d = new goog.date.Date(2218, goog.date.month.DEC, 31);
387 assertEquals('31 Dec 2218 is 22181231', d.toIsoString(), '22181231');
388 }
389
390
391 function testDateTimeFromTimestamp() {
392 assertEquals(0, goog.date.DateTime.fromTimestamp(0).getTime());
393 assertEquals(1234, goog.date.DateTime.fromTimestamp(1234).getTime());
394 }
395
396
397 function testRfc822StringToDate() {
398 var date = goog.date.DateTime.fromRfc822String('October 2, 2002 8:00:00');
399 assertNotNull(date);
400 assertEquals(2002, date.getFullYear());
401 assertEquals(9, date.getMonth());
402 assertEquals(2, date.getDate());
403 assertEquals(8, date.getHours());
404 assertEquals(0, date.getMinutes());
405 assertEquals(0, date.getSeconds());
406 assertEquals(0, date.getMilliseconds());
407 assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
408
409 date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010 08:00:00 UTC');
410 assertEquals(2010, date.getFullYear());
411 assertEquals(9, date.getUTCMonth());
412 assertEquals(2, date.getUTCDate());
413 assertEquals(8, date.getUTCHours());
414 assertEquals(0, date.getUTCMinutes());
415 assertEquals(0, date.getUTCSeconds());
416 assertEquals(0, date.getUTCMilliseconds());
417
418 date = goog.date.DateTime.fromRfc822String('');
419 assertEquals(null, date);
420
421 date = goog.date.DateTime.fromRfc822String('Invalid Date String');
422 assertEquals(null, date);
423
424 date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010');
425 assertEquals(2010, date.getFullYear());
426 assertEquals(9, date.getMonth());
427 assertEquals(2, date.getDate());
428 assertEquals(0, date.getHours());
429 assertEquals(0, date.getMinutes());
430 assertEquals(0, date.getSeconds());
431 assertEquals(0, date.getMilliseconds());
432 assertEquals(new Date(2010, 9, 2).getTime(), date.getTime());
433 }
434
435 function testIsoStringToDate() {
436 var iso = '20060210T000000Z';
437 var date = goog.date.fromIsoString(iso);
438
439 assertEquals('Got 2006 from ' + iso, 2006, date.getFullYear());
440 assertEquals('Got February from ' + iso, 1, date.getMonth());
441 // use getUTCDate() here, since in 'iso' var we specified timezone
442 // as being a zero offset from GMT
443 assertEquals('Got 10th from ' + iso, 10, date.getUTCDate());
444
445 // YYYY-MM-DD
446 iso = '2005-02-22';
447 date = goog.date.fromIsoString(iso);
448 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
449 assertEquals('Got February from ' + iso, 1, date.getMonth());
450 assertEquals('Got 22nd from ' + iso, 22, date.getDate());
451
452 // YYYYMMDD
453 iso = '20050222';
454 date = goog.date.fromIsoString(iso);
455 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
456 assertEquals('Got February from ' + iso, 1, date.getMonth());
457 assertEquals('Got 22nd from ' + iso, 22, date.getDate());
458
459 // YYYY-MM
460 iso = '2005-08';
461 date = goog.date.fromIsoString(iso);
462 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
463 assertEquals('Got August from ' + iso, 7, date.getMonth());
464
465 // YYYYMM
466 iso = '200502';
467 date = goog.date.fromIsoString(iso);
468 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
469 assertEquals('Got February from ' + iso, 1, date.getMonth());
470
471 // YYYY
472 iso = '2005';
473 date = goog.date.fromIsoString(iso);
474 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
475
476 // 1997-W01 or 1997W01
477 iso = '2005-W22';
478 date = goog.date.fromIsoString(iso);
479 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
480 assertEquals('Got May from ' + iso, 4, date.getMonth());
481 assertEquals('Got 30th from ' + iso, 30, date.getDate());
482
483 iso = '2005W22';
484 date = goog.date.fromIsoString(iso);
485 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
486 assertEquals('Got May from ' + iso, 4, date.getMonth());
487 assertEquals('Got 30th from ' + iso, 30, date.getDate());
488
489 // 1997-W01-2 or 1997W012
490 iso = '2005-W22-4';
491 date = goog.date.fromIsoString(iso);
492 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
493 assertEquals('Got June from ' + iso, 5, date.getMonth());
494 assertEquals('Got 2nd from ' + iso, 2, date.getDate());
495
496 iso = '2005W224';
497 date = goog.date.fromIsoString(iso);
498 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
499 assertEquals('Got June from ' + iso, 5, date.getMonth());
500 assertEquals('Got 2nd from ' + iso, 2, date.getDate());
501
502 iso = '2004-W53-6';
503 date = goog.date.fromIsoString(iso);
504 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
505 assertEquals('Got January from ' + iso, 0, date.getMonth());
506 assertEquals('Got 1st from ' + iso, 1, date.getDate());
507
508 iso = '2004-W53-7';
509 date = goog.date.fromIsoString(iso);
510 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
511 assertEquals('Got January from ' + iso, 0, date.getMonth());
512 assertEquals('Got 2nd from ' + iso, 2, date.getDate());
513
514 iso = '2005-W52-6';
515 date = goog.date.fromIsoString(iso);
516 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
517 assertEquals('Got December from ' + iso, 11, date.getMonth());
518 assertEquals('Got 31st from ' + iso, 31, date.getDate());
519
520 // both years 2007 start with the same day
521 iso = '2007-W01-1';
522 date = goog.date.fromIsoString(iso);
523 assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
524 assertEquals('Got January from ' + iso, 0, date.getMonth());
525 assertEquals('Got 1st from ' + iso, 1, date.getDate());
526
527 iso = '2007-W52-7';
528 date = goog.date.fromIsoString(iso);
529 assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
530 assertEquals('Got December from ' + iso, 11, date.getMonth());
531 assertEquals('Got 30th from ' + iso, 30, date.getDate());
532
533 iso = '2008-W01-1';
534 date = goog.date.fromIsoString(iso);
535 assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
536 assertEquals('Got December from ' + iso, 11, date.getMonth());
537 assertEquals('Got 31st from ' + iso, 31, date.getDate());
538
539 // Gregorian year 2008 is a leap year,
540 // ISO year 2008 is 2 days shorter:
541 // 1 day longer at the start, 3 days shorter at the end
542 iso = '2008-W01-2';
543 date = goog.date.fromIsoString(iso);
544 assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
545 assertEquals('Got Jan from ' + iso, 0, date.getMonth());
546 assertEquals('Got 1st from ' + iso, 1, date.getDate());
547
548 // ISO year is three days into the previous gregorian year
549 iso = '2009-W01-1';
550 date = goog.date.fromIsoString(iso);
551 assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
552 assertEquals('Got December from ' + iso, 11, date.getMonth());
553 assertEquals('Got 29th from ' + iso, 29, date.getDate());
554
555 iso = '2009-W01-3';
556 date = goog.date.fromIsoString(iso);
557 assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
558 assertEquals('Got December from ' + iso, 11, date.getMonth());
559 assertEquals('Got 31st from ' + iso, 31, date.getDate());
560
561 iso = '2009-W01-4';
562 date = goog.date.fromIsoString(iso);
563 assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
564 assertEquals('Got January from ' + iso, 0, date.getMonth());
565 assertEquals('Got 1st from ' + iso, 1, date.getDate());
566
567 // ISO year is three days into the next gregorian year
568 iso = '2009-W53-4';
569 date = goog.date.fromIsoString(iso);
570 assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
571 assertEquals('Got December from ' + iso, 11, date.getMonth());
572 assertEquals('Got 31st from ' + iso, 31, date.getDate());
573
574 iso = '2009-W53-5';
575 date = goog.date.fromIsoString(iso);
576 assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
577 assertEquals('Got January from ' + iso, 0, date.getMonth());
578 assertEquals('Got 1st from ' + iso, 1, date.getDate());
579
580 iso = '2009-W53-6';
581 date = goog.date.fromIsoString(iso);
582 assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
583 assertEquals('Got January from ' + iso, 0, date.getMonth());
584 assertEquals('Got 2nd from ' + iso, 2, date.getDate());
585
586 iso = '2009-W53-7';
587 date = goog.date.fromIsoString(iso);
588 assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
589 assertEquals('Got January from ' + iso, 0, date.getMonth());
590 assertEquals('Got 3rd from ' + iso, 3, date.getDate());
591
592 iso = '2010-W01-1';
593 date = goog.date.fromIsoString(iso);
594 assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
595 assertEquals('Got January from ' + iso, 0, date.getMonth());
596 assertEquals('Got 4th from ' + iso, 4, date.getDate());
597
598 // Examples where the ISO year is three days into the previous gregorian year
599
600 // 1995-035 or 1995035
601 iso = '2005-146';
602 date = goog.date.fromIsoString(iso);
603 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
604 assertEquals('Got May from ' + iso, 4, date.getMonth());
605 assertEquals('Got 26th from ' + iso, 26, date.getDate());
606
607 iso = '2005146';
608 date = goog.date.fromIsoString(iso);
609 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
610 assertEquals('Got May from ' + iso, 4, date.getMonth());
611 assertEquals('Got 26th from ' + iso, 26, date.getDate());
612 }
613
614
615 function testDate_fromIsoString() {
616 // YYYY-MM-DD
617 var iso = '2005-02-22';
618 var date = goog.date.Date.fromIsoString(iso);
619 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
620 assertEquals('Got February from ' + iso, 1, date.getMonth());
621 assertEquals('Got 22nd from ' + iso, 22, date.getDate());
622
623 // YYYY-MM-DDTHH:MM:SS
624 iso = '2005-02-22T11:22:33';
625 date = goog.date.Date.fromIsoString(iso);
626 assertNull('Got null from ' + iso, date);
627 }
628
629 function testDateTime_fromIsoString() {
630 // YYYY-MM-DD
631 var iso = '2005-02-22';
632 var date = goog.date.DateTime.fromIsoString(iso);
633 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
634 assertEquals('Got February from ' + iso, 1, date.getMonth());
635 assertEquals('Got 22nd from ' + iso, 22, date.getDate());
636 assertEquals('Got 0 hours from ' + iso, 0, date.getHours());
637 assertEquals('Got 0 minutes from ' + iso, 0, date.getMinutes());
638 assertEquals('Got 0 seconds from ' + iso, 0, date.getSeconds());
639
640 // YYYY-MM-DDTHH:MM:SS
641 iso = '2005-02-22T11:22:33';
642 date = goog.date.DateTime.fromIsoString(iso);
643 assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
644 assertEquals('Got February from ' + iso, 1, date.getMonth());
645 assertEquals('Got 22nd from ' + iso, 22, date.getDate());
646 assertEquals('Got 11 hours from ' + iso, 11, date.getHours());
647 assertEquals('Got 22 minutes from ' + iso, 22, date.getMinutes());
648 assertEquals('Got 33 seconds from ' + iso, 33, date.getSeconds());
649 }
650
651
652 // test private function used by goog.date.Date.toIsoString()
653 function test_setIso8601TimeOnly_() {
654 if (goog.userAgent.product.SAFARI) {
655 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
656 // suite running in a continuous build. Will investigate later.
657 return;
658 }
659
660 // 23:59:59
661 var d = new goog.date.DateTime(0, 0);
662 var iso = '18:46:39';
663 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
664 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
665 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
666 assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
667
668 // 235959
669 d = new goog.date.DateTime(0, 0);
670 iso = '184639';
671 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
672 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
673 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
674 assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
675
676 // 23:59, 2359, or 23
677 d = new goog.date.DateTime(0, 0);
678 iso = '18:46';
679 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
680 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
681 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
682
683 d = new goog.date.DateTime(0, 0);
684 iso = '1846';
685 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
686 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
687 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
688
689 d = new goog.date.DateTime(0, 0);
690 iso = '18';
691 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
692 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
693
694 d = new goog.date.DateTime(0, 0);
695 iso = '18463';
696 assertFalse('failed to parse ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
697 assertTrue('date did not change', d.equals(new goog.date.DateTime(0, 0)));
698
699 // 23:59:59.9942 or 235959.9942
700 d = new goog.date.DateTime(0, 0);
701 iso = '18:46:39.9942';
702 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
703 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
704 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
705 assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
706
707 // Fails in Safari4 Winxp, temporarily disabled
708 expectedFailures.expectFailureFor(isWinxpSafari4());
709 try {
710 if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
711 // Both Safari 3.1 and WebKit (on Mac) return floating-point values.
712 assertRoughlyEquals(
713 'Got roughly 994.2 milliseconds from ' + iso, 994.2,
714 d.getMilliseconds(), 0.01);
715 } else {
716 // Other browsers, including WebKit on Windows, return integers.
717 assertEquals(
718 'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
719 }
720
721 d = new goog.date.DateTime(0, 0);
722 iso = '184639.9942';
723 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
724 assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
725 assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
726 assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
727 if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
728 // Both Safari 3.1 and WebKit (on Mac) return floating-point values.
729 assertRoughlyEquals(
730 'Got roughly 994.2 milliseconds from ' + iso, 994.2,
731 d.getMilliseconds(), 0.01);
732 } else {
733 // Other browsers, including WebKit on Windows, return integers.
734 assertEquals(
735 'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
736 }
737 } catch (e) {
738 expectedFailures.handleException(e);
739 }
740
741
742 // 1995-02-04 24:00 = 1995-02-05 00:00
743 // timezone tests
744 var offset = new Date().getTimezoneOffset() / 60;
745 d = new goog.date.DateTime(0, 0);
746 iso = '18:46:39+07:00';
747 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
748 assertEquals('Got an 11-hour GMT offset from ' + iso, 11, d.getUTCHours());
749
750 d = new goog.date.DateTime(0, 0);
751 iso = '18:46:39+00:00';
752 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
753 assertEquals('Got an 18-hour GMT offset from ' + iso, 18, d.getUTCHours());
754
755 d = new goog.date.DateTime(0, 0);
756 iso = '16:46:39-07:00';
757 assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
758 assertEquals('Got a 23-hour GMT offset from ' + iso, 23, d.getUTCHours());
759 }
760
761
762 function testDateIntervalAdd() {
763 // -1m, cross year boundary
764 var d = new goog.date.Date(2007, goog.date.month.JAN, 1);
765 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
766 assertEquals('2007-01-01 - 1m = 2006-12-01', '20061201', d.toIsoString());
767
768 // +1y2m3d
769 d = new goog.date.Date(2006, goog.date.month.JAN, 1);
770 d.add(new goog.date.Interval(1, 2, 3));
771 assertEquals('2006-01-01 + 1y2m3d = 2007-03-04', '20070304', d.toIsoString());
772
773 // -1y2m3d (negative interval)
774 d = new goog.date.Date(2007, goog.date.month.MAR, 4);
775 d.add(new goog.date.Interval(-1, -2, -3));
776 assertEquals('2007-03-04 - 1y2m3d = 2006-01-01', '20060101', d.toIsoString());
777
778 // 2007-12-30 + 3d (roll over to next year)
779 d = new goog.date.Date(2007, goog.date.month.DEC, 30);
780 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 3));
781 assertEquals('2007-12-30 + 3d = 2008-01-02', '20080102', d.toIsoString());
782
783 // 2004-03-01 - 1d (handle leap year)
784 d = new goog.date.Date(2004, goog.date.month.MAR, 1);
785 d.add(new goog.date.Interval(goog.date.Interval.DAYS, -1));
786 assertEquals('2004-03-01 - 1d = 2004-02-29', '20040229', d.toIsoString());
787
788 // 2004-02-29 + 1y (stays at end of Feb, doesn't roll to Mar)
789 d = new goog.date.Date(2004, goog.date.month.FEB, 29);
790 d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
791 assertEquals('2004-02-29 + 1y = 2005-02-28', '20050228', d.toIsoString());
792
793 // 2004-02-29 - 1y (stays at end of Feb, doesn't roll to Mar)
794 d = new goog.date.Date(2004, goog.date.month.FEB, 29);
795 d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
796 assertEquals('2004-02-29 - 1y = 2003-02-28', '20030228', d.toIsoString());
797
798 // 2003-02-28 + 1y (stays at 28, doesn't go to leap year end of Feb)
799 d = new goog.date.Date(2003, goog.date.month.FEB, 28);
800 d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
801 assertEquals('2003-02-28 + 1y = 2004-02-28', '20040228', d.toIsoString());
802
803 // 2005-02-28 - 1y (stays at 28, doesn't go to leap year end of Feb)
804 d = new goog.date.Date(2005, goog.date.month.FEB, 28);
805 d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
806 assertEquals('2005-02-28 - 1y = 2004-02-28', '20040228', d.toIsoString());
807
808 // 2003-01-31 + 1y (stays at end of Jan, standard case)
809 d = new goog.date.Date(2003, goog.date.month.JAN, 31);
810 d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
811 assertEquals('2003-01-31 + 1y = 2004-01-31', '20040131', d.toIsoString());
812
813 // 2005-01-31 - 1y (stays at end of Jan, standard case)
814 d = new goog.date.Date(2005, goog.date.month.JAN, 31);
815 d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
816 assertEquals('2005-01-31 - 1y = 2004-01-31', '20040131', d.toIsoString());
817
818 // 2006-01-31 + 1m (stays at end of Feb, doesn't roll to Mar, non-leap-year)
819 d = new goog.date.Date(2006, goog.date.month.JAN, 31);
820 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
821 assertEquals('2006-01-31 + 1m = 2006-02-28', '20060228', d.toIsoString());
822
823 // 2004-02-29 + 1m (stays at 29, standard case)
824 d = new goog.date.Date(2004, goog.date.month.FEB, 29);
825 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, +1));
826 assertEquals('2004-02-29 + 1m = 2004-03-29', '20040329', d.toIsoString());
827
828 // 2004-02-29 - 1m (stays at 29, standard case)
829 d = new goog.date.Date(2004, goog.date.month.FEB, 29);
830 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
831 assertEquals('2004-02-29 - 1m = 2004-01-29', '20040129', d.toIsoString());
832
833 // 2004-01-30 + 1m (snaps to Feb 29)
834 d = new goog.date.Date(2004, goog.date.month.JAN, 30);
835 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
836 assertEquals('2004-01-30 + 1m = 2004-02-29', '20040229', d.toIsoString());
837
838 // 2004-03-30 - 1m (snaps to Feb 29)
839 d = new goog.date.Date(2004, goog.date.month.MAR, 30);
840 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
841 assertEquals('2004-03-30 - 1m = 2004-02-29', '20040229', d.toIsoString());
842
843 // 2004-03-30 + 1m (stays at 30, standard case)
844 d = new goog.date.Date(2004, goog.date.month.MAR, 30);
845 d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
846 assertEquals('2004-03-30 + 1m = 2004-04-30', '20040430', d.toIsoString());
847
848 // 2008-10-30 + 1d (roll over to 31).
849 d = new goog.date.Date(2008, goog.date.month.OCT, 30);
850 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
851 assertEquals('2008-10-30 + 1d = 2008-10-31', '20081031', d.toIsoString());
852
853 // 2008-10-31 + 1d (roll over to November, not December).
854 d = new goog.date.Date(2008, goog.date.month.OCT, 31);
855 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
856 assertEquals('2008-10-31 + 1d = 2008-11-01', '20081101', d.toIsoString());
857
858 // 2008-10-17 + 1d (Brasilia dst).
859 d = new goog.date.Date(2008, goog.date.month.OCT, 17);
860 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
861 assertEquals('2008-10-17 + 1d = 2008-10-18', '20081018', d.toIsoString());
862
863 // 2008-10-18 + 1d (Brasilia dst).
864 d = new goog.date.Date(2008, goog.date.month.OCT, 18);
865 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
866 assertEquals('2008-10-18 + 1d = 2008-10-19', '20081019', d.toIsoString());
867
868 // 2008-10-19 + 1d (Brasilia dst).
869 d = new goog.date.Date(2008, goog.date.month.OCT, 19);
870 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
871 assertEquals('2008-10-19 + 1d = 2008-10-20', '20081020', d.toIsoString());
872
873 // 2008-02-16 + 1d (Brasilia dst).
874 d = new goog.date.Date(2008, goog.date.month.FEB, 16);
875 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
876 assertEquals('2008-02-16 + 1d = 2008-02-17', '20080217', d.toIsoString());
877
878 // 2008-02-17 + 1d (Brasilia dst).
879 d = new goog.date.Date(2008, goog.date.month.FEB, 17);
880 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
881 assertEquals('2008-02-17 + 1d = 2008-02-18', '20080218', d.toIsoString());
882 }
883
884
885 function testDateEquals() {
886 var d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
887 var d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
888 assertFalse('d1 != null', d1.equals(null));
889 assertFalse('d2 != undefined', d2.equals(undefined));
890 assertTrue('d1 == d2', d1.equals(d2));
891 assertTrue('d2 == d1', d2.equals(d1));
892
893 d1 = new goog.date.Date(2005, goog.date.month.MAR, 1);
894 d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
895 assertFalse('different year', d1.equals(d2));
896
897 d1 = new goog.date.Date(2004, goog.date.month.FEB, 1);
898 d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
899 assertFalse('different month', d1.equals(d2));
900
901 d1 = new goog.date.Date(2004, goog.date.month.MAR, 2);
902 d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
903 assertFalse('different date', d1.equals(d2));
904
905 // try passing in DateTime, time fields should be ignored
906 d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
907 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
908 assertTrue('using goog.date.DateTime, same date', d1.equals(d2));
909 }
910
911
912 function testDateTimeConstructor() {
913 var date = new goog.date.DateTime(2001, 2, 3, 4, 5, 6, 7);
914 assertEquals(2001, date.getFullYear());
915 assertEquals(2, date.getMonth());
916 assertEquals(3, date.getDate());
917 assertEquals(4, date.getHours());
918 assertEquals(5, date.getMinutes());
919 assertEquals(6, date.getSeconds());
920 assertEquals(7, date.getMilliseconds());
921 assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
922
923 date = new goog.date.DateTime(2001);
924 assertEquals(2001, date.getFullYear());
925 assertEquals(0, date.getMonth());
926 assertEquals(1, date.getDate());
927 assertEquals(0, date.getHours());
928 assertEquals(0, date.getMinutes());
929 assertEquals(0, date.getSeconds());
930 assertEquals(0, date.getMilliseconds());
931
932 date = new goog.date.DateTime(new Date(2001, 2, 3, 4, 5, 6, 7));
933 assertEquals(2001, date.getFullYear());
934 assertEquals(2, date.getMonth());
935 assertEquals(3, date.getDate());
936 assertEquals(4, date.getHours());
937 assertEquals(5, date.getMinutes());
938 assertEquals(6, date.getSeconds());
939 assertEquals(7, date.getMilliseconds());
940 assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
941
942 goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
943 date = new goog.date.DateTime();
944 assertEquals(2001, date.getFullYear());
945 assertEquals(2, date.getMonth());
946 assertEquals(3, date.getDate());
947 assertEquals(4, date.getHours());
948 assertEquals(0, date.getMinutes());
949 assertEquals(0, date.getSeconds());
950 assertEquals(0, date.getMilliseconds());
951 assertEquals(new Date(2001, 2, 3, 4).getTime(), date.getTime());
952
953 date = new goog.date.DateTime(new Date('October 2, 2002 8:00:00'));
954 assertEquals(2002, date.getFullYear());
955 assertEquals(9, date.getMonth());
956 assertEquals(2, date.getDate());
957 assertEquals(8, date.getHours());
958 assertEquals(0, date.getMinutes());
959 assertEquals(0, date.getSeconds());
960 assertEquals(0, date.getMilliseconds());
961 assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
962 }
963
964
965 function testDateTimeEquals() {
966 var d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
967 var d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
968 assertTrue('d1 == d2', d1.equals(d2));
969 assertTrue('d2 == d1', d2.equals(d1));
970
971 d1 = new goog.date.DateTime(2007, goog.date.month.JAN, 1);
972 d2 = new goog.date.DateTime(); // today
973 assertFalse('different date', d1.equals(d2));
974
975 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1);
976 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12);
977 assertFalse('different hours', d1.equals(d2));
978
979 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 29, 30);
980 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
981 assertFalse('different minutes', d1.equals(d2));
982
983 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 29);
984 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
985 assertFalse('different seconds', d1.equals(d2));
986
987 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
988 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
989 assertTrue('same milliseconds', d1.equals(d2));
990
991 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 499);
992 d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
993 assertFalse('different milliseconds', d1.equals(d2));
994
995 // try milliseconds again, this time comparing against a native Date
996 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
997 d2 = new Date(2004, 2, 1, 12, 30, 30, 999);
998 assertFalse('different milliseconds, native Date', d1.equals(d2));
999
1000 // pass in a goog.date.Date rather than a goog.date.DateTime
1001 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
1002 d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
1003 assertFalse('using goog.date.Date, different times', d1.equals(d2));
1004
1005 d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 0, 0, 0);
1006 d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
1007 assertTrue('using goog.date.Date, same time (midnight)', d1.equals(d2));
1008 }
1009
1010
1011 function testIntervalIsZero() {
1012 assertTrue('zero interval', new goog.date.Interval().isZero());
1013 var i = new goog.date.Interval(0, 0, 1, -24, 0, 0);
1014 assertFalse('1 day minus 24 hours is not considered zero', i.isZero());
1015 }
1016
1017
1018 function testIntervalGetInverse() {
1019 var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
1020 var i2 = i1.getInverse();
1021
1022 var d = new goog.date.Date(2004, goog.date.month.MAR, 1);
1023 d.add(i1);
1024 d.add(i2);
1025 var label = '2004-03-01 - 1d + 1d = 2004-03-01';
1026 assertEquals(label, d.toIsoString(), '20040301');
1027
1028 i1 = new goog.date.Interval(1, 2, 3);
1029 i2 = i1.getInverse();
1030
1031 d = new goog.date.Date(2004, goog.date.month.MAR, 1);
1032 d.add(i1);
1033 d.add(i2);
1034 label = '2004-03-01 - 1y2m3d + 1y2m3d = 2004-03-01';
1035 assertEquals(label, d.toIsoString(), '20040301');
1036 }
1037
1038
1039 function testIntervalTimes() {
1040 var i = new goog.date.Interval(1, 2, 3, 4, 5, 6);
1041 var expected = new goog.date.Interval(2, 4, 6, 8, 10, 12);
1042 assertTrue('double interval', expected.equals(i.times(2)));
1043 }
1044
1045
1046 //=== tests for goog.date.Interval.equals() ===
1047 function testIntervalEquals() {
1048 var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
1049 var i2 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
1050 assertTrue('-1d == -1d, aka i1 == i2', i1.equals(i2));
1051 assertTrue('-1d == -1d, aka i2 == i1', i2.equals(i1));
1052
1053 i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
1054 i2 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
1055 assertFalse('-1d != +1d, aka i1 == i2', i1.equals(i2));
1056 assertFalse('-1d != +1d, aka i2 == i1', i2.equals(i1));
1057
1058 i1 = new goog.date.Interval(0, 3); // Three months
1059 i2 = new goog.date.Interval(goog.date.Interval.MONTHS, 3);
1060 assertTrue('3m == 3m, aka i1 == i2', i1.equals(i2));
1061 assertTrue('3m == 3m, aka i2 == i1', i2.equals(i1));
1062
1063 // 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
1064 i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1065 i2 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1066 assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i1.equals(i2));
1067 assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i2.equals(i1));
1068 }
1069
1070
1071 //=== tests for adding two goog.date.Interval intervals ===
1072 function testIntervalIntervalAdd() {
1073 var i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1074 var i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
1075 i1.add(i2);
1076 assertTrue('i1 + i2', i1.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
1077
1078 i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1079 i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
1080 i2.add(i1);
1081 assertTrue('i2 + i1', i2.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
1082
1083 i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1084 i2 = i1.getInverse();
1085 i1.add(i2);
1086 assertTrue('i1 + (-i1)', i1.equals(new goog.date.Interval()));
1087
1088 i1 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
1089 i2 = new goog.date.Interval(0, -2, -2);
1090 i1.add(i2);
1091 assertTrue('1d + (-2m-2d)', i1.equals(new goog.date.Interval(0, -2, -1)));
1092 }
1093
1094
1095 //=== tests conversion to and from ISO 8601 duration string ===
1096 function testIsoDuration() {
1097 var interval1 = new goog.date.Interval(123, 456, 678, 11, 12, 455.5);
1098 var duration1 = 'P123Y456M678DT11H12M455.5S';
1099 assertTrue(
1100 'parse full duration',
1101 interval1.equals(goog.date.Interval.fromIsoString(duration1)));
1102 assertEquals('create full duration', duration1, interval1.toIsoString());
1103
1104 var interval2 = new goog.date.Interval(123);
1105 var duration2 = 'P123Y';
1106 var duration2v = 'P123Y0M0DT0H0M0S';
1107 assertTrue(
1108 'parse year',
1109 interval2.equals(goog.date.Interval.fromIsoString(duration2)));
1110 assertEquals('create year', duration2, interval2.toIsoString());
1111 assertEquals('create year, verbose', duration2v, interval2.toIsoString(true));
1112
1113 var interval3 = new goog.date.Interval(0, 0, 0, 11, 12, 40);
1114 var duration3 = 'PT11H12M40S';
1115 var duration3v = 'P0Y0M0DT11H12M40S';
1116 assertTrue(
1117 'parse time duration',
1118 interval3.equals(goog.date.Interval.fromIsoString(duration3)));
1119 assertEquals('create time duration', duration3, interval3.toIsoString());
1120 assertEquals(
1121 'create time duration, verbove', duration3v, interval3.toIsoString(true));
1122
1123 var interval4 = new goog.date.Interval(7, 8, 9, 1, 2, 4);
1124 var duration4 = 'P7Y8M9DT1H2M4S';
1125 assertTrue(
1126 'parse one-digit duration',
1127 interval4.equals(goog.date.Interval.fromIsoString(duration4)));
1128 assertEquals('create one-digit duration', duration4, interval4.toIsoString());
1129
1130 var interval5 = new goog.date.Interval(-123, -456, -678, -11, -12, -455.5);
1131 var duration5 = '-P123Y456M678DT11H12M455.5S';
1132 assertTrue(
1133 'parse full negative duration',
1134 interval5.equals(goog.date.Interval.fromIsoString(duration5)));
1135 assertEquals(
1136 'create full negative duration', duration5, interval5.toIsoString());
1137
1138 var interval6 = new goog.date.Interval(0, 0, -1);
1139 var duration6 = '-P1D';
1140 var duration6v = '-P0Y0M1DT0H0M0S';
1141 assertTrue(
1142 'parse partial negative duration',
1143 interval6.equals(goog.date.Interval.fromIsoString(duration6)));
1144 assertEquals(
1145 'create partial negative duration', duration6, interval6.toIsoString());
1146 assertEquals(
1147 'create partial negative duration, verbose', duration6v,
1148 interval6.toIsoString(true));
1149
1150 var interval7 = new goog.date.Interval(0, 0, 9, 0, 0, 4);
1151 var duration7 = 'P9DT4S';
1152 var duration7v = 'P0Y0M9DT0H0M4S';
1153 assertTrue(
1154 'parse partial one-digit duration',
1155 interval7.equals(goog.date.Interval.fromIsoString(duration7)));
1156 assertTrue(
1157 'parse partial one-digit duration, verbose',
1158 interval7.equals(goog.date.Interval.fromIsoString(duration7v)));
1159 assertEquals(
1160 'create partial one-digit duration', duration7, interval7.toIsoString());
1161 assertEquals(
1162 'create partial one-digit duration, verbose', duration7v,
1163 interval7.toIsoString(true));
1164
1165 var interval8 = new goog.date.Interval(1, -1, 1, -1, 1, -1);
1166 assertNull('create mixed sign duration', interval8.toIsoString());
1167
1168 var duration9 = '1Y1M1DT1H1M1S';
1169 assertNull('missing P', goog.date.Interval.fromIsoString(duration9));
1170
1171 var duration10 = 'P1Y1M1D1H1M1S';
1172 assertNull('missing T', goog.date.Interval.fromIsoString(duration10));
1173
1174 var duration11 = 'P1Y1M1DT';
1175 assertNull('extra T', goog.date.Interval.fromIsoString(duration11));
1176
1177 var duration12 = 'PT.5S';
1178 assertNull(
1179 'invalid seconds, missing integer part',
1180 goog.date.Interval.fromIsoString(duration12));
1181
1182 var duration13 = 'PT1.S';
1183 assertNull(
1184 'invalid seconds, missing fractional part',
1185 goog.date.Interval.fromIsoString(duration13));
1186 }
1187
1188
1189 function testGetTotalSeconds() {
1190 var duration = new goog.date.Interval(0, 0, 2, 3, 4, 5);
1191 assertEquals(
1192 'seconds in 2d3h4m5s', 2 * 86400 + 3 * 3600 + 4 * 60 + 5,
1193 duration.getTotalSeconds());
1194 }
1195
1196
1197 function testIsDateLikeWithGoogDateTime() {
1198 var jsDate = new Date();
1199 var googDate = new goog.date.DateTime();
1200 var string = 'foo';
1201 var number = 1;
1202 var nullVar = null;
1203 var notDefined;
1204
1205 assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
1206 assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
1207 assertFalse('string should not be date-like', goog.isDateLike(string));
1208 assertFalse('number should not be date-like', goog.isDateLike(number));
1209 assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
1210 assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
1211 }
1212
1213
1214 function testToUTCRfc3339String() {
1215 var date = goog.date.fromIsoString('19850412T232050Z');
1216 date.setUTCMilliseconds(52);
1217 assertEquals(date.toUTCRfc3339String(), '1985-04-12T23:20:50.052Z');
1218 assertNotEquals(
1219 'Diverges from ISO 8601', date.toUTCRfc3339String(),
1220 date.toUTCIsoString(true, true));
1221
1222 var date = goog.date.fromIsoString('19901231T235959Z');
1223 assertEquals(date.toUTCRfc3339String(), '1990-12-31T23:59:59Z');
1224 assertNotEquals(
1225 'Diverges from ISO 8601', date.toUTCRfc3339String(),
1226 date.toUTCIsoString(true, true));
1227
1228 var date = goog.date.fromIsoString('19370101T120027Z');
1229 date.setUTCMilliseconds(87);
1230 assertEquals(date.toUTCRfc3339String(), '1937-01-01T12:00:27.087Z');
1231 assertNotEquals(
1232 'Diverges from ISO 8601', date.toUTCRfc3339String(),
1233 date.toUTCIsoString(true, true));
1234 }
1235
1236
1237 function testDateTimezone() {
1238 var d = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
1239 d.add(
1240 new goog.date.Interval(
1241 goog.date.Interval.MINUTES, d.getTimezoneOffset()));
1242 var d2 = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
1243 assertEquals(
1244 'Compensate for timezone and compare with UTC date/time',
1245 d.toIsoString(true), d2.toUTCIsoString(true));
1246 }
1247
1248
1249 function testToUsTimeString() {
1250 var doPad = true;
1251 var doShowPm = true;
1252 var dontPad = false;
1253 var dontShowPm = false;
1254
1255 // 12am
1256 var d = new goog.date.DateTime(2007, 1, 14);
1257 assertEquals('12am test 1', '12:00 AM', d.toUsTimeString());
1258 assertEquals('12am test 2', '12:00 AM', d.toUsTimeString(doPad));
1259 assertEquals('12am test 3', '12:00 AM', d.toUsTimeString(dontPad));
1260 assertEquals('12am test 4', '12:00 AM', d.toUsTimeString(doPad, doShowPm));
1261 assertEquals('12am test 5', '00:00', d.toUsTimeString(doPad, dontShowPm));
1262 assertEquals('12am test 6', '12:00 AM', d.toUsTimeString(dontPad, doShowPm));
1263 assertEquals('12am test 7', '0:00', d.toUsTimeString(dontPad, dontShowPm));
1264
1265 // 9am
1266 d = new goog.date.DateTime(2007, 1, 14, 9);
1267 assertEquals('9am test 1', '9:00 AM', d.toUsTimeString());
1268 assertEquals('9am test 2', '09:00 AM', d.toUsTimeString(doPad));
1269 assertEquals('9am test 3', '9:00 AM', d.toUsTimeString(dontPad));
1270 assertEquals('9am test 4', '09:00 AM', d.toUsTimeString(doPad, doShowPm));
1271 assertEquals('9am test 5', '09:00', d.toUsTimeString(doPad, dontShowPm));
1272 assertEquals('9am test 6', '9:00 AM', d.toUsTimeString(dontPad, doShowPm));
1273 assertEquals('9am test 7', '9:00', d.toUsTimeString(dontPad, dontShowPm));
1274
1275 // 12pm
1276 d = new goog.date.DateTime(2007, 1, 14, 12);
1277 assertEquals('12pm test 1', '12:00 PM', d.toUsTimeString());
1278 assertEquals('12pm test 2', '12:00 PM', d.toUsTimeString(doPad));
1279 assertEquals('12pm test 3', '12:00 PM', d.toUsTimeString(dontPad));
1280 assertEquals('12pm test 4', '12:00 PM', d.toUsTimeString(doPad, doShowPm));
1281 assertEquals('12pm test 5', '12:00', d.toUsTimeString(doPad, dontShowPm));
1282 assertEquals('12pm test 6', '12:00 PM', d.toUsTimeString(dontPad, doShowPm));
1283 assertEquals('12pm test 7', '12:00', d.toUsTimeString(dontPad, dontShowPm));
1284
1285 // 6 PM
1286 d = new goog.date.DateTime(2007, 1, 14, 18);
1287 assertEquals('6 PM test 1', '6:00 PM', d.toUsTimeString());
1288 assertEquals('6 PM test 2', '06:00 PM', d.toUsTimeString(doPad));
1289 assertEquals('6 PM test 3', '6:00 PM', d.toUsTimeString(dontPad));
1290 assertEquals('6 PM test 4', '06:00 PM', d.toUsTimeString(doPad, doShowPm));
1291 assertEquals('6 PM test 5', '06:00', d.toUsTimeString(doPad, dontShowPm));
1292 assertEquals('6 PM test 6', '6:00 PM', d.toUsTimeString(dontPad, doShowPm));
1293 assertEquals('6 PM test 7', '6:00', d.toUsTimeString(dontPad, dontShowPm));
1294
1295 // 6:01 PM
1296 d = new goog.date.DateTime(2007, 1, 14, 18, 1);
1297 assertEquals('6:01 PM test 1', '6:01 PM', d.toUsTimeString());
1298 assertEquals('6:01 PM test 2', '06:01 PM', d.toUsTimeString(doPad));
1299 assertEquals('6:01 PM test 3', '6:01 PM', d.toUsTimeString(dontPad));
1300 assertEquals('6:01 PM test 4', '06:01 PM', d.toUsTimeString(doPad, doShowPm));
1301 assertEquals('6:01 PM test 5', '06:01', d.toUsTimeString(doPad, dontShowPm));
1302 assertEquals(
1303 '6:01 PM test 6', '6:01 PM', d.toUsTimeString(dontPad, doShowPm));
1304 assertEquals('6:01 PM test 7', '6:01', d.toUsTimeString(dontPad, dontShowPm));
1305
1306 // 6:35 PM
1307 d = new goog.date.DateTime(2007, 1, 14, 18, 35);
1308 assertEquals('6:35 PM test 1', '6:35 PM', d.toUsTimeString());
1309 assertEquals('6:35 PM test 2', '06:35 PM', d.toUsTimeString(doPad));
1310 assertEquals('6:35 PM test 3', '6:35 PM', d.toUsTimeString(dontPad));
1311 assertEquals('6:35 PM test 4', '06:35 PM', d.toUsTimeString(doPad, doShowPm));
1312 assertEquals('6:35 PM test 5', '06:35', d.toUsTimeString(doPad, dontShowPm));
1313 assertEquals(
1314 '6:35 PM test 6', '6:35 PM', d.toUsTimeString(dontPad, doShowPm));
1315 assertEquals('6:35 PM test 7', '6:35', d.toUsTimeString(dontPad, dontShowPm));
1316
1317 // omit zero minutes
1318 d = new goog.date.DateTime(2007, 1, 14, 18);
1319 assertEquals(
1320 'omit zero 1', '6:00 PM', d.toUsTimeString(dontPad, doShowPm, false));
1321 assertEquals(
1322 'omit zero 2', '6 PM', d.toUsTimeString(dontPad, doShowPm, true));
1323
1324 // but don't omit zero minutes if not actually zero minutes
1325 d = new goog.date.DateTime(2007, 1, 14, 18, 1);
1326 assertEquals(
1327 'omit zero 3', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, false));
1328 assertEquals(
1329 'omit zero 4', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, true));
1330 }
1331
1332
1333 function testToIsoTimeString() {
1334 // 00:00
1335 var d = new goog.date.DateTime(2007, 1, 14);
1336 assertEquals('00:00', '00:00:00', d.toIsoTimeString());
1337
1338 // 09:00
1339 d = new goog.date.DateTime(2007, 1, 14, 9);
1340 assertEquals('09:00', '09:00:00', d.toIsoTimeString());
1341
1342 // 12:00
1343 d = new goog.date.DateTime(2007, 1, 14, 12);
1344 assertEquals('12:00', '12:00:00', d.toIsoTimeString());
1345
1346 // 18:00
1347 d = new goog.date.DateTime(2007, 1, 14, 18);
1348 assertEquals('18:00', '18:00:00', d.toIsoTimeString());
1349
1350 // 18:01
1351 d = new goog.date.DateTime(2007, 1, 14, 18, 1);
1352 assertEquals('18:01', '18:01:00', d.toIsoTimeString());
1353
1354 // 18:35
1355 d = new goog.date.DateTime(2007, 1, 14, 18, 35);
1356 assertEquals('18:35', '18:35:00', d.toIsoTimeString());
1357
1358 // 18:35:01
1359 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
1360 assertEquals('18:35:01', '18:35:01', d.toIsoTimeString());
1361
1362 // 18:35:11
1363 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
1364 assertEquals('18:35:11', '18:35:11', d.toIsoTimeString());
1365
1366 // 18:35:11 >> 18:35
1367 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
1368 assertEquals('18:35:11 no secs', '18:35', d.toIsoTimeString(false));
1369 }
1370
1371
1372 function testToXmlDateTimeString() {
1373 var d = new goog.date.DateTime(2007, 1, 14);
1374 assertEquals('2007-02-14', '2007-02-14T00:00:00', d.toXmlDateTime());
1375
1376 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
1377 assertEquals(
1378 '2007-02-14, 8:35:01, timezone==undefined', '2007-02-14T18:35:01',
1379 d.toXmlDateTime());
1380
1381 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
1382 assertEquals(
1383 '2007-02-14, 8:35:01, timezone==false', '2007-02-14T18:35:01',
1384 d.toXmlDateTime(false));
1385
1386 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
1387 assertEquals(
1388 '2007-02-14, 8:35:01, timezone==true',
1389 '2007-02-14T18:35:01' + d.getTimezoneOffsetString(),
1390 d.toXmlDateTime(true));
1391 }
1392
1393
1394 function testClone() {
1395 var d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
1396 var d2 = d.clone();
1397 assertTrue('datetimes equal', d.equals(d2));
1398
1399 d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1, 310);
1400 d2 = d.clone();
1401 assertTrue('datetimes with milliseconds equal', d.equals(d2));
1402
1403 d = new goog.date.Date(2007, 1, 14);
1404 d2 = d.clone();
1405 assertTrue('dates equal', d.equals(d2));
1406
1407 // 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
1408 var i = new goog.date.Interval(1, 6, 15, 12, 30, 30);
1409 var i2 = i.clone();
1410 assertTrue('intervals equal', i.equals(i2));
1411
1412 i = new goog.date.Interval(goog.date.Interval.DAYS, -1);
1413 i2 = i.clone();
1414 assertTrue('day intervals equal', i.equals(i2));
1415
1416 // Brasilia dst
1417 d = new goog.date.Date(2008, goog.date.month.OCT, 18);
1418 d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
1419 d2 = d.clone();
1420 assertTrue('dates equal', d.equals(d2));
1421 }
1422
1423 function testValueOf() {
1424 var date1 = new goog.date.DateTime(2008, 11, 26, 15, 40, 0);
1425 var date2 = new goog.date.Date(2008, 11, 27);
1426 var date3 = new goog.date.DateTime(2008, 11, 26, 15, 40, 1);
1427 var nativeDate = new Date();
1428 nativeDate.setFullYear(2008, 11, 26);
1429 nativeDate.setHours(15, 40, 0, 0);
1430 assertEquals(date1.valueOf(), nativeDate.valueOf());
1431 assertFalse(date1 < date1);
1432 assertTrue(date1 <= date1);
1433 assertTrue(date1 < date2);
1434 assertTrue(date2 > date3);
1435 }
1436
1437 function isWinxpSafari4() {
1438 return goog.userAgent.product.SAFARI &&
1439 goog.userAgent.product.isVersion('4') &&
1440 !goog.userAgent.product.isVersion('5') && goog.userAgent.WINDOWS &&
1441 goog.userAgent.platform.isVersion('5.0') &&
1442 !goog.userAgent.platform.isVersion('6.0');
1443 }
1444
1445 function testDateCompare() {
1446 // May 16th, 2011, 3:17:36.500
1447 var date1 =
1448 new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
1449
1450 // May 16th, 2011, 3:17:36.501
1451 var date2 =
1452 new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 501);
1453
1454 // May 16th, 2011, 3:17:36.501
1455 var date3 =
1456 new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 502);
1457
1458 assertEquals(0, goog.date.Date.compare(date1.clone(), date1.clone()));
1459 assertEquals(-1, goog.date.Date.compare(date1, date2));
1460 assertEquals(1, goog.date.Date.compare(date2, date1));
1461
1462 var dates = [date2, date3, date1];
1463 goog.array.sort(dates, goog.date.Date.compare);
1464 assertArrayEquals(
1465 'Dates should be sorted in time.', [date1, date2, date3], dates);
1466
1467 // Assert a known millisecond difference between two points in time.
1468 assertEquals(
1469 -19129478,
1470 goog.date.Date.compare(
1471 new goog.date.DateTime(1982, goog.date.month.MAR, 12, 6, 48, 32, 354),
1472 new goog.date.DateTime(
1473 1982, goog.date.month.MAR, 12, 12, 7, 21, 832)));
1474
1475 // Test dates before the year 0. Dates are Talk Like a Pirate Day, and
1476 // Towel Day, 300 B.C. (and before pirates).
1477
1478 var pirateDay = new goog.date.Date(-300, goog.date.month.SEP, 2);
1479 var towelDay = new goog.date.Date(-300, goog.date.month.MAY, 12);
1480
1481 assertEquals(
1482 'Dates should be 113 days apart.', 113 * 24 * 60 * 60 * 1000,
1483 goog.date.Date.compare(pirateDay, towelDay));
1484 }
1485
1486 function testDateCompareDateLikes() {
1487 var nativeDate = new Date(2011, 4, 16, 15, 17, 36, 500);
1488 var closureDate =
1489 new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
1490
1491 assertEquals(0, goog.date.Date.compare(nativeDate, closureDate));
1492
1493 nativeDate.setMilliseconds(499);
1494 assertEquals(-1, goog.date.Date.compare(nativeDate, closureDate));
1495
1496 nativeDate.setMilliseconds(501);
1497 assertEquals(1, goog.date.Date.compare(nativeDate, closureDate));
1498 }
1499
1500 function testIsMidnight() {
1501 assertTrue(new goog.date.DateTime(2013, 0, 1).isMidnight());
1502 assertFalse(new goog.date.DateTime(2013, 0, 1, 1).isMidnight());
1503 assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 1).isMidnight());
1504 assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 1).isMidnight());
1505 assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 0, 1).isMidnight());
1506 }
1507
1508 function testMinMax() {
1509 // Comparison of two goog.date.DateTimes
1510 var dateTime1 = new goog.date.DateTime(2000, 0, 1);
1511 var dateTime2 = new goog.date.DateTime(2000, 0, 1, 0, 0, 0, 1);
1512 assertEquals(dateTime1, goog.date.min(dateTime1, dateTime2));
1513 assertEquals(dateTime1, goog.date.min(dateTime2, dateTime1));
1514 assertEquals(dateTime2, goog.date.max(dateTime1, dateTime2));
1515 assertEquals(dateTime2, goog.date.max(dateTime2, dateTime1));
1516
1517 // Comparison of two goog.date.Dates
1518 var date1 = new goog.date.Date(2000, 0, 1);
1519 var date2 = new goog.date.Date(2000, 0, 2);
1520 assertEquals(date1, goog.date.min(date1, date2));
1521
1522 // Comparison of native Dates.
1523 var jsDate1 = new Date(2000, 0, 1);
1524 var jsDate2 = new Date(2000, 0, 2);
1525 assertEquals(jsDate1, goog.date.min(jsDate1, jsDate2));
1526 assertEquals(jsDate2, goog.date.max(jsDate1, jsDate2));
1527
1528 // Comparison of different types.
1529 assertEquals(date1, goog.date.min(date1, dateTime2));
1530 assertEquals(dateTime2, goog.date.min(date2, dateTime2));
1531 assertEquals(date1, goog.date.min(date1, jsDate2));
1532 assertEquals(jsDate2, goog.date.max(dateTime1, jsDate2));
1533 }
1534
1535 function testDateTimeIntervalAdd() {
1536 // Add hours
1537 var d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 10, 20, 30);
1538 d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
1539 assertEquals(20, d.getHours());
1540
1541 // Add negative hours
1542 d.add(new goog.date.Interval(goog.date.Interval.HOURS, -5));
1543 assertEquals(15, d.getHours());
1544
1545 // Add hours to the next day
1546 d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
1547 assertEquals(2, d.getDay());
1548 assertEquals(1, d.getHours());
1549
1550 // Add minutes
1551 d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 22, 20, 30);
1552 d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 10));
1553 assertEquals(30, d.getMinutes());
1554
1555 // Add negative minutes
1556 d.add(new goog.date.Interval(goog.date.Interval.MINUTES, -5));
1557 assertEquals(25, d.getMinutes());
1558
1559 // Add minutes to the next day
1560 d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 130));
1561 assertEquals(2, d.getDay());
1562 assertEquals(0, d.getHours());
1563 assertEquals(35, d.getMinutes());
1564
1565 // Add seconds
1566 d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 23, 45, 30);
1567 d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 10));
1568 assertEquals(40, d.getSeconds());
1569
1570 // Add negative seconds
1571 d.add(new goog.date.Interval(goog.date.Interval.SECONDS, -5));
1572 assertEquals(35, d.getSeconds());
1573
1574 // Add seconds to the next day
1575 d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 1200));
1576 assertEquals(2, d.getDay());
1577 assertEquals(0, d.getHours());
1578 assertEquals(5, d.getMinutes());
1579 assertEquals(35, d.getSeconds());
1580
1581 // Test daylight savings day 2015-11-1
1582 d = new goog.date.DateTime(2015, goog.date.month.NOV, 1, 0, 50, 30);
1583 d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
1584 assertEquals(1, d.getHours());
1585 assertEquals(5, d.getMinutes());
1586
1587 d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
1588 assertEquals(1, d.getHours());
1589
1590 // Test daylight savings day 2015-3-8
1591 d = new goog.date.DateTime(2015, goog.date.month.MAR, 8, 0, 50, 30);
1592 d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
1593 assertEquals(1, d.getHours());
1594 assertEquals(5, d.getMinutes());
1595
1596 d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
1597 assertEquals(3, d.getHours());
1598 }