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 -0 is treated correctly in MakeDay. 150var d = new Date(); 151assertDoesNotThrow("d.setDate(-0)"); 152assertDoesNotThrow("new Date(-0, -0, -0, -0, -0, -0, -0)"); 153assertDoesNotThrow("new Date(0x40000000, 0x40000000, 0x40000000," + 154 "0x40000000, 0x40000000, 0x40000000, 0x40000000)") 155assertDoesNotThrow("new Date(-0x40000001, -0x40000001, -0x40000001," + 156 "-0x40000001, -0x40000001, -0x40000001, -0x40000001)") 157 158 159// Modified test from WebKit 160// LayoutTests/fast/js/script-tests/date-utc-timeclip.js: 161 162assertEquals(8639999999999999, Date.UTC(275760, 8, 12, 23, 59, 59, 999)); 163assertEquals(8640000000000000, Date.UTC(275760, 8, 13)); 164assertTrue(isNaN(Date.UTC(275760, 8, 13, 0, 0, 0, 1))); 165assertTrue(isNaN(Date.UTC(275760, 8, 14))); 166 167assertEquals(Date.UTC(-271821, 3, 20, 0, 0, 0, 1), -8639999999999999); 168assertEquals(Date.UTC(-271821, 3, 20), -8640000000000000); 169assertTrue(isNaN(Date.UTC(-271821, 3, 19, 23, 59, 59, 999))); 170assertTrue(isNaN(Date.UTC(-271821, 3, 19))); 171 172 173// Test creation with large date values. 174d = new Date(1969, 12, 1, 99999999999); 175assertTrue(isNaN(d.getTime())); 176d = new Date(1969, 12, 1, -99999999999); 177assertTrue(isNaN(d.getTime())); 178d = new Date(1969, 12, 1, Infinity); 179assertTrue(isNaN(d.getTime())); 180d = new Date(1969, 12, 1, -Infinity); 181assertTrue(isNaN(d.getTime())); 182d = new Date(1969, 12, 1, 0); 183d.setTime(Math.pow(2, 64)); 184assertTrue(isNaN(d.getTime())); 185d = new Date(1969, 12, 1, 0); 186d.setTime(Math.pow(-2, 64)); 187assertTrue(isNaN(d.getTime())); 188 189 190// Test creation with obscure date values. 191assertEquals(8640000000000000, Date.UTC(1970, 0, 1 + 100000001, -24)); 192assertEquals(-8640000000000000, Date.UTC(1970, 0, 1 - 100000001, 24)); 193 194 195// Parsing ES5 ISO-8601 dates. 196// When TZ is omitted, it defaults to 'Z' meaning UTC. 197 198// Check epoch. 199assertEquals(0, Date.parse("1970-01-01T00:00:00.000+00:00")); 200assertEquals(0, Date.parse("1970-01-01T00:00:00.000-00:00")); 201assertEquals(0, Date.parse("1970-01-01T00:00:00.000Z")); 202assertEquals(0, Date.parse("1970-01-01T00:00:00.000")); 203assertEquals(0, Date.parse("1970-01-01T00:00:00")); 204assertEquals(0, Date.parse("1970-01-01T00:00")); 205assertEquals(0, Date.parse("1970-01-01")); 206 207assertEquals(0, Date.parse("1970-01T00:00:00.000+00:00")); 208assertEquals(0, Date.parse("1970-01T00:00:00.000-00:00")); 209assertEquals(0, Date.parse("1970-01T00:00:00.000Z")); 210assertEquals(0, Date.parse("1970-01T00:00:00.000")); 211assertEquals(0, Date.parse("1970-01T00:00:00")); 212assertEquals(0, Date.parse("1970-01T00:00")); 213assertEquals(0, Date.parse("1970-01")); 214 215assertEquals(0, Date.parse("1970T00:00:00.000+00:00")); 216assertEquals(0, Date.parse("1970T00:00:00.000-00:00")); 217assertEquals(0, Date.parse("1970T00:00:00.000Z")); 218assertEquals(0, Date.parse("1970T00:00:00.000")); 219assertEquals(0, Date.parse("1970T00:00:00")); 220assertEquals(0, Date.parse("1970T00:00")); 221assertEquals(0, Date.parse("1970")); 222 223assertEquals(0, Date.parse("+001970-01-01T00:00:00.000+00:00")); 224assertEquals(0, Date.parse("+001970-01-01T00:00:00.000-00:00")); 225assertEquals(0, Date.parse("+001970-01-01T00:00:00.000Z")); 226assertEquals(0, Date.parse("+001970-01-01T00:00:00.000")); 227assertEquals(0, Date.parse("+001970-01-01T00:00:00")); 228assertEquals(0, Date.parse("+001970-01-01T00:00")); 229assertEquals(0, Date.parse("+001970-01-01")); 230 231assertEquals(0, Date.parse("+001970-01T00:00:00.000+00:00")); 232assertEquals(0, Date.parse("+001970-01T00:00:00.000-00:00")); 233assertEquals(0, Date.parse("+001970-01T00:00:00.000Z")); 234assertEquals(0, Date.parse("+001970-01T00:00:00.000")); 235assertEquals(0, Date.parse("+001970-01T00:00:00")); 236assertEquals(0, Date.parse("+001970-01T00:00")); 237assertEquals(0, Date.parse("+001970-01")); 238 239assertEquals(0, Date.parse("+001970T00:00:00.000+00:00")); 240assertEquals(0, Date.parse("+001970T00:00:00.000-00:00")); 241assertEquals(0, Date.parse("+001970T00:00:00.000Z")); 242assertEquals(0, Date.parse("+001970T00:00:00.000")); 243assertEquals(0, Date.parse("+001970T00:00:00")); 244assertEquals(0, Date.parse("+001970T00:00")); 245assertEquals(0, Date.parse("+001970")); 246 247// Check random date. 248assertEquals(70671003500, Date.parse("1972-03-28T23:50:03.500+01:00")); 249assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 250assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500")); 251assertEquals(70674603000, Date.parse("1972-03-28T23:50:03")); 252assertEquals(70674600000, Date.parse("1972-03-28T23:50")); 253assertEquals(70588800000, Date.parse("1972-03-28")); 254 255assertEquals(68338203500, Date.parse("1972-03T23:50:03.500+01:00")); 256assertEquals(68341803500, Date.parse("1972-03T23:50:03.500Z")); 257assertEquals(68341803500, Date.parse("1972-03T23:50:03.500")); 258assertEquals(68341803000, Date.parse("1972-03T23:50:03")); 259assertEquals(68341800000, Date.parse("1972-03T23:50")); 260assertEquals(68256000000, Date.parse("1972-03")); 261 262assertEquals(63154203500, Date.parse("1972T23:50:03.500+01:00")); 263assertEquals(63157803500, Date.parse("1972T23:50:03.500Z")); 264assertEquals(63157803500, Date.parse("1972T23:50:03.500")); 265assertEquals(63157803000, Date.parse("1972T23:50:03")); 266assertEquals(63072000000, Date.parse("1972")); 267 268assertEquals(70671003500, Date.parse("+001972-03-28T23:50:03.500+01:00")); 269assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500Z")); 270assertEquals(70674603500, Date.parse("+001972-03-28T23:50:03.500")); 271assertEquals(70674603000, Date.parse("+001972-03-28T23:50:03")); 272assertEquals(70674600000, Date.parse("+001972-03-28T23:50")); 273assertEquals(70588800000, Date.parse("+001972-03-28")); 274 275assertEquals(68338203500, Date.parse("+001972-03T23:50:03.500+01:00")); 276assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500Z")); 277assertEquals(68341803500, Date.parse("+001972-03T23:50:03.500")); 278assertEquals(68341803000, Date.parse("+001972-03T23:50:03")); 279assertEquals(68341800000, Date.parse("+001972-03T23:50")); 280assertEquals(68256000000, Date.parse("+001972-03")); 281 282assertEquals(63154203500, Date.parse("+001972T23:50:03.500+01:00")); 283assertEquals(63157803500, Date.parse("+001972T23:50:03.500Z")); 284assertEquals(63157803500, Date.parse("+001972T23:50:03.500")); 285assertEquals(63157803000, Date.parse("+001972T23:50:03")); 286assertEquals(63072000000, Date.parse("+001972")); 287 288 289// Ensure that ISO-years in the range 00-99 aren't translated to the range 290// 1950..2049. 291assertEquals(-60904915200000, Date.parse("0040-01-01")); 292assertEquals(-60273763200000, Date.parse("0060-01-01")); 293assertEquals(-62167219200000, Date.parse("0000-01-01")); 294assertEquals(-62167219200000, Date.parse("+000000-01-01")); 295 296// Test negative years. 297assertEquals(-63429523200000, Date.parse("-000040-01-01")); 298assertEquals(-64060675200000, Date.parse("-000060-01-01")); 299assertEquals(-124397510400000, Date.parse("-001972-01-01")); 300 301// Check time-zones. 302assertEquals(70674603500, Date.parse("1972-03-28T23:50:03.500Z")); 303for (var i = 0; i < 24; i++) { 304 var hh = (i < 10) ? "0" + i : "" + i; 305 for (var j = 0; j < 60; j += 15) { 306 var mm = (j < 10) ? "0" + j : "" + j; 307 var ms = (i * 60 + j) * 60000; 308 var string = "1972-03-28T23:50:03.500-" + hh + ":" + mm; 309 assertEquals(70674603500 + ms, Date.parse(string), string); 310 string = "1972-03-28T23:50:03.500+" + hh + ":" + mm; 311 assertEquals(70674603500 - ms, Date.parse(string), string); 312 } 313} 314 315assertThrows('Date.prototype.setTime.call("", 1);', TypeError); 316assertThrows('Date.prototype.setYear.call("", 1);', TypeError); 317assertThrows('Date.prototype.setHours.call("", 1, 2, 3, 4);', TypeError); 318assertThrows('Date.prototype.getDate.call("");', TypeError); 319assertThrows('Date.prototype.getUTCDate.call("");', TypeError); 320 321assertThrows(function() { Date.prototype.getTime.call(0) }, TypeError); 322assertThrows(function() { Date.prototype.getTime.call("") }, TypeError); 323 324assertThrows(function() { Date.prototype.getYear.call(0) }, TypeError); 325assertThrows(function() { Date.prototype.getYear.call("") }, TypeError); 326 327(function TestDatePrototypeOrdinaryObject() { 328 assertEquals(Object.prototype, Date.prototype.__proto__); 329 assertThrows(function () { Date.prototype.toString() }, TypeError); 330})(); 331 332delete Date.prototype.getUTCFullYear; 333delete Date.prototype.getUTCMonth; 334delete Date.prototype.getUTCDate; 335delete Date.prototype.getUTCHours; 336delete Date.prototype.getUTCMinutes; 337delete Date.prototype.getUTCSeconds; 338delete Date.prototype.getUTCMilliseconds; 339(new Date()).toISOString(); 340 341(function TestDeleteToString() { 342 assertTrue(delete Date.prototype.toString); 343 assertTrue('[object Date]' !== Date()); 344})(); 345