date_test.js 59 KB

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