1// Copyright 2008 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// Test date construction from other dates. 29var date0 = new Date(1111); 30var date1 = new Date(date0); 31assertEquals(1111, date0.getTime()); 32assertEquals(date0.getTime(), date1.getTime()); 33var date2 = new Date(date0.toString()); 34assertEquals(1000, date2.getTime()); 35 36// Test that dates may contain commas. 37var date0 = Date.parse("Dec 25 1995 1:30"); 38var date1 = Date.parse("Dec 25, 1995 1:30"); 39var date2 = Date.parse("Dec 25 1995, 1:30"); 40var date3 = Date.parse("Dec 25, 1995, 1:30"); 41assertEquals(date0, date1); 42assertEquals(date1, date2); 43assertEquals(date2, date3); 44 45// Test limits (+/-1e8 days from epoch) 46 47var dMax = new Date(8.64e15); 48assertEquals(8.64e15, dMax.getTime()); 49assertEquals(275760, dMax.getFullYear()); 50assertEquals(8, dMax.getMonth()); 51assertEquals(13, dMax.getUTCDate()); 52 53var dOverflow = new Date(8.64e15+1); 54assertTrue(isNaN(dOverflow.getTime())); 55 56var dMin = new Date(-8.64e15); 57assertEquals(-8.64e15, dMin.getTime()); 58assertEquals(-271821, dMin.getFullYear()); 59assertEquals(3, dMin.getMonth()); 60assertEquals(20, dMin.getUTCDate()); 61 62var dUnderflow = new Date(-8.64e15-1); 63assertTrue(isNaN(dUnderflow.getTime())); 64 65 66// Tests inspired by js1_5/Date/regress-346363.js 67 68// Year 69var a = new Date(); 70a.setFullYear(); 71a.setFullYear(2006); 72assertEquals(2006, a.getFullYear()); 73 74var b = new Date(); 75b.setUTCFullYear(); 76b.setUTCFullYear(2006); 77assertEquals(2006, b.getUTCFullYear()); 78 79// Month 80var c = new Date(); 81c.setMonth(); 82c.setMonth(2); 83assertTrue(isNaN(c.getMonth())); 84 85var d = new Date(); 86d.setUTCMonth(); 87d.setUTCMonth(2); 88assertTrue(isNaN(d.getUTCMonth())); 89 90// Date 91var e = new Date(); 92e.setDate(); 93e.setDate(2); 94assertTrue(isNaN(e.getDate())); 95 96var f = new Date(); 97f.setUTCDate(); 98f.setUTCDate(2); 99assertTrue(isNaN(f.getUTCDate())); 100 101// Hours 102var g = new Date(); 103g.setHours(); 104g.setHours(2); 105assertTrue(isNaN(g.getHours())); 106 107var h = new Date(); 108h.setUTCHours(); 109h.setUTCHours(2); 110assertTrue(isNaN(h.getUTCHours())); 111 112// Minutes 113var g = new Date(); 114g.setMinutes(); 115g.setMinutes(2); 116assertTrue(isNaN(g.getMinutes())); 117 118var h = new Date(); 119h.setUTCHours(); 120h.setUTCHours(2); 121assertTrue(isNaN(h.getUTCHours())); 122 123 124// Seconds 125var i = new Date(); 126i.setSeconds(); 127i.setSeconds(2); 128assertTrue(isNaN(i.getSeconds())); 129 130var j = new Date(); 131j.setUTCSeconds(); 132j.setUTCSeconds(2); 133assertTrue(isNaN(j.getUTCSeconds())); 134 135 136// Milliseconds 137var k = new Date(); 138k.setMilliseconds(); 139k.setMilliseconds(2); 140assertTrue(isNaN(k.getMilliseconds())); 141 142var l = new Date(); 143l.setUTCMilliseconds(); 144l.setUTCMilliseconds(2); 145assertTrue(isNaN(l.getUTCMilliseconds())); 146 147// Test that toLocaleTimeString only returns the time portion of the 148// date without the timezone information. 149function testToLocaleTimeString() { 150 var d = new Date(); 151 var s = d.toLocaleTimeString(); 152 assertEquals(8, s.length); 153} 154 155testToLocaleTimeString(); 156 157// Test that -0 is treated correctly in MakeDay. 158var d = new Date(); 159assertDoesNotThrow("d.setDate(-0)"); 160assertDoesNotThrow("new Date(-0, -0, -0, -0, -0, -0, -0)"); 161assertDoesNotThrow("new Date(0x40000000, 0x40000000, 0x40000000," + 162 "0x40000000, 0x40000000, 0x40000000, 0x40000000)") 163assertDoesNotThrow("new Date(-0x40000001, -0x40000001, -0x40000001," + 164 "-0x40000001, -0x40000001, -0x40000001, -0x40000001)") 165 166 167// Modified test from WebKit 168// LayoutTests/fast/js/script-tests/date-utc-timeclip.js: 169 170assertEquals(8639999999999999, Date.UTC(275760, 8, 12, 23, 59, 59, 999)); 171assertEquals(8640000000000000, Date.UTC(275760, 8, 13)); 172assertTrue(isNaN(Date.UTC(275760, 8, 13, 0, 0, 0, 1))); 173assertTrue(isNaN(Date.UTC(275760, 8, 14))); 174 175assertEquals(Date.UTC(-271821, 3, 20, 0, 0, 0, 1), -8639999999999999); 176assertEquals(Date.UTC(-271821, 3, 20), -8640000000000000); 177assertTrue(isNaN(Date.UTC(-271821, 3, 19, 23, 59, 59, 999))); 178assertTrue(isNaN(Date.UTC(-271821, 3, 19))); 179 180 181// Test creation with large date values. 182d = new Date(1969, 12, 1, 99999999999); 183assertTrue(isNaN(d.getTime())); 184d = new Date(1969, 12, 1, -99999999999); 185assertTrue(isNaN(d.getTime())); 186d = new Date(1969, 12, 1, Infinity); 187assertTrue(isNaN(d.getTime())); 188d = new Date(1969, 12, 1, -Infinity); 189assertTrue(isNaN(d.getTime())); 190d = new Date(1969, 12, 1, 0); 191d.setTime(Math.pow(2, 64)); 192assertTrue(isNaN(d.getTime())); 193d = new Date(1969, 12, 1, 0); 194d.setTime(Math.pow(-2, 64)); 195assertTrue(isNaN(d.getTime())); 196 197 198// Test creation with obscure date values. 199assertEquals(8640000000000000, Date.UTC(1970, 0, 1 + 100000001, -24)); 200assertEquals(-8640000000000000, Date.UTC(1970, 0, 1 - 100000001, 24)); 201 202 203// Parsing ES5 ISO-8601 dates. 204// When TZ is omitted, it defaults to 'Z' meaning UTC. 205 206// Check epoch. 207assertEquals(0, Date.parse("1970-01-01T00:00:00.000+00:00")); 208assertEquals(0, Date.parse("1970-01-01T00:00:00.000-00:00")); 209assertEquals(0, Date.parse("1970-01-01T00:00:00.000Z")); 210assertEquals(0, Date.parse("1970-01-01T00:00:00.000")); 211assertEquals(0, Date.parse("1970-01-01T00:00:00")); 212assertEquals(0, Date.parse("1970-01-01T00:00")); 213assertEquals(0, Date.parse("1970-01-01")); 214 215assertEquals(0, Date.parse("1970-01T00:00:00.000+00:00")); 216assertEquals(0, Date.parse("1970-01T00:00:00.000-00:00")); 217assertEquals(0, Date.parse("1970-01T00:00:00.000Z")); 218assertEquals(0, Date.parse("1970-01T00:00:00.000")); 219assertEquals(0, Date.parse("1970-01T00:00:00")); 220assertEquals(0, Date.parse("1970-01T00:00")); 221assertEquals(0, Date.parse("1970-01")); 222 223assertEquals(0, Date.parse("1970T00:00:00.000+00:00")); 224assertEquals(0, Date.parse("1970T00:00:00.000-00:00")); 225assertEquals(0, Date.parse("1970T00:00:00.000Z")); 226assertEquals(0, Date.parse("1970T00:00:00.000")); 227assertEquals(0, Date.parse("1970T00:00:00")); 228assertEquals(0, Date.parse("1970T00:00")); 229assertEquals(0, Date.parse("1970")); 230 231assertEquals(0, Date.parse("+001970-01-01T00:00:00.000+00:00")); 232assertEquals(0, Date.parse("+001970-01-01T00:00:00.000-00:00")); 233assertEquals(0, Date.parse("+001970-01-01T00:00:00.000Z")); 234assertEquals(0, Date.parse("+001970-01-01T00:00:00.000")); 235assertEquals(0, Date.parse("+001970-01-01T00:00:00")); 236assertEquals(0, Date.parse("+001970-01-01T00:00")); 237assertEquals(0, Date.parse("+001970-01-01")); 238 239assertEquals(0, Date.parse("+001970-01T00:00:00.000+00:00")); 240assertEquals(0, Date.parse("+001970-01T00:00:00.000-00:00")); 241assertEquals(0, Date.parse("+001970-01T00:00:00.000Z")); 242assertEquals(0, Date.parse("+001970-01T00:00:00.000")); 243assertEquals(0, Date.parse("+001970-01T00:00:00")); 244assertEquals(0, Date.parse("+001970-01T00:00")); 245assertEquals(0, Date.parse("+001970-01")); 246 247assertEquals(0, Date.parse("+001970T00:00:00.000+00:00")); 248assertEquals(0, Date.parse("+001970T00:00:00.000-00:00")); 249assertEquals(0, Date.parse("+001970T00:00:00.000Z")); 250assertEquals(0, Date.parse("+001970T00:00:00.000")); 251assertEquals(0, Date.parse("+001970T00:00:00")); 252assertEquals(0, Date.parse("+001970T00:00")); 253assertEquals(0, Date.parse("+001970")); 254 255// Check random date. 256assertEquals(70671003500, Date.parse("1972-03-28T23:50:03.500+01:00")); 257assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 258assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500")); 259assertEquals(70674603000, Date.parse("1972-03-28T23:50:03")); 260assertEquals(70674600000, Date.parse("1972-03-28T23:50")); 261assertEquals(70588800000, Date.parse("1972-03-28")); 262 263assertEquals(68338203500, Date.parse("1972-03T23:50:03.500+01:00")); 264assertEquals(68341803500, Date.parse("1972-03T23:50:03.500Z")); 265assertEquals(68341803500, Date.parse("1972-03T23:50:03.500")); 266assertEquals(68341803000, Date.parse("1972-03T23:50:03")); 267assertEquals(68341800000, Date.parse("1972-03T23:50")); 268assertEquals(68256000000, Date.parse("1972-03")); 269 270assertEquals(63154203500, Date.parse("1972T23:50:03.500+01:00")); 271assertEquals(63157803500, Date.parse("1972T23:50:03.500Z")); 272assertEquals(63157803500, Date.parse("1972T23:50:03.500")); 273assertEquals(63157803000, Date.parse("1972T23:50:03")); 274assertEquals(63072000000, Date.parse("1972")); 275 276assertEquals(70671003500, Date.parse("+001972-03-28T23:50:03.500+01:00")); 277assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500Z")); 278assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500")); 279assertEquals(70674603000, Date.parse("+001972-03-28T23:50:03")); 280assertEquals(70674600000, Date.parse("+001972-03-28T23:50")); 281assertEquals(70588800000, Date.parse("+001972-03-28")); 282 283assertEquals(68338203500, Date.parse("+001972-03T23:50:03.500+01:00")); 284assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500Z")); 285assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500")); 286assertEquals(68341803000, Date.parse("+001972-03T23:50:03")); 287assertEquals(68341800000, Date.parse("+001972-03T23:50")); 288assertEquals(68256000000, Date.parse("+001972-03")); 289 290assertEquals(63154203500, Date.parse("+001972T23:50:03.500+01:00")); 291assertEquals(63157803500, Date.parse("+001972T23:50:03.500Z")); 292assertEquals(63157803500, Date.parse("+001972T23:50:03.500")); 293assertEquals(63157803000, Date.parse("+001972T23:50:03")); 294assertEquals(63072000000, Date.parse("+001972")); 295 296 297// Ensure that ISO-years in the range 00-99 aren't translated to the range 298// 1950..2049. 299assertEquals(-60904915200000, Date.parse("0040-01-01")); 300assertEquals(-60273763200000, Date.parse("0060-01-01")); 301assertEquals(-62167219200000, Date.parse("0000-01-01")); 302assertEquals(-62167219200000, Date.parse("+000000-01-01")); 303 304// Test negative years. 305assertEquals(-63429523200000, Date.parse("-000040-01-01")); 306assertEquals(-64060675200000, Date.parse("-000060-01-01")); 307assertEquals(-124397510400000, Date.parse("-001972-01-01")); 308 309// Check time-zones. 310assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 311for (var i = 0; i < 24; i++) { 312 var hh = (i < 10) ? "0" + i : "" + i; 313 for (var j = 0; j < 60; j += 15) { 314 var mm = (j < 10) ? "0" + j : "" + j; 315 var ms = (i * 60 + j) * 60000; 316 var string = "1972-03-28T23:50:03.500-" + hh + ":" + mm; 317 assertEquals(70674603500 + ms, Date.parse(string), string); 318 string = "1972-03-28T23:50:03.500+" + hh + ":" + mm; 319 assertEquals(70674603500 - ms, Date.parse(string), string); 320 } 321} 322