| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 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. |
| 29 | var date0 = new Date(1111); |
| 30 | var date1 = new Date(date0); |
| 31 | assertEquals(1111, date0.getTime()); |
| 32 | assertEquals(date0.getTime(), date1.getTime()); |
| 33 | var date2 = new Date(date0.toString()); |
| 34 | assertEquals(1000, date2.getTime()); |
| 35 | |
| 36 | // Test that dates may contain commas. |
| 37 | var date0 = Date.parse("Dec 25 1995 1:30"); |
| 38 | var date1 = Date.parse("Dec 25, 1995 1:30"); |
| 39 | var date2 = Date.parse("Dec 25 1995, 1:30"); |
| 40 | var date3 = Date.parse("Dec 25, 1995, 1:30"); |
| 41 | assertEquals(date0, date1); |
| 42 | assertEquals(date1, date2); |
| 43 | assertEquals(date2, date3); |
| 44 | |
| 45 | // Test limits (+/-1e8 days from epoch) |
| 46 | |
| 47 | var dMax = new Date(8.64e15); |
| 48 | assertEquals(8.64e15, dMax.getTime()); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 49 | assertEquals(275760, dMax.getFullYear()); |
| 50 | assertEquals(8, dMax.getMonth()); |
| 51 | assertEquals(13, dMax.getUTCDate()); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 52 | |
| 53 | var dOverflow = new Date(8.64e15+1); |
| 54 | assertTrue(isNaN(dOverflow.getTime())); |
| 55 | |
| 56 | var dMin = new Date(-8.64e15); |
| 57 | assertEquals(-8.64e15, dMin.getTime()); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 58 | assertEquals(-271821, dMin.getFullYear()); |
| 59 | assertEquals(3, dMin.getMonth()); |
| 60 | assertEquals(20, dMin.getUTCDate()); |
| Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 61 | |
| 62 | var dUnderflow = new Date(-8.64e15-1); |
| 63 | assertTrue(isNaN(dUnderflow.getTime())); |
| 64 | |
| 65 | |
| 66 | // Tests inspired by js1_5/Date/regress-346363.js |
| 67 | |
| 68 | // Year |
| 69 | var a = new Date(); |
| 70 | a.setFullYear(); |
| 71 | a.setFullYear(2006); |
| 72 | assertEquals(2006, a.getFullYear()); |
| 73 | |
| 74 | var b = new Date(); |
| 75 | b.setUTCFullYear(); |
| 76 | b.setUTCFullYear(2006); |
| 77 | assertEquals(2006, b.getUTCFullYear()); |
| 78 | |
| 79 | // Month |
| 80 | var c = new Date(); |
| 81 | c.setMonth(); |
| 82 | c.setMonth(2); |
| 83 | assertTrue(isNaN(c.getMonth())); |
| 84 | |
| 85 | var d = new Date(); |
| 86 | d.setUTCMonth(); |
| 87 | d.setUTCMonth(2); |
| 88 | assertTrue(isNaN(d.getUTCMonth())); |
| 89 | |
| 90 | // Date |
| 91 | var e = new Date(); |
| 92 | e.setDate(); |
| 93 | e.setDate(2); |
| 94 | assertTrue(isNaN(e.getDate())); |
| 95 | |
| 96 | var f = new Date(); |
| 97 | f.setUTCDate(); |
| 98 | f.setUTCDate(2); |
| 99 | assertTrue(isNaN(f.getUTCDate())); |
| 100 | |
| 101 | // Hours |
| 102 | var g = new Date(); |
| 103 | g.setHours(); |
| 104 | g.setHours(2); |
| 105 | assertTrue(isNaN(g.getHours())); |
| 106 | |
| 107 | var h = new Date(); |
| 108 | h.setUTCHours(); |
| 109 | h.setUTCHours(2); |
| 110 | assertTrue(isNaN(h.getUTCHours())); |
| 111 | |
| 112 | // Minutes |
| 113 | var g = new Date(); |
| 114 | g.setMinutes(); |
| 115 | g.setMinutes(2); |
| 116 | assertTrue(isNaN(g.getMinutes())); |
| 117 | |
| 118 | var h = new Date(); |
| 119 | h.setUTCHours(); |
| 120 | h.setUTCHours(2); |
| 121 | assertTrue(isNaN(h.getUTCHours())); |
| 122 | |
| 123 | |
| 124 | // Seconds |
| 125 | var i = new Date(); |
| 126 | i.setSeconds(); |
| 127 | i.setSeconds(2); |
| 128 | assertTrue(isNaN(i.getSeconds())); |
| 129 | |
| 130 | var j = new Date(); |
| 131 | j.setUTCSeconds(); |
| 132 | j.setUTCSeconds(2); |
| 133 | assertTrue(isNaN(j.getUTCSeconds())); |
| 134 | |
| 135 | |
| 136 | // Milliseconds |
| 137 | var k = new Date(); |
| 138 | k.setMilliseconds(); |
| 139 | k.setMilliseconds(2); |
| 140 | assertTrue(isNaN(k.getMilliseconds())); |
| 141 | |
| 142 | var l = new Date(); |
| 143 | l.setUTCMilliseconds(); |
| 144 | l.setUTCMilliseconds(2); |
| 145 | assertTrue(isNaN(l.getUTCMilliseconds())); |
| 146 | |
| 147 | // Test that toLocaleTimeString only returns the time portion of the |
| 148 | // date without the timezone information. |
| 149 | function testToLocaleTimeString() { |
| 150 | var d = new Date(); |
| 151 | var s = d.toLocaleTimeString(); |
| 152 | assertEquals(8, s.length); |
| 153 | } |
| 154 | |
| 155 | testToLocaleTimeString(); |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 156 | |
| Steve Block | 8defd9f | 2010-07-08 12:39:36 +0100 | [diff] [blame] | 157 | // Test that -0 is treated correctly in MakeDay. |
| 158 | var d = new Date(); |
| 159 | assertDoesNotThrow("d.setDate(-0)"); |
| 160 | assertDoesNotThrow("new Date(-0, -0, -0, -0, -0, -0. -0)"); |
| 161 | assertDoesNotThrow("new Date(0x40000000, 0x40000000, 0x40000000," + |
| 162 | "0x40000000, 0x40000000, 0x40000000, 0x40000000)") |
| 163 | assertDoesNotThrow("new Date(-0x40000001, -0x40000001, -0x40000001," + |
| 164 | "-0x40000001, -0x40000001, -0x40000001, -0x40000001)") |
| 165 | |
| Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 166 | |
| 167 | // Modified test from WebKit |
| 168 | // LayoutTests/fast/js/script-tests/date-utc-timeclip.js: |
| 169 | |
| 170 | assertEquals(Date.UTC(275760, 8, 12, 23, 59, 59, 999), 8639999999999999); |
| 171 | assertEquals(Date.UTC(275760, 8, 13), 8640000000000000); |
| 172 | assertTrue(isNaN(Date.UTC(275760, 8, 13, 0, 0, 0, 1))); |
| 173 | assertTrue(isNaN(Date.UTC(275760, 8, 14))); |
| 174 | |
| 175 | assertEquals(Date.UTC(-271821, 3, 20, 0, 0, 0, 1), -8639999999999999); |
| 176 | assertEquals(Date.UTC(-271821, 3, 20), -8640000000000000); |
| 177 | assertTrue(isNaN(Date.UTC(-271821, 3, 19, 23, 59, 59, 999))); |
| 178 | assertTrue(isNaN(Date.UTC(-271821, 3, 19))); |