1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561 |
- // 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.
- goog.provide('goog.dateTest');
- goog.setTestOnly('goog.dateTest');
- goog.require('goog.array');
- goog.require('goog.date');
- goog.require('goog.date.Date');
- goog.require('goog.date.DateTime');
- goog.require('goog.date.Interval');
- goog.require('goog.date.month');
- goog.require('goog.date.weekDay');
- goog.require('goog.i18n.DateTimeSymbols');
- goog.require('goog.testing.ExpectedFailures');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- goog.require('goog.userAgent.platform');
- goog.require('goog.userAgent.product');
- goog.require('goog.userAgent.product.isVersion');
- var expectedFailures;
- function shouldRunTests() {
- // Test disabled in Chrome-vista due to flakiness. See b/2753939.
- if (goog.userAgent.product.CHROME && goog.userAgent.WINDOWS &&
- goog.userAgent.platform.VERSION == '6.0') {
- return false;
- }
- return true;
- }
- function setUpPage() {
- expectedFailures = new goog.testing.ExpectedFailures();
- }
- function tearDown() {
- expectedFailures.handleTearDown();
- }
- /**
- * Unit test for Closure's 'goog.date'.
- */
- function testIsLeapYear() {
- var f = goog.date.isLeapYear;
- assertFalse('Year 1900 was not a leap year (the 100 rule)', f(1900));
- assertFalse('Year 1901 was not a leap year (the 100 rule)', f(1901));
- assertTrue('Year 1904 was a leap year', f(1904));
- assertFalse('Year 1999 was not a leap year', f(1999));
- assertTrue('Year 2000 was a leap year (the 400 rule)', f(2000));
- assertTrue('Year 2004 was a leap year', f(2004));
- assertFalse('Year 2006 was not a leap year', f(2006));
- }
- function testIsLongIsoYear() {
- var f = goog.date.isLongIsoYear;
- // see http://en.wikipedia.org/wiki/ISO_week_date#The_leap_year_cycle
- assertFalse('1902 was not an ISO leap year', f(1902));
- assertTrue('1903 was an ISO leap year', f(1903));
- assertFalse('1904 was not an ISO leap year', f(1904));
- assertTrue('1981 was an ISO leap year', f(1981));
- assertTrue('1987 was an ISO leap year', f(1987));
- assertFalse('2000 was not an ISO leap year', f(2000));
- }
- function testGetNumberOfDaysInMonth() {
- var f = goog.date.getNumberOfDaysInMonth;
- assertEquals('January has 31 days', f(2006, goog.date.month.JAN, 2000), 31);
- assertEquals('February has 28 days', f(2006, goog.date.month.FEB), 28);
- assertEquals(
- 'February has 29 days (leap year)', f(2008, goog.date.month.FEB), 29);
- assertEquals('March has 31 days', f(2006, goog.date.month.MAR), 31);
- assertEquals('April has 30 days', f(2006, goog.date.month.APR), 30);
- assertEquals('May has 31 days', f(2006, goog.date.month.MAY), 31);
- assertEquals('June has 30 days', f(2006, goog.date.month.JUN), 30);
- assertEquals('July has 31 days', f(2006, goog.date.month.JUL), 31);
- assertEquals('August has 31 days', f(2006, goog.date.month.AUG), 31);
- assertEquals('September has 30 days', f(2006, goog.date.month.SEP), 30);
- assertEquals('October has 31 days', f(2006, goog.date.month.OCT), 31);
- assertEquals('November has 30 days', f(2006, goog.date.month.NOV), 30);
- assertEquals('December has 31 days', f(2006, goog.date.month.DEC), 31);
- }
- function testIsSameDay() {
- assertTrue(
- 'Dates are on the same day',
- goog.date.isSameDay(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
- assertFalse(
- 'Days are different',
- goog.date.isSameDay(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/02 01:15:49')));
- assertFalse(
- 'Months are different',
- goog.date.isSameDay(
- new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
- assertFalse(
- 'Years are different',
- goog.date.isSameDay(
- new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
- assertFalse(
- 'Wrong millennium',
- goog.date.isSameDay(
- new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
- }
- function testIsSameMonth() {
- assertTrue(
- 'Dates are on the same day',
- goog.date.isSameMonth(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
- assertTrue(
- 'Dates are in the same month',
- goog.date.isSameMonth(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/10 01:15:49')));
- assertFalse(
- 'Month is different',
- goog.date.isSameMonth(
- new Date('2009/02/01 12:45:12'), new Date('2009/03/01 01:15:49')));
- assertFalse(
- 'Year is different',
- goog.date.isSameMonth(
- new Date('2008/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
- assertFalse(
- 'Wrong millennium',
- goog.date.isSameMonth(
- new Date('2009/02/01 12:45:12'), new Date('1009/02/01 01:15:49')));
- }
- function testIsSameYear() {
- assertTrue(
- 'Dates are on the same day',
- goog.date.isSameYear(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
- assertTrue(
- 'Only days are different',
- goog.date.isSameYear(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/11 01:15:49')));
- assertTrue(
- 'Only months are different',
- goog.date.isSameYear(
- new Date('2009/02/01 12:45:12'), new Date('2009/02/01 01:15:49')));
- assertFalse(
- 'Years are different',
- goog.date.isSameYear(
- new Date('2009/02/01 12:45:12'), new Date('2010/02/01 01:15:49')));
- assertFalse(
- 'Years are different',
- goog.date.isSameYear(
- new Date('2009/02/01 12:45:12'), new Date('2008/02/01 01:15:49')));
- }
- function testGetWeekNumber() {
- var f = goog.date.getWeekNumber;
- // Test cases from http://en.wikipedia.org/wiki/ISO_week_date#Examples
- assertEquals(
- '2005-01-01 is the week 53 of the previous year', 53,
- f(2005, goog.date.month.JAN, 1));
- assertEquals(
- '2005-01-02 is the week 53 of the previous year', 53,
- f(2005, goog.date.month.JAN, 2));
- assertEquals(
- '2005-12-31 is the week 52', 52, f(2005, goog.date.month.DEC, 31));
- assertEquals('2007-01-01 is the week 1', 1, f(2007, goog.date.month.JAN, 1));
- assertEquals(
- '2007-12-30 is the week 52', 52, f(2007, goog.date.month.DEC, 30));
- assertEquals(
- '2007-12-31 is the week 1 of the following year', 1,
- f(2007, goog.date.month.DEC, 31));
- assertEquals('2008-01-01 is the week 1', 1, f(2008, goog.date.month.JAN, 1));
- assertEquals(
- '2008-12-28 is the week 52', 52, f(2008, goog.date.month.DEC, 28));
- assertEquals(
- '2008-12-29 is the week 1 of the following year', 1,
- f(2008, goog.date.month.DEC, 29));
- assertEquals(
- '2008-12-31 is the week 1 of the following year', 1,
- f(2008, goog.date.month.DEC, 31));
- assertEquals('2009-01-01 is the week 1', 1, f(2009, goog.date.month.JAN, 1));
- assertEquals(
- '2009-12-31 is the week 53 of the previous year', 53,
- f(2009, goog.date.month.DEC, 31));
- assertEquals(
- '2010-01-01 is the week 53 of the previous year', 53,
- f(2010, goog.date.month.JAN, 1));
- assertEquals(
- '2010-01-03 is the week 53 of the previous year', 53,
- f(2010, goog.date.month.JAN, 3));
- assertEquals('2010-01-04 is the week 1', 1, f(2010, goog.date.month.JAN, 4));
- assertEquals(
- '2006-01-01 is in week 52 of the following year', 52,
- f(2006, goog.date.month.JAN, 1));
- assertEquals('2006-01-02 is in week 1', 1, f(2006, goog.date.month.JAN, 2));
- assertEquals(
- '2006-10-16 is in week 42', 42, f(2006, goog.date.month.OCT, 16));
- assertEquals(
- '2006-10-19 is in week 42', 42, f(2006, goog.date.month.OCT, 19));
- assertEquals(
- '2006-10-22 is in week 42', 42, f(2006, goog.date.month.OCT, 22));
- assertEquals(
- '2006-10-23 is in week 43', 43, f(2006, goog.date.month.OCT, 23));
- assertEquals(
- '2008-12-29 is in week 1 of the following year', 1,
- f(2008, goog.date.month.DEC, 29));
- assertEquals(
- '2010-01-03 is in week 53 of the previous year', 53,
- f(2010, goog.date.month.JAN, 3));
- assertEquals('2008-02-01 is in week 5', 5, f(2008, goog.date.month.FEB, 1));
- assertEquals('2008-02-04 is in week 6', 6, f(2008, goog.date.month.FEB, 4));
- // Tests for different cutoff days.
- assertEquals(
- '2006-01-01 is in week 52 of the prev. year (cutoff=Monday)', 52,
- f(2006, goog.date.month.JAN, 1, goog.date.weekDay.MON));
- assertEquals(
- '2006-01-01 is in week 1 (cutoff=Sunday)', 1,
- f(2006, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
- assertEquals(
- '2006-12-31 is in week 52 (cutoff=Monday)', 52,
- f(2006, goog.date.month.DEC, 31, goog.date.weekDay.MON));
- assertEquals(
- '2006-12-31 is in week 53 (cutoff=Sunday)', 53,
- f(2006, goog.date.month.DEC, 31, goog.date.weekDay.SUN));
- assertEquals(
- '2007-01-01 is in week 1 (cutoff=Monday)', 1,
- f(2007, goog.date.month.JAN, 1, goog.date.weekDay.MON));
- assertEquals(
- '2007-01-01 is in week 1 (cutoff=Sunday)', 1,
- f(2007, goog.date.month.JAN, 1, goog.date.weekDay.SUN));
- assertEquals(
- '2015-01-01 is in week 52 of the previous year (cutoff=Monday)', 52,
- f(2015, goog.date.month.JAN, 1, goog.date.weekDay.MON));
- // Tests for leap year 2000.
- assertEquals('2000-02-27 is in week 8', 8, f(2000, goog.date.month.FEB, 27));
- assertEquals('2000-02-28 is in week 9', 9, f(2000, goog.date.month.FEB, 28));
- assertEquals('2000-02-29 is in week 9', 9, f(2000, goog.date.month.FEB, 29));
- assertEquals('2000-03-01 is in week 9', 9, f(2000, goog.date.month.MAR, 1));
- assertEquals('2000-03-05 is in week 9', 9, f(2000, goog.date.month.MAR, 5));
- assertEquals('2000-03-06 is in week 10', 10, f(2000, goog.date.month.MAR, 6));
- // Check that week number is strictly incremented by 1.
- var dt = new goog.date.Date(2008, goog.date.month.JAN, 1);
- for (var i = 0; i < 52; ++i) {
- var expected_week = i + 1;
- assertEquals(
- dt.toUTCIsoString(true) + ' is in week ' + expected_week, expected_week,
- dt.getWeekNumber());
- dt.add(new goog.date.Interval(goog.date.Interval.DAYS, 7));
- }
- }
- function testFormatMonthAndYear() {
- var f = goog.date.formatMonthAndYear;
- assertEquals(
- 'January 2008',
- f(goog.i18n.DateTimeSymbols.MONTHS[goog.date.month.JAN], 2008));
- assertEquals(
- 'Jun 2007',
- f(goog.i18n.DateTimeSymbols.SHORTMONTHS[goog.date.month.JUN], 2007));
- }
- //=== tests for goog.date.Date ===
- function testIsDateLikeWithGoogDate() {
- var jsDate = new Date();
- var googDate = new goog.date.Date();
- var string = 'foo';
- var number = 1;
- var nullVar = null;
- var notDefined;
- assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
- assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
- assertFalse('string should not be date-like', goog.isDateLike(string));
- assertFalse('number should not be date-like', goog.isDateLike(number));
- assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
- assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
- }
- function testDateConstructor() {
- var date = new goog.date.Date(2001, 2, 3);
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- date = new goog.date.Date(2001);
- assertEquals(2001, date.getFullYear());
- assertEquals(0, date.getMonth());
- assertEquals(1, date.getDate());
- date = new goog.date.Date(new Date(2001, 2, 3, 4, 5, 6, 7));
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
- goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
- date = new goog.date.Date();
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- assertEquals(new Date(2001, 2, 3).getTime(), date.getTime());
- }
- function testDateConstructor_yearBelow100() {
- var date = new goog.date.Date(14, 7, 19);
- assertEquals(
- 'Date constructor should respect passed in full year', 14,
- date.getFullYear());
- var copied = new goog.date.Date(date);
- assertEquals(
- 'Copying a should return identical date', date.getTime(),
- copied.getTime());
- assertEquals(
- 'Full year should be left intact by copying', 14, copied.getFullYear());
- // Test boundaries.
- assertEquals(-1, new goog.date.Date(-1, 0, 1).getFullYear());
- assertEquals(
- 'There is no year zero, but JS dates accept it', 0,
- new goog.date.Date(0, 0, 1).getFullYear());
- assertEquals(1, new goog.date.Date(1, 0, 1).getFullYear());
- assertEquals(99, new goog.date.Date(99, 0, 1).getFullYear());
- assertEquals(100, new goog.date.Date(100, 0, 1).getFullYear());
- }
- function testDateConstructor_flipOver() {
- var date = new goog.date.Date(2012, 12, 1);
- assertEquals('20130101', date.toIsoString());
- date = new goog.date.Date(12, 12, 1);
- assertEquals('130101', date.toIsoString());
- }
- function testDateToIsoString() {
- var d = new goog.date.Date(2006, goog.date.month.JAN, 1);
- assertEquals('1 Jan 2006 is 20060101', d.toIsoString(), '20060101');
- d = new goog.date.Date(2007, goog.date.month.JUN, 12);
- assertEquals('12 Jun 2007 is 20070612', d.toIsoString(), '20070612');
- d = new goog.date.Date(2218, goog.date.month.DEC, 31);
- assertEquals('31 Dec 2218 is 22181231', d.toIsoString(), '22181231');
- }
- function testDateTimeFromTimestamp() {
- assertEquals(0, goog.date.DateTime.fromTimestamp(0).getTime());
- assertEquals(1234, goog.date.DateTime.fromTimestamp(1234).getTime());
- }
- function testRfc822StringToDate() {
- var date = goog.date.DateTime.fromRfc822String('October 2, 2002 8:00:00');
- assertNotNull(date);
- assertEquals(2002, date.getFullYear());
- assertEquals(9, date.getMonth());
- assertEquals(2, date.getDate());
- assertEquals(8, date.getHours());
- assertEquals(0, date.getMinutes());
- assertEquals(0, date.getSeconds());
- assertEquals(0, date.getMilliseconds());
- assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
- date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010 08:00:00 UTC');
- assertEquals(2010, date.getFullYear());
- assertEquals(9, date.getUTCMonth());
- assertEquals(2, date.getUTCDate());
- assertEquals(8, date.getUTCHours());
- assertEquals(0, date.getUTCMinutes());
- assertEquals(0, date.getUTCSeconds());
- assertEquals(0, date.getUTCMilliseconds());
- date = goog.date.DateTime.fromRfc822String('');
- assertEquals(null, date);
- date = goog.date.DateTime.fromRfc822String('Invalid Date String');
- assertEquals(null, date);
- date = goog.date.DateTime.fromRfc822String('Sat, 02 Oct 2010');
- assertEquals(2010, date.getFullYear());
- assertEquals(9, date.getMonth());
- assertEquals(2, date.getDate());
- assertEquals(0, date.getHours());
- assertEquals(0, date.getMinutes());
- assertEquals(0, date.getSeconds());
- assertEquals(0, date.getMilliseconds());
- assertEquals(new Date(2010, 9, 2).getTime(), date.getTime());
- }
- function testIsoStringToDate() {
- var iso = '20060210T000000Z';
- var date = goog.date.fromIsoString(iso);
- assertEquals('Got 2006 from ' + iso, 2006, date.getFullYear());
- assertEquals('Got February from ' + iso, 1, date.getMonth());
- // use getUTCDate() here, since in 'iso' var we specified timezone
- // as being a zero offset from GMT
- assertEquals('Got 10th from ' + iso, 10, date.getUTCDate());
- // YYYY-MM-DD
- iso = '2005-02-22';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got February from ' + iso, 1, date.getMonth());
- assertEquals('Got 22nd from ' + iso, 22, date.getDate());
- // YYYYMMDD
- iso = '20050222';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got February from ' + iso, 1, date.getMonth());
- assertEquals('Got 22nd from ' + iso, 22, date.getDate());
- // YYYY-MM
- iso = '2005-08';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got August from ' + iso, 7, date.getMonth());
- // YYYYMM
- iso = '200502';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got February from ' + iso, 1, date.getMonth());
- // YYYY
- iso = '2005';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- // 1997-W01 or 1997W01
- iso = '2005-W22';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got May from ' + iso, 4, date.getMonth());
- assertEquals('Got 30th from ' + iso, 30, date.getDate());
- iso = '2005W22';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got May from ' + iso, 4, date.getMonth());
- assertEquals('Got 30th from ' + iso, 30, date.getDate());
- // 1997-W01-2 or 1997W012
- iso = '2005-W22-4';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got June from ' + iso, 5, date.getMonth());
- assertEquals('Got 2nd from ' + iso, 2, date.getDate());
- iso = '2005W224';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got June from ' + iso, 5, date.getMonth());
- assertEquals('Got 2nd from ' + iso, 2, date.getDate());
- iso = '2004-W53-6';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 1st from ' + iso, 1, date.getDate());
- iso = '2004-W53-7';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 2nd from ' + iso, 2, date.getDate());
- iso = '2005-W52-6';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 31st from ' + iso, 31, date.getDate());
- // both years 2007 start with the same day
- iso = '2007-W01-1';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 1st from ' + iso, 1, date.getDate());
- iso = '2007-W52-7';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 30th from ' + iso, 30, date.getDate());
- iso = '2008-W01-1';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2007 from ' + iso, 2007, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 31st from ' + iso, 31, date.getDate());
- // Gregorian year 2008 is a leap year,
- // ISO year 2008 is 2 days shorter:
- // 1 day longer at the start, 3 days shorter at the end
- iso = '2008-W01-2';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
- assertEquals('Got Jan from ' + iso, 0, date.getMonth());
- assertEquals('Got 1st from ' + iso, 1, date.getDate());
- // ISO year is three days into the previous gregorian year
- iso = '2009-W01-1';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 29th from ' + iso, 29, date.getDate());
- iso = '2009-W01-3';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2008 from ' + iso, 2008, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 31st from ' + iso, 31, date.getDate());
- iso = '2009-W01-4';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 1st from ' + iso, 1, date.getDate());
- // ISO year is three days into the next gregorian year
- iso = '2009-W53-4';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2009 from ' + iso, 2009, date.getFullYear());
- assertEquals('Got December from ' + iso, 11, date.getMonth());
- assertEquals('Got 31st from ' + iso, 31, date.getDate());
- iso = '2009-W53-5';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 1st from ' + iso, 1, date.getDate());
- iso = '2009-W53-6';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 2nd from ' + iso, 2, date.getDate());
- iso = '2009-W53-7';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 3rd from ' + iso, 3, date.getDate());
- iso = '2010-W01-1';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2010 from ' + iso, 2010, date.getFullYear());
- assertEquals('Got January from ' + iso, 0, date.getMonth());
- assertEquals('Got 4th from ' + iso, 4, date.getDate());
- // Examples where the ISO year is three days into the previous gregorian year
- // 1995-035 or 1995035
- iso = '2005-146';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got May from ' + iso, 4, date.getMonth());
- assertEquals('Got 26th from ' + iso, 26, date.getDate());
- iso = '2005146';
- date = goog.date.fromIsoString(iso);
- assertEquals('Got 2005 from ' + iso, 2005, date.getFullYear());
- assertEquals('Got May from ' + iso, 4, date.getMonth());
- assertEquals('Got 26th from ' + iso, 26, date.getDate());
- }
- // test private function used by goog.date.Date.toIsoString()
- function test_setIso8601TimeOnly_() {
- if (goog.userAgent.product.SAFARI) {
- // TODO(b/20733468): Disabled so we can get the rest of the Closure test
- // suite running in a continuous build. Will investigate later.
- return;
- }
- // 23:59:59
- var d = new goog.date.DateTime(0, 0);
- var iso = '18:46:39';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
- // 235959
- d = new goog.date.DateTime(0, 0);
- iso = '184639';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
- // 23:59, 2359, or 23
- d = new goog.date.DateTime(0, 0);
- iso = '18:46';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- d = new goog.date.DateTime(0, 0);
- iso = '1846';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- d = new goog.date.DateTime(0, 0);
- iso = '18';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- d = new goog.date.DateTime(0, 0);
- iso = '18463';
- assertFalse('failed to parse ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertTrue('date did not change', d.equals(new goog.date.DateTime(0, 0)));
- // 23:59:59.9942 or 235959.9942
- d = new goog.date.DateTime(0, 0);
- iso = '18:46:39.9942';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
- // Fails in Safari4 Winxp, temporarily disabled
- expectedFailures.expectFailureFor(isWinxpSafari4());
- try {
- if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
- // Both Safari 3.1 and WebKit (on Mac) return floating-point values.
- assertRoughlyEquals(
- 'Got roughly 994.2 milliseconds from ' + iso, 994.2,
- d.getMilliseconds(), 0.01);
- } else {
- // Other browsers, including WebKit on Windows, return integers.
- assertEquals(
- 'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
- }
- d = new goog.date.DateTime(0, 0);
- iso = '184639.9942';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got 18 hours from ' + iso, 18, d.getHours());
- assertEquals('Got 46 minutes from ' + iso, 46, d.getMinutes());
- assertEquals('Got 39 seconds from ' + iso, 39, d.getSeconds());
- if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
- // Both Safari 3.1 and WebKit (on Mac) return floating-point values.
- assertRoughlyEquals(
- 'Got roughly 994.2 milliseconds from ' + iso, 994.2,
- d.getMilliseconds(), 0.01);
- } else {
- // Other browsers, including WebKit on Windows, return integers.
- assertEquals(
- 'Got 994 milliseconds from ' + iso, 994, d.getMilliseconds());
- }
- } catch (e) {
- expectedFailures.handleException(e);
- }
- // 1995-02-04 24:00 = 1995-02-05 00:00
- // timezone tests
- var offset = new Date().getTimezoneOffset() / 60;
- d = new goog.date.DateTime(0, 0);
- iso = '18:46:39+07:00';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got an 11-hour GMT offset from ' + iso, 11, d.getUTCHours());
- d = new goog.date.DateTime(0, 0);
- iso = '18:46:39+00:00';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got an 18-hour GMT offset from ' + iso, 18, d.getUTCHours());
- d = new goog.date.DateTime(0, 0);
- iso = '16:46:39-07:00';
- assertTrue('parsed ' + iso, goog.date.setIso8601TimeOnly_(d, iso));
- assertEquals('Got a 23-hour GMT offset from ' + iso, 23, d.getUTCHours());
- }
- function testDateIntervalAdd() {
- // -1m, cross year boundary
- var d = new goog.date.Date(2007, goog.date.month.JAN, 1);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
- assertEquals('2007-01-01 - 1m = 2006-12-01', '20061201', d.toIsoString());
- // +1y2m3d
- d = new goog.date.Date(2006, goog.date.month.JAN, 1);
- d.add(new goog.date.Interval(1, 2, 3));
- assertEquals('2006-01-01 + 1y2m3d = 2007-03-04', '20070304', d.toIsoString());
- // -1y2m3d (negative interval)
- d = new goog.date.Date(2007, goog.date.month.MAR, 4);
- d.add(new goog.date.Interval(-1, -2, -3));
- assertEquals('2007-03-04 - 1y2m3d = 2006-01-01', '20060101', d.toIsoString());
- // 2007-12-30 + 3d (roll over to next year)
- d = new goog.date.Date(2007, goog.date.month.DEC, 30);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 3));
- assertEquals('2007-12-30 + 3d = 2008-01-02', '20080102', d.toIsoString());
- // 2004-03-01 - 1d (handle leap year)
- d = new goog.date.Date(2004, goog.date.month.MAR, 1);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, -1));
- assertEquals('2004-03-01 - 1d = 2004-02-29', '20040229', d.toIsoString());
- // 2004-02-29 + 1y (stays at end of Feb, doesn't roll to Mar)
- d = new goog.date.Date(2004, goog.date.month.FEB, 29);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
- assertEquals('2004-02-29 + 1y = 2005-02-28', '20050228', d.toIsoString());
- // 2004-02-29 - 1y (stays at end of Feb, doesn't roll to Mar)
- d = new goog.date.Date(2004, goog.date.month.FEB, 29);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
- assertEquals('2004-02-29 - 1y = 2003-02-28', '20030228', d.toIsoString());
- // 2003-02-28 + 1y (stays at 28, doesn't go to leap year end of Feb)
- d = new goog.date.Date(2003, goog.date.month.FEB, 28);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
- assertEquals('2003-02-28 + 1y = 2004-02-28', '20040228', d.toIsoString());
- // 2005-02-28 - 1y (stays at 28, doesn't go to leap year end of Feb)
- d = new goog.date.Date(2005, goog.date.month.FEB, 28);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
- assertEquals('2005-02-28 - 1y = 2004-02-28', '20040228', d.toIsoString());
- // 2003-01-31 + 1y (stays at end of Jan, standard case)
- d = new goog.date.Date(2003, goog.date.month.JAN, 31);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, 1));
- assertEquals('2003-01-31 + 1y = 2004-01-31', '20040131', d.toIsoString());
- // 2005-01-31 - 1y (stays at end of Jan, standard case)
- d = new goog.date.Date(2005, goog.date.month.JAN, 31);
- d.add(new goog.date.Interval(goog.date.Interval.YEARS, -1));
- assertEquals('2005-01-31 - 1y = 2004-01-31', '20040131', d.toIsoString());
- // 2006-01-31 + 1m (stays at end of Feb, doesn't roll to Mar, non-leap-year)
- d = new goog.date.Date(2006, goog.date.month.JAN, 31);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
- assertEquals('2006-01-31 + 1m = 2006-02-28', '20060228', d.toIsoString());
- // 2004-02-29 + 1m (stays at 29, standard case)
- d = new goog.date.Date(2004, goog.date.month.FEB, 29);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, +1));
- assertEquals('2004-02-29 + 1m = 2004-03-29', '20040329', d.toIsoString());
- // 2004-02-29 - 1m (stays at 29, standard case)
- d = new goog.date.Date(2004, goog.date.month.FEB, 29);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
- assertEquals('2004-02-29 - 1m = 2004-01-29', '20040129', d.toIsoString());
- // 2004-01-30 + 1m (snaps to Feb 29)
- d = new goog.date.Date(2004, goog.date.month.JAN, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
- assertEquals('2004-01-30 + 1m = 2004-02-29', '20040229', d.toIsoString());
- // 2004-03-30 - 1m (snaps to Feb 29)
- d = new goog.date.Date(2004, goog.date.month.MAR, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, -1));
- assertEquals('2004-03-30 - 1m = 2004-02-29', '20040229', d.toIsoString());
- // 2004-03-30 + 1m (stays at 30, standard case)
- d = new goog.date.Date(2004, goog.date.month.MAR, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MONTHS, 1));
- assertEquals('2004-03-30 + 1m = 2004-04-30', '20040430', d.toIsoString());
- // 2008-10-30 + 1d (roll over to 31).
- d = new goog.date.Date(2008, goog.date.month.OCT, 30);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-10-30 + 1d = 2008-10-31', '20081031', d.toIsoString());
- // 2008-10-31 + 1d (roll over to November, not December).
- d = new goog.date.Date(2008, goog.date.month.OCT, 31);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-10-31 + 1d = 2008-11-01', '20081101', d.toIsoString());
- // 2008-10-17 + 1d (Brasilia dst).
- d = new goog.date.Date(2008, goog.date.month.OCT, 17);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-10-17 + 1d = 2008-10-18', '20081018', d.toIsoString());
- // 2008-10-18 + 1d (Brasilia dst).
- d = new goog.date.Date(2008, goog.date.month.OCT, 18);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-10-18 + 1d = 2008-10-19', '20081019', d.toIsoString());
- // 2008-10-19 + 1d (Brasilia dst).
- d = new goog.date.Date(2008, goog.date.month.OCT, 19);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-10-19 + 1d = 2008-10-20', '20081020', d.toIsoString());
- // 2008-02-16 + 1d (Brasilia dst).
- d = new goog.date.Date(2008, goog.date.month.FEB, 16);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-02-16 + 1d = 2008-02-17', '20080217', d.toIsoString());
- // 2008-02-17 + 1d (Brasilia dst).
- d = new goog.date.Date(2008, goog.date.month.FEB, 17);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- assertEquals('2008-02-17 + 1d = 2008-02-18', '20080218', d.toIsoString());
- }
- function testDateEquals() {
- var d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- var d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertFalse('d1 != null', d1.equals(null));
- assertFalse('d2 != undefined', d2.equals(undefined));
- assertTrue('d1 == d2', d1.equals(d2));
- assertTrue('d2 == d1', d2.equals(d1));
- d1 = new goog.date.Date(2005, goog.date.month.MAR, 1);
- d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertFalse('different year', d1.equals(d2));
- d1 = new goog.date.Date(2004, goog.date.month.FEB, 1);
- d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertFalse('different month', d1.equals(d2));
- d1 = new goog.date.Date(2004, goog.date.month.MAR, 2);
- d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertFalse('different date', d1.equals(d2));
- // try passing in DateTime, time fields should be ignored
- d1 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- assertTrue('using goog.date.DateTime, same date', d1.equals(d2));
- }
- function testDateTimeConstructor() {
- var date = new goog.date.DateTime(2001, 2, 3, 4, 5, 6, 7);
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- assertEquals(4, date.getHours());
- assertEquals(5, date.getMinutes());
- assertEquals(6, date.getSeconds());
- assertEquals(7, date.getMilliseconds());
- assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
- date = new goog.date.DateTime(2001);
- assertEquals(2001, date.getFullYear());
- assertEquals(0, date.getMonth());
- assertEquals(1, date.getDate());
- assertEquals(0, date.getHours());
- assertEquals(0, date.getMinutes());
- assertEquals(0, date.getSeconds());
- assertEquals(0, date.getMilliseconds());
- date = new goog.date.DateTime(new Date(2001, 2, 3, 4, 5, 6, 7));
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- assertEquals(4, date.getHours());
- assertEquals(5, date.getMinutes());
- assertEquals(6, date.getSeconds());
- assertEquals(7, date.getMilliseconds());
- assertEquals(new Date(2001, 2, 3, 4, 5, 6, 7).getTime(), date.getTime());
- goog.now = function() { return new Date(2001, 2, 3, 4).getTime(); };
- date = new goog.date.DateTime();
- assertEquals(2001, date.getFullYear());
- assertEquals(2, date.getMonth());
- assertEquals(3, date.getDate());
- assertEquals(4, date.getHours());
- assertEquals(0, date.getMinutes());
- assertEquals(0, date.getSeconds());
- assertEquals(0, date.getMilliseconds());
- assertEquals(new Date(2001, 2, 3, 4).getTime(), date.getTime());
- date = new goog.date.DateTime(new Date('October 2, 2002 8:00:00'));
- assertEquals(2002, date.getFullYear());
- assertEquals(9, date.getMonth());
- assertEquals(2, date.getDate());
- assertEquals(8, date.getHours());
- assertEquals(0, date.getMinutes());
- assertEquals(0, date.getSeconds());
- assertEquals(0, date.getMilliseconds());
- assertEquals(new Date(2002, 9, 2, 8).getTime(), date.getTime());
- }
- function testDateTimeEquals() {
- var d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- var d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- assertTrue('d1 == d2', d1.equals(d2));
- assertTrue('d2 == d1', d2.equals(d1));
- d1 = new goog.date.DateTime(2007, goog.date.month.JAN, 1);
- d2 = new goog.date.DateTime(); // today
- assertFalse('different date', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12);
- assertFalse('different hours', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 29, 30);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- assertFalse('different minutes', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 29);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- assertFalse('different seconds', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
- assertTrue('same milliseconds', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 499);
- d2 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30, 500);
- assertFalse('different milliseconds', d1.equals(d2));
- // try milliseconds again, this time comparing against a native Date
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- d2 = new Date(2004, 2, 1, 12, 30, 30, 999);
- assertFalse('different milliseconds, native Date', d1.equals(d2));
- // pass in a goog.date.Date rather than a goog.date.DateTime
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 12, 30, 30);
- d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertFalse('using goog.date.Date, different times', d1.equals(d2));
- d1 = new goog.date.DateTime(2004, goog.date.month.MAR, 1, 0, 0, 0);
- d2 = new goog.date.Date(2004, goog.date.month.MAR, 1);
- assertTrue('using goog.date.Date, same time (midnight)', d1.equals(d2));
- }
- function testIntervalIsZero() {
- assertTrue('zero interval', new goog.date.Interval().isZero());
- var i = new goog.date.Interval(0, 0, 1, -24, 0, 0);
- assertFalse('1 day minus 24 hours is not considered zero', i.isZero());
- }
- function testIntervalGetInverse() {
- var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
- var i2 = i1.getInverse();
- var d = new goog.date.Date(2004, goog.date.month.MAR, 1);
- d.add(i1);
- d.add(i2);
- var label = '2004-03-01 - 1d + 1d = 2004-03-01';
- assertEquals(label, d.toIsoString(), '20040301');
- i1 = new goog.date.Interval(1, 2, 3);
- i2 = i1.getInverse();
- d = new goog.date.Date(2004, goog.date.month.MAR, 1);
- d.add(i1);
- d.add(i2);
- label = '2004-03-01 - 1y2m3d + 1y2m3d = 2004-03-01';
- assertEquals(label, d.toIsoString(), '20040301');
- }
- function testIntervalTimes() {
- var i = new goog.date.Interval(1, 2, 3, 4, 5, 6);
- var expected = new goog.date.Interval(2, 4, 6, 8, 10, 12);
- assertTrue('double interval', expected.equals(i.times(2)));
- }
- //=== tests for goog.date.Interval.equals() ===
- function testIntervalEquals() {
- var i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
- var i2 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
- assertTrue('-1d == -1d, aka i1 == i2', i1.equals(i2));
- assertTrue('-1d == -1d, aka i2 == i1', i2.equals(i1));
- i1 = new goog.date.Interval(goog.date.Interval.DAYS, -1);
- i2 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
- assertFalse('-1d != +1d, aka i1 == i2', i1.equals(i2));
- assertFalse('-1d != +1d, aka i2 == i1', i2.equals(i1));
- i1 = new goog.date.Interval(0, 3); // Three months
- i2 = new goog.date.Interval(goog.date.Interval.MONTHS, 3);
- assertTrue('3m == 3m, aka i1 == i2', i1.equals(i2));
- assertTrue('3m == 3m, aka i2 == i1', i2.equals(i1));
- // 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
- i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- i2 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i1.equals(i2));
- assertTrue('1y6m15d12h30M30s == 1y6m15d12h30M30s', i2.equals(i1));
- }
- //=== tests for adding two goog.date.Interval intervals ===
- function testIntervalIntervalAdd() {
- var i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- var i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
- i1.add(i2);
- assertTrue('i1 + i2', i1.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
- i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- i2 = new goog.date.Interval(0, 3, 20, 10, 50, -25);
- i2.add(i1);
- assertTrue('i2 + i1', i2.equals(new goog.date.Interval(1, 9, 35, 22, 80, 5)));
- i1 = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- i2 = i1.getInverse();
- i1.add(i2);
- assertTrue('i1 + (-i1)', i1.equals(new goog.date.Interval()));
- i1 = new goog.date.Interval(goog.date.Interval.DAYS, 1);
- i2 = new goog.date.Interval(0, -2, -2);
- i1.add(i2);
- assertTrue('1d + (-2m-2d)', i1.equals(new goog.date.Interval(0, -2, -1)));
- }
- //=== tests conversion to and from ISO 8601 duration string ===
- function testIsoDuration() {
- var interval1 = new goog.date.Interval(123, 456, 678, 11, 12, 455.5);
- var duration1 = 'P123Y456M678DT11H12M455.5S';
- assertTrue(
- 'parse full duration',
- interval1.equals(goog.date.Interval.fromIsoString(duration1)));
- assertEquals('create full duration', duration1, interval1.toIsoString());
- var interval2 = new goog.date.Interval(123);
- var duration2 = 'P123Y';
- var duration2v = 'P123Y0M0DT0H0M0S';
- assertTrue(
- 'parse year',
- interval2.equals(goog.date.Interval.fromIsoString(duration2)));
- assertEquals('create year', duration2, interval2.toIsoString());
- assertEquals('create year, verbose', duration2v, interval2.toIsoString(true));
- var interval3 = new goog.date.Interval(0, 0, 0, 11, 12, 40);
- var duration3 = 'PT11H12M40S';
- var duration3v = 'P0Y0M0DT11H12M40S';
- assertTrue(
- 'parse time duration',
- interval3.equals(goog.date.Interval.fromIsoString(duration3)));
- assertEquals('create time duration', duration3, interval3.toIsoString());
- assertEquals(
- 'create time duration, verbove', duration3v, interval3.toIsoString(true));
- var interval4 = new goog.date.Interval(7, 8, 9, 1, 2, 4);
- var duration4 = 'P7Y8M9DT1H2M4S';
- assertTrue(
- 'parse one-digit duration',
- interval4.equals(goog.date.Interval.fromIsoString(duration4)));
- assertEquals('create one-digit duration', duration4, interval4.toIsoString());
- var interval5 = new goog.date.Interval(-123, -456, -678, -11, -12, -455.5);
- var duration5 = '-P123Y456M678DT11H12M455.5S';
- assertTrue(
- 'parse full negative duration',
- interval5.equals(goog.date.Interval.fromIsoString(duration5)));
- assertEquals(
- 'create full negative duration', duration5, interval5.toIsoString());
- var interval6 = new goog.date.Interval(0, 0, -1);
- var duration6 = '-P1D';
- var duration6v = '-P0Y0M1DT0H0M0S';
- assertTrue(
- 'parse partial negative duration',
- interval6.equals(goog.date.Interval.fromIsoString(duration6)));
- assertEquals(
- 'create partial negative duration', duration6, interval6.toIsoString());
- assertEquals(
- 'create partial negative duration, verbose', duration6v,
- interval6.toIsoString(true));
- var interval7 = new goog.date.Interval(0, 0, 9, 0, 0, 4);
- var duration7 = 'P9DT4S';
- var duration7v = 'P0Y0M9DT0H0M4S';
- assertTrue(
- 'parse partial one-digit duration',
- interval7.equals(goog.date.Interval.fromIsoString(duration7)));
- assertTrue(
- 'parse partial one-digit duration, verbose',
- interval7.equals(goog.date.Interval.fromIsoString(duration7v)));
- assertEquals(
- 'create partial one-digit duration', duration7, interval7.toIsoString());
- assertEquals(
- 'create partial one-digit duration, verbose', duration7v,
- interval7.toIsoString(true));
- var interval8 = new goog.date.Interval(1, -1, 1, -1, 1, -1);
- assertNull('create mixed sign duration', interval8.toIsoString());
- var duration9 = '1Y1M1DT1H1M1S';
- assertNull('missing P', goog.date.Interval.fromIsoString(duration9));
- var duration10 = 'P1Y1M1D1H1M1S';
- assertNull('missing T', goog.date.Interval.fromIsoString(duration10));
- var duration11 = 'P1Y1M1DT';
- assertNull('extra T', goog.date.Interval.fromIsoString(duration11));
- var duration12 = 'PT.5S';
- assertNull(
- 'invalid seconds, missing integer part',
- goog.date.Interval.fromIsoString(duration12));
- var duration13 = 'PT1.S';
- assertNull(
- 'invalid seconds, missing fractional part',
- goog.date.Interval.fromIsoString(duration13));
- }
- function testGetTotalSeconds() {
- var duration = new goog.date.Interval(0, 0, 2, 3, 4, 5);
- assertEquals(
- 'seconds in 2d3h4m5s', 2 * 86400 + 3 * 3600 + 4 * 60 + 5,
- duration.getTotalSeconds());
- }
- function testIsDateLikeWithGoogDateTime() {
- var jsDate = new Date();
- var googDate = new goog.date.DateTime();
- var string = 'foo';
- var number = 1;
- var nullVar = null;
- var notDefined;
- assertTrue('js Date should be date-like', goog.isDateLike(jsDate));
- assertTrue('goog Date should be date-like', goog.isDateLike(googDate));
- assertFalse('string should not be date-like', goog.isDateLike(string));
- assertFalse('number should not be date-like', goog.isDateLike(number));
- assertFalse('nullVar should not be date-like', goog.isDateLike(nullVar));
- assertFalse('undefined should not be date-like', goog.isDateLike(notDefined));
- }
- function testToUTCRfc3339String() {
- var date = goog.date.fromIsoString('19850412T232050Z');
- date.setUTCMilliseconds(52);
- assertEquals(date.toUTCRfc3339String(), '1985-04-12T23:20:50.52Z');
- assertNotEquals(
- 'Diverges from ISO 8601', date.toUTCRfc3339String(),
- date.toUTCIsoString(true, true));
- var date = goog.date.fromIsoString('19901231T235959Z');
- assertEquals(date.toUTCRfc3339String(), '1990-12-31T23:59:59Z');
- assertNotEquals(
- 'Diverges from ISO 8601', date.toUTCRfc3339String(),
- date.toUTCIsoString(true, true));
- var date = goog.date.fromIsoString('19370101T120027Z');
- date.setUTCMilliseconds(87);
- assertEquals(date.toUTCRfc3339String(), '1937-01-01T12:00:27.87Z');
- assertNotEquals(
- 'Diverges from ISO 8601', date.toUTCRfc3339String(),
- date.toUTCIsoString(true, true));
- }
- function testDateTimezone() {
- var d = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
- d.add(
- new goog.date.Interval(
- goog.date.Interval.MINUTES, d.getTimezoneOffset()));
- var d2 = new goog.date.DateTime(2006, 1, 1, 12, 0, 0);
- assertEquals(
- 'Compensate for timezone and compare with UTC date/time',
- d.toIsoString(true), d2.toUTCIsoString(true));
- }
- function testToUsTimeString() {
- var doPad = true;
- var doShowPm = true;
- var dontPad = false;
- var dontShowPm = false;
- // 12am
- var d = new goog.date.DateTime(2007, 1, 14);
- assertEquals('12am test 1', '12:00 AM', d.toUsTimeString());
- assertEquals('12am test 2', '12:00 AM', d.toUsTimeString(doPad));
- assertEquals('12am test 3', '12:00 AM', d.toUsTimeString(dontPad));
- assertEquals('12am test 4', '12:00 AM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('12am test 5', '00:00', d.toUsTimeString(doPad, dontShowPm));
- assertEquals('12am test 6', '12:00 AM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('12am test 7', '0:00', d.toUsTimeString(dontPad, dontShowPm));
- // 9am
- d = new goog.date.DateTime(2007, 1, 14, 9);
- assertEquals('9am test 1', '9:00 AM', d.toUsTimeString());
- assertEquals('9am test 2', '09:00 AM', d.toUsTimeString(doPad));
- assertEquals('9am test 3', '9:00 AM', d.toUsTimeString(dontPad));
- assertEquals('9am test 4', '09:00 AM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('9am test 5', '09:00', d.toUsTimeString(doPad, dontShowPm));
- assertEquals('9am test 6', '9:00 AM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('9am test 7', '9:00', d.toUsTimeString(dontPad, dontShowPm));
- // 12pm
- d = new goog.date.DateTime(2007, 1, 14, 12);
- assertEquals('12pm test 1', '12:00 PM', d.toUsTimeString());
- assertEquals('12pm test 2', '12:00 PM', d.toUsTimeString(doPad));
- assertEquals('12pm test 3', '12:00 PM', d.toUsTimeString(dontPad));
- assertEquals('12pm test 4', '12:00 PM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('12pm test 5', '12:00', d.toUsTimeString(doPad, dontShowPm));
- assertEquals('12pm test 6', '12:00 PM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('12pm test 7', '12:00', d.toUsTimeString(dontPad, dontShowPm));
- // 6 PM
- d = new goog.date.DateTime(2007, 1, 14, 18);
- assertEquals('6 PM test 1', '6:00 PM', d.toUsTimeString());
- assertEquals('6 PM test 2', '06:00 PM', d.toUsTimeString(doPad));
- assertEquals('6 PM test 3', '6:00 PM', d.toUsTimeString(dontPad));
- assertEquals('6 PM test 4', '06:00 PM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('6 PM test 5', '06:00', d.toUsTimeString(doPad, dontShowPm));
- assertEquals('6 PM test 6', '6:00 PM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('6 PM test 7', '6:00', d.toUsTimeString(dontPad, dontShowPm));
- // 6:01 PM
- d = new goog.date.DateTime(2007, 1, 14, 18, 1);
- assertEquals('6:01 PM test 1', '6:01 PM', d.toUsTimeString());
- assertEquals('6:01 PM test 2', '06:01 PM', d.toUsTimeString(doPad));
- assertEquals('6:01 PM test 3', '6:01 PM', d.toUsTimeString(dontPad));
- assertEquals('6:01 PM test 4', '06:01 PM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('6:01 PM test 5', '06:01', d.toUsTimeString(doPad, dontShowPm));
- assertEquals(
- '6:01 PM test 6', '6:01 PM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('6:01 PM test 7', '6:01', d.toUsTimeString(dontPad, dontShowPm));
- // 6:35 PM
- d = new goog.date.DateTime(2007, 1, 14, 18, 35);
- assertEquals('6:35 PM test 1', '6:35 PM', d.toUsTimeString());
- assertEquals('6:35 PM test 2', '06:35 PM', d.toUsTimeString(doPad));
- assertEquals('6:35 PM test 3', '6:35 PM', d.toUsTimeString(dontPad));
- assertEquals('6:35 PM test 4', '06:35 PM', d.toUsTimeString(doPad, doShowPm));
- assertEquals('6:35 PM test 5', '06:35', d.toUsTimeString(doPad, dontShowPm));
- assertEquals(
- '6:35 PM test 6', '6:35 PM', d.toUsTimeString(dontPad, doShowPm));
- assertEquals('6:35 PM test 7', '6:35', d.toUsTimeString(dontPad, dontShowPm));
- // omit zero minutes
- d = new goog.date.DateTime(2007, 1, 14, 18);
- assertEquals(
- 'omit zero 1', '6:00 PM', d.toUsTimeString(dontPad, doShowPm, false));
- assertEquals(
- 'omit zero 2', '6 PM', d.toUsTimeString(dontPad, doShowPm, true));
- // but don't omit zero minutes if not actually zero minutes
- d = new goog.date.DateTime(2007, 1, 14, 18, 1);
- assertEquals(
- 'omit zero 3', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, false));
- assertEquals(
- 'omit zero 4', '6:01 PM', d.toUsTimeString(dontPad, doShowPm, true));
- }
- function testToIsoTimeString() {
- // 00:00
- var d = new goog.date.DateTime(2007, 1, 14);
- assertEquals('00:00', '00:00:00', d.toIsoTimeString());
- // 09:00
- d = new goog.date.DateTime(2007, 1, 14, 9);
- assertEquals('09:00', '09:00:00', d.toIsoTimeString());
- // 12:00
- d = new goog.date.DateTime(2007, 1, 14, 12);
- assertEquals('12:00', '12:00:00', d.toIsoTimeString());
- // 18:00
- d = new goog.date.DateTime(2007, 1, 14, 18);
- assertEquals('18:00', '18:00:00', d.toIsoTimeString());
- // 18:01
- d = new goog.date.DateTime(2007, 1, 14, 18, 1);
- assertEquals('18:01', '18:01:00', d.toIsoTimeString());
- // 18:35
- d = new goog.date.DateTime(2007, 1, 14, 18, 35);
- assertEquals('18:35', '18:35:00', d.toIsoTimeString());
- // 18:35:01
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
- assertEquals('18:35:01', '18:35:01', d.toIsoTimeString());
- // 18:35:11
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
- assertEquals('18:35:11', '18:35:11', d.toIsoTimeString());
- // 18:35:11 >> 18:35
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 11);
- assertEquals('18:35:11 no secs', '18:35', d.toIsoTimeString(false));
- }
- function testToXmlDateTimeString() {
- var d = new goog.date.DateTime(2007, 1, 14);
- assertEquals('2007-02-14', '2007-02-14T00:00:00', d.toXmlDateTime());
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
- assertEquals(
- '2007-02-14, 8:35:01, timezone==undefined', '2007-02-14T18:35:01',
- d.toXmlDateTime());
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
- assertEquals(
- '2007-02-14, 8:35:01, timezone==false', '2007-02-14T18:35:01',
- d.toXmlDateTime(false));
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
- assertEquals(
- '2007-02-14, 8:35:01, timezone==true',
- '2007-02-14T18:35:01' + d.getTimezoneOffsetString(),
- d.toXmlDateTime(true));
- }
- function testClone() {
- var d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1);
- var d2 = d.clone();
- assertTrue('datetimes equal', d.equals(d2));
- d = new goog.date.DateTime(2007, 1, 14, 18, 35, 1, 310);
- d2 = d.clone();
- assertTrue('datetimes with milliseconds equal', d.equals(d2));
- d = new goog.date.Date(2007, 1, 14);
- d2 = d.clone();
- assertTrue('dates equal', d.equals(d2));
- // 1 year, 6 months, 15 days, 12 hours, 30 minutes, 30 seconds
- var i = new goog.date.Interval(1, 6, 15, 12, 30, 30);
- var i2 = i.clone();
- assertTrue('intervals equal', i.equals(i2));
- i = new goog.date.Interval(goog.date.Interval.DAYS, -1);
- i2 = i.clone();
- assertTrue('day intervals equal', i.equals(i2));
- // Brasilia dst
- d = new goog.date.Date(2008, goog.date.month.OCT, 18);
- d.add(new goog.date.Interval(goog.date.Interval.DAYS, 1));
- d2 = d.clone();
- assertTrue('dates equal', d.equals(d2));
- }
- function testValueOf() {
- var date1 = new goog.date.DateTime(2008, 11, 26, 15, 40, 0);
- var date2 = new goog.date.Date(2008, 11, 27);
- var date3 = new goog.date.DateTime(2008, 11, 26, 15, 40, 1);
- var nativeDate = new Date();
- nativeDate.setFullYear(2008, 11, 26);
- nativeDate.setHours(15, 40, 0, 0);
- assertEquals(date1.valueOf(), nativeDate.valueOf());
- assertFalse(date1 < date1);
- assertTrue(date1 <= date1);
- assertTrue(date1 < date2);
- assertTrue(date2 > date3);
- }
- function isWinxpSafari4() {
- return goog.userAgent.product.SAFARI &&
- goog.userAgent.product.isVersion('4') &&
- !goog.userAgent.product.isVersion('5') && goog.userAgent.WINDOWS &&
- goog.userAgent.platform.isVersion('5.0') &&
- !goog.userAgent.platform.isVersion('6.0');
- }
- function testDateCompare() {
- // May 16th, 2011, 3:17:36.500
- var date1 =
- new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
- // May 16th, 2011, 3:17:36.501
- var date2 =
- new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 501);
- // May 16th, 2011, 3:17:36.501
- var date3 =
- new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 502);
- assertEquals(0, goog.date.Date.compare(date1.clone(), date1.clone()));
- assertEquals(-1, goog.date.Date.compare(date1, date2));
- assertEquals(1, goog.date.Date.compare(date2, date1));
- var dates = [date2, date3, date1];
- goog.array.sort(dates, goog.date.Date.compare);
- assertArrayEquals(
- 'Dates should be sorted in time.', [date1, date2, date3], dates);
- // Assert a known millisecond difference between two points in time.
- assertEquals(
- -19129478,
- goog.date.Date.compare(
- new goog.date.DateTime(1982, goog.date.month.MAR, 12, 6, 48, 32, 354),
- new goog.date.DateTime(
- 1982, goog.date.month.MAR, 12, 12, 7, 21, 832)));
- // Test dates before the year 0. Dates are Talk Like a Pirate Day, and
- // Towel Day, 300 B.C. (and before pirates).
- var pirateDay = new goog.date.Date(-300, goog.date.month.SEP, 2);
- var towelDay = new goog.date.Date(-300, goog.date.month.MAY, 12);
- assertEquals(
- 'Dates should be 113 days apart.', 113 * 24 * 60 * 60 * 1000,
- goog.date.Date.compare(pirateDay, towelDay));
- }
- function testDateCompareDateLikes() {
- var nativeDate = new Date(2011, 4, 16, 15, 17, 36, 500);
- var closureDate =
- new goog.date.DateTime(2011, goog.date.month.MAY, 16, 15, 17, 36, 500);
- assertEquals(0, goog.date.Date.compare(nativeDate, closureDate));
- nativeDate.setMilliseconds(499);
- assertEquals(-1, goog.date.Date.compare(nativeDate, closureDate));
- nativeDate.setMilliseconds(501);
- assertEquals(1, goog.date.Date.compare(nativeDate, closureDate));
- }
- function testIsMidnight() {
- assertTrue(new goog.date.DateTime(2013, 0, 1).isMidnight());
- assertFalse(new goog.date.DateTime(2013, 0, 1, 1).isMidnight());
- assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 1).isMidnight());
- assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 1).isMidnight());
- assertFalse(new goog.date.DateTime(2013, 0, 1, 0, 0, 0, 1).isMidnight());
- }
- function testMinMax() {
- // Comparison of two goog.date.DateTimes
- var dateTime1 = new goog.date.DateTime(2000, 0, 1);
- var dateTime2 = new goog.date.DateTime(2000, 0, 1, 0, 0, 0, 1);
- assertEquals(dateTime1, goog.date.min(dateTime1, dateTime2));
- assertEquals(dateTime1, goog.date.min(dateTime2, dateTime1));
- assertEquals(dateTime2, goog.date.max(dateTime1, dateTime2));
- assertEquals(dateTime2, goog.date.max(dateTime2, dateTime1));
- // Comparison of two goog.date.Dates
- var date1 = new goog.date.Date(2000, 0, 1);
- var date2 = new goog.date.Date(2000, 0, 2);
- assertEquals(date1, goog.date.min(date1, date2));
- // Comparison of native Dates.
- var jsDate1 = new Date(2000, 0, 1);
- var jsDate2 = new Date(2000, 0, 2);
- assertEquals(jsDate1, goog.date.min(jsDate1, jsDate2));
- assertEquals(jsDate2, goog.date.max(jsDate1, jsDate2));
- // Comparison of different types.
- assertEquals(date1, goog.date.min(date1, dateTime2));
- assertEquals(dateTime2, goog.date.min(date2, dateTime2));
- assertEquals(date1, goog.date.min(date1, jsDate2));
- assertEquals(jsDate2, goog.date.max(dateTime1, jsDate2));
- }
- function testDateTimeIntervalAdd() {
- // Add hours
- var d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 10, 20, 30);
- d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
- assertEquals(20, d.getHours());
- // Add negative hours
- d.add(new goog.date.Interval(goog.date.Interval.HOURS, -5));
- assertEquals(15, d.getHours());
- // Add hours to the next day
- d.add(new goog.date.Interval(goog.date.Interval.HOURS, 10));
- assertEquals(2, d.getDay());
- assertEquals(1, d.getHours());
- // Add minutes
- d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 22, 20, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 10));
- assertEquals(30, d.getMinutes());
- // Add negative minutes
- d.add(new goog.date.Interval(goog.date.Interval.MINUTES, -5));
- assertEquals(25, d.getMinutes());
- // Add minutes to the next day
- d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 130));
- assertEquals(2, d.getDay());
- assertEquals(0, d.getHours());
- assertEquals(35, d.getMinutes());
- // Add seconds
- d = new goog.date.DateTime(2007, goog.date.month.JAN, 1, 23, 45, 30);
- d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 10));
- assertEquals(40, d.getSeconds());
- // Add negative seconds
- d.add(new goog.date.Interval(goog.date.Interval.SECONDS, -5));
- assertEquals(35, d.getSeconds());
- // Add seconds to the next day
- d.add(new goog.date.Interval(goog.date.Interval.SECONDS, 1200));
- assertEquals(2, d.getDay());
- assertEquals(0, d.getHours());
- assertEquals(5, d.getMinutes());
- assertEquals(35, d.getSeconds());
- // Test daylight savings day 2015-11-1
- d = new goog.date.DateTime(2015, goog.date.month.NOV, 1, 0, 50, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
- assertEquals(1, d.getHours());
- assertEquals(5, d.getMinutes());
- d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
- assertEquals(1, d.getHours());
- // Test daylight savings day 2015-3-8
- d = new goog.date.DateTime(2015, goog.date.month.MAR, 8, 0, 50, 30);
- d.add(new goog.date.Interval(goog.date.Interval.MINUTES, 15));
- assertEquals(1, d.getHours());
- assertEquals(5, d.getMinutes());
- d.add(new goog.date.Interval(goog.date.Interval.HOURS, 1));
- assertEquals(3, d.getHours());
- }
|