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// Flags: --allow-natives-syntax 29 30// Test date construction from other dates. 31var date0 = new Date(1111); 32var date1 = new Date(date0); 33assertEquals(1111, date0.getTime()); 34assertEquals(date0.getTime(), date1.getTime()); 35var date2 = new Date(date0.toString()); 36assertEquals(1000, date2.getTime()); 37 38// Test that dates may contain commas. 39var date0 = Date.parse("Dec 25 1995 1:30"); 40var date1 = Date.parse("Dec 25, 1995 1:30"); 41var date2 = Date.parse("Dec 25 1995, 1:30"); 42var date3 = Date.parse("Dec 25, 1995, 1:30"); 43assertEquals(date0, date1); 44assertEquals(date1, date2); 45assertEquals(date2, date3); 46 47// Test limits (+/-1e8 days from epoch) 48 49var dMax = new Date(8.64e15); 50assertEquals(8.64e15, dMax.getTime()); 51assertEquals(275760, dMax.getFullYear()); 52assertEquals(8, dMax.getMonth()); 53assertEquals(13, dMax.getUTCDate()); 54 55var dOverflow = new Date(8.64e15+1); 56assertTrue(isNaN(dOverflow.getTime())); 57 58var dMin = new Date(-8.64e15); 59assertEquals(-8.64e15, dMin.getTime()); 60assertEquals(-271821, dMin.getFullYear()); 61assertEquals(3, dMin.getMonth()); 62assertEquals(20, dMin.getUTCDate()); 63 64var dUnderflow = new Date(-8.64e15-1); 65assertTrue(isNaN(dUnderflow.getTime())); 66 67 68// Tests inspired by js1_5/Date/regress-346363.js 69 70// Year 71var a = new Date(); 72a.setFullYear(); 73a.setFullYear(2006); 74assertEquals(2006, a.getFullYear()); 75 76var b = new Date(); 77b.setUTCFullYear(); 78b.setUTCFullYear(2006); 79assertEquals(2006, b.getUTCFullYear()); 80 81// Month 82var c = new Date(); 83c.setMonth(); 84c.setMonth(2); 85assertTrue(isNaN(c.getMonth())); 86 87var d = new Date(); 88d.setUTCMonth(); 89d.setUTCMonth(2); 90assertTrue(isNaN(d.getUTCMonth())); 91 92// Date 93var e = new Date(); 94e.setDate(); 95e.setDate(2); 96assertTrue(isNaN(e.getDate())); 97 98var f = new Date(); 99f.setUTCDate(); 100f.setUTCDate(2); 101assertTrue(isNaN(f.getUTCDate())); 102 103// Hours 104var g = new Date(); 105g.setHours(); 106g.setHours(2); 107assertTrue(isNaN(g.getHours())); 108 109var h = new Date(); 110h.setUTCHours(); 111h.setUTCHours(2); 112assertTrue(isNaN(h.getUTCHours())); 113 114// Minutes 115var g = new Date(); 116g.setMinutes(); 117g.setMinutes(2); 118assertTrue(isNaN(g.getMinutes())); 119 120var h = new Date(); 121h.setUTCHours(); 122h.setUTCHours(2); 123assertTrue(isNaN(h.getUTCHours())); 124 125 126// Seconds 127var i = new Date(); 128i.setSeconds(); 129i.setSeconds(2); 130assertTrue(isNaN(i.getSeconds())); 131 132var j = new Date(); 133j.setUTCSeconds(); 134j.setUTCSeconds(2); 135assertTrue(isNaN(j.getUTCSeconds())); 136 137 138// Milliseconds 139var k = new Date(); 140k.setMilliseconds(); 141k.setMilliseconds(2); 142assertTrue(isNaN(k.getMilliseconds())); 143 144var l = new Date(); 145l.setUTCMilliseconds(); 146l.setUTCMilliseconds(2); 147assertTrue(isNaN(l.getUTCMilliseconds())); 148 149// Test that toLocaleTimeString only returns the time portion of the 150// date without the timezone information. 151function testToLocaleTimeString() { 152 var d = new Date(); 153 var s = d.toLocaleTimeString("en-GB"); 154 assertEquals(8, s.length); 155} 156 157testToLocaleTimeString(); 158 159// Test that -0 is treated correctly in MakeDay. 160var d = new Date(); 161assertDoesNotThrow("d.setDate(-0)"); 162assertDoesNotThrow("new Date(-0, -0, -0, -0, -0, -0, -0)"); 163assertDoesNotThrow("new Date(0x40000000, 0x40000000, 0x40000000," + 164 "0x40000000, 0x40000000, 0x40000000, 0x40000000)") 165assertDoesNotThrow("new Date(-0x40000001, -0x40000001, -0x40000001," + 166 "-0x40000001, -0x40000001, -0x40000001, -0x40000001)") 167 168 169// Modified test from WebKit 170// LayoutTests/fast/js/script-tests/date-utc-timeclip.js: 171 172assertEquals(8639999999999999, Date.UTC(275760, 8, 12, 23, 59, 59, 999)); 173assertEquals(8640000000000000, Date.UTC(275760, 8, 13)); 174assertTrue(isNaN(Date.UTC(275760, 8, 13, 0, 0, 0, 1))); 175assertTrue(isNaN(Date.UTC(275760, 8, 14))); 176 177assertEquals(Date.UTC(-271821, 3, 20, 0, 0, 0, 1), -8639999999999999); 178assertEquals(Date.UTC(-271821, 3, 20), -8640000000000000); 179assertTrue(isNaN(Date.UTC(-271821, 3, 19, 23, 59, 59, 999))); 180assertTrue(isNaN(Date.UTC(-271821, 3, 19))); 181 182 183// Test creation with large date values. 184d = new Date(1969, 12, 1, 99999999999); 185assertTrue(isNaN(d.getTime())); 186d = new Date(1969, 12, 1, -99999999999); 187assertTrue(isNaN(d.getTime())); 188d = new Date(1969, 12, 1, Infinity); 189assertTrue(isNaN(d.getTime())); 190d = new Date(1969, 12, 1, -Infinity); 191assertTrue(isNaN(d.getTime())); 192d = new Date(1969, 12, 1, 0); 193d.setTime(Math.pow(2, 64)); 194assertTrue(isNaN(d.getTime())); 195d = new Date(1969, 12, 1, 0); 196d.setTime(Math.pow(-2, 64)); 197assertTrue(isNaN(d.getTime())); 198 199 200// Test creation with obscure date values. 201assertEquals(8640000000000000, Date.UTC(1970, 0, 1 + 100000001, -24)); 202assertEquals(-8640000000000000, Date.UTC(1970, 0, 1 - 100000001, 24)); 203 204 205// Parsing ES5 ISO-8601 dates. 206// When TZ is omitted, it defaults to 'Z' meaning UTC. 207 208// Check epoch. 209assertEquals(0, Date.parse("1970-01-01T00:00:00.000+00:00")); 210assertEquals(0, Date.parse("1970-01-01T00:00:00.000-00:00")); 211assertEquals(0, Date.parse("1970-01-01T00:00:00.000Z")); 212assertEquals(0, Date.parse("1970-01-01T00:00:00.000")); 213assertEquals(0, Date.parse("1970-01-01T00:00:00")); 214assertEquals(0, Date.parse("1970-01-01T00:00")); 215assertEquals(0, Date.parse("1970-01-01")); 216 217assertEquals(0, Date.parse("1970-01T00:00:00.000+00:00")); 218assertEquals(0, Date.parse("1970-01T00:00:00.000-00:00")); 219assertEquals(0, Date.parse("1970-01T00:00:00.000Z")); 220assertEquals(0, Date.parse("1970-01T00:00:00.000")); 221assertEquals(0, Date.parse("1970-01T00:00:00")); 222assertEquals(0, Date.parse("1970-01T00:00")); 223assertEquals(0, Date.parse("1970-01")); 224 225assertEquals(0, Date.parse("1970T00:00:00.000+00:00")); 226assertEquals(0, Date.parse("1970T00:00:00.000-00:00")); 227assertEquals(0, Date.parse("1970T00:00:00.000Z")); 228assertEquals(0, Date.parse("1970T00:00:00.000")); 229assertEquals(0, Date.parse("1970T00:00:00")); 230assertEquals(0, Date.parse("1970T00:00")); 231assertEquals(0, Date.parse("1970")); 232 233assertEquals(0, Date.parse("+001970-01-01T00:00:00.000+00:00")); 234assertEquals(0, Date.parse("+001970-01-01T00:00:00.000-00:00")); 235assertEquals(0, Date.parse("+001970-01-01T00:00:00.000Z")); 236assertEquals(0, Date.parse("+001970-01-01T00:00:00.000")); 237assertEquals(0, Date.parse("+001970-01-01T00:00:00")); 238assertEquals(0, Date.parse("+001970-01-01T00:00")); 239assertEquals(0, Date.parse("+001970-01-01")); 240 241assertEquals(0, Date.parse("+001970-01T00:00:00.000+00:00")); 242assertEquals(0, Date.parse("+001970-01T00:00:00.000-00:00")); 243assertEquals(0, Date.parse("+001970-01T00:00:00.000Z")); 244assertEquals(0, Date.parse("+001970-01T00:00:00.000")); 245assertEquals(0, Date.parse("+001970-01T00:00:00")); 246assertEquals(0, Date.parse("+001970-01T00:00")); 247assertEquals(0, Date.parse("+001970-01")); 248 249assertEquals(0, Date.parse("+001970T00:00:00.000+00:00")); 250assertEquals(0, Date.parse("+001970T00:00:00.000-00:00")); 251assertEquals(0, Date.parse("+001970T00:00:00.000Z")); 252assertEquals(0, Date.parse("+001970T00:00:00.000")); 253assertEquals(0, Date.parse("+001970T00:00:00")); 254assertEquals(0, Date.parse("+001970T00:00")); 255assertEquals(0, Date.parse("+001970")); 256 257// Check random date. 258assertEquals(70671003500, Date.parse("1972-03-28T23:50:03.500+01:00")); 259assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 260assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500")); 261assertEquals(70674603000, Date.parse("1972-03-28T23:50:03")); 262assertEquals(70674600000, Date.parse("1972-03-28T23:50")); 263assertEquals(70588800000, Date.parse("1972-03-28")); 264 265assertEquals(68338203500, Date.parse("1972-03T23:50:03.500+01:00")); 266assertEquals(68341803500, Date.parse("1972-03T23:50:03.500Z")); 267assertEquals(68341803500, Date.parse("1972-03T23:50:03.500")); 268assertEquals(68341803000, Date.parse("1972-03T23:50:03")); 269assertEquals(68341800000, Date.parse("1972-03T23:50")); 270assertEquals(68256000000, Date.parse("1972-03")); 271 272assertEquals(63154203500, Date.parse("1972T23:50:03.500+01:00")); 273assertEquals(63157803500, Date.parse("1972T23:50:03.500Z")); 274assertEquals(63157803500, Date.parse("1972T23:50:03.500")); 275assertEquals(63157803000, Date.parse("1972T23:50:03")); 276assertEquals(63072000000, Date.parse("1972")); 277 278assertEquals(70671003500, Date.parse("+001972-03-28T23:50:03.500+01:00")); 279assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500Z")); 280assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500")); 281assertEquals(70674603000, Date.parse("+001972-03-28T23:50:03")); 282assertEquals(70674600000, Date.parse("+001972-03-28T23:50")); 283assertEquals(70588800000, Date.parse("+001972-03-28")); 284 285assertEquals(68338203500, Date.parse("+001972-03T23:50:03.500+01:00")); 286assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500Z")); 287assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500")); 288assertEquals(68341803000, Date.parse("+001972-03T23:50:03")); 289assertEquals(68341800000, Date.parse("+001972-03T23:50")); 290assertEquals(68256000000, Date.parse("+001972-03")); 291 292assertEquals(63154203500, Date.parse("+001972T23:50:03.500+01:00")); 293assertEquals(63157803500, Date.parse("+001972T23:50:03.500Z")); 294assertEquals(63157803500, Date.parse("+001972T23:50:03.500")); 295assertEquals(63157803000, Date.parse("+001972T23:50:03")); 296assertEquals(63072000000, Date.parse("+001972")); 297 298 299// Ensure that ISO-years in the range 00-99 aren't translated to the range 300// 1950..2049. 301assertEquals(-60904915200000, Date.parse("0040-01-01")); 302assertEquals(-60273763200000, Date.parse("0060-01-01")); 303assertEquals(-62167219200000, Date.parse("0000-01-01")); 304assertEquals(-62167219200000, Date.parse("+000000-01-01")); 305 306// Test negative years. 307assertEquals(-63429523200000, Date.parse("-000040-01-01")); 308assertEquals(-64060675200000, Date.parse("-000060-01-01")); 309assertEquals(-124397510400000, Date.parse("-001972-01-01")); 310 311// Check time-zones. 312assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 313for (var i = 0; i < 24; i++) { 314 var hh = (i < 10) ? "0" + i : "" + i; 315 for (var j = 0; j < 60; j += 15) { 316 var mm = (j < 10) ? "0" + j : "" + j; 317 var ms = (i * 60 + j) * 60000; 318 var string = "1972-03-28T23:50:03.500-" + hh + ":" + mm; 319 assertEquals(70674603500 + ms, Date.parse(string), string); 320 string = "1972-03-28T23:50:03.500+" + hh + ":" + mm; 321 assertEquals(70674603500 - ms, Date.parse(string), string); 322 } 323} 324 325assertThrows('Date.prototype.setTime.call("", 1);', TypeError); 326assertThrows('Date.prototype.setYear.call("", 1);', TypeError); 327assertThrows('Date.prototype.setHours.call("", 1, 2, 3, 4);', TypeError); 328assertThrows('Date.prototype.getDate.call("");', TypeError); 329assertThrows('Date.prototype.getUTCDate.call("");', TypeError); 330 331var date = new Date(); 332date.getTime(); 333date.getTime(); 334%OptimizeFunctionOnNextCall(Date.prototype.getTime); 335assertThrows(function() { Date.prototype.getTime.call(""); }, TypeError); 336assertUnoptimized(Date.prototype.getTime); 337 338date.getYear(); 339date.getYear(); 340%OptimizeFunctionOnNextCall(Date.prototype.getYear); 341assertThrows(function() { Date.prototype.getYear.call(""); }, TypeError); 342assertUnoptimized(Date.prototype.getYear); 343