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