| 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()); |
| 49 | |
| 50 | var dOverflow = new Date(8.64e15+1); |
| 51 | assertTrue(isNaN(dOverflow.getTime())); |
| 52 | |
| 53 | var dMin = new Date(-8.64e15); |
| 54 | assertEquals(-8.64e15, dMin.getTime()); |
| 55 | |
| 56 | var dUnderflow = new Date(-8.64e15-1); |
| 57 | assertTrue(isNaN(dUnderflow.getTime())); |
| 58 | |
| 59 | |
| 60 | // Tests inspired by js1_5/Date/regress-346363.js |
| 61 | |
| 62 | // Year |
| 63 | var a = new Date(); |
| 64 | a.setFullYear(); |
| 65 | a.setFullYear(2006); |
| 66 | assertEquals(2006, a.getFullYear()); |
| 67 | |
| 68 | var b = new Date(); |
| 69 | b.setUTCFullYear(); |
| 70 | b.setUTCFullYear(2006); |
| 71 | assertEquals(2006, b.getUTCFullYear()); |
| 72 | |
| 73 | // Month |
| 74 | var c = new Date(); |
| 75 | c.setMonth(); |
| 76 | c.setMonth(2); |
| 77 | assertTrue(isNaN(c.getMonth())); |
| 78 | |
| 79 | var d = new Date(); |
| 80 | d.setUTCMonth(); |
| 81 | d.setUTCMonth(2); |
| 82 | assertTrue(isNaN(d.getUTCMonth())); |
| 83 | |
| 84 | // Date |
| 85 | var e = new Date(); |
| 86 | e.setDate(); |
| 87 | e.setDate(2); |
| 88 | assertTrue(isNaN(e.getDate())); |
| 89 | |
| 90 | var f = new Date(); |
| 91 | f.setUTCDate(); |
| 92 | f.setUTCDate(2); |
| 93 | assertTrue(isNaN(f.getUTCDate())); |
| 94 | |
| 95 | // Hours |
| 96 | var g = new Date(); |
| 97 | g.setHours(); |
| 98 | g.setHours(2); |
| 99 | assertTrue(isNaN(g.getHours())); |
| 100 | |
| 101 | var h = new Date(); |
| 102 | h.setUTCHours(); |
| 103 | h.setUTCHours(2); |
| 104 | assertTrue(isNaN(h.getUTCHours())); |
| 105 | |
| 106 | // Minutes |
| 107 | var g = new Date(); |
| 108 | g.setMinutes(); |
| 109 | g.setMinutes(2); |
| 110 | assertTrue(isNaN(g.getMinutes())); |
| 111 | |
| 112 | var h = new Date(); |
| 113 | h.setUTCHours(); |
| 114 | h.setUTCHours(2); |
| 115 | assertTrue(isNaN(h.getUTCHours())); |
| 116 | |
| 117 | |
| 118 | // Seconds |
| 119 | var i = new Date(); |
| 120 | i.setSeconds(); |
| 121 | i.setSeconds(2); |
| 122 | assertTrue(isNaN(i.getSeconds())); |
| 123 | |
| 124 | var j = new Date(); |
| 125 | j.setUTCSeconds(); |
| 126 | j.setUTCSeconds(2); |
| 127 | assertTrue(isNaN(j.getUTCSeconds())); |
| 128 | |
| 129 | |
| 130 | // Milliseconds |
| 131 | var k = new Date(); |
| 132 | k.setMilliseconds(); |
| 133 | k.setMilliseconds(2); |
| 134 | assertTrue(isNaN(k.getMilliseconds())); |
| 135 | |
| 136 | var l = new Date(); |
| 137 | l.setUTCMilliseconds(); |
| 138 | l.setUTCMilliseconds(2); |
| 139 | assertTrue(isNaN(l.getUTCMilliseconds())); |
| 140 | |
| 141 | // Test that toLocaleTimeString only returns the time portion of the |
| 142 | // date without the timezone information. |
| 143 | function testToLocaleTimeString() { |
| 144 | var d = new Date(); |
| 145 | var s = d.toLocaleTimeString(); |
| 146 | assertEquals(8, s.length); |
| 147 | } |
| 148 | |
| 149 | testToLocaleTimeString(); |