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