blob: f13af8266db5768ea3aca519ba02dee649b2cd58 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// 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());
Steve Block6ded16b2010-05-10 14:33:55 +010049assertEquals(275760, dMax.getFullYear());
50assertEquals(8, dMax.getMonth());
51assertEquals(13, dMax.getUTCDate());
Steve Blocka7e24c12009-10-30 11:49:00 +000052
53var dOverflow = new Date(8.64e15+1);
54assertTrue(isNaN(dOverflow.getTime()));
55
56var dMin = new Date(-8.64e15);
57assertEquals(-8.64e15, dMin.getTime());
Steve Block6ded16b2010-05-10 14:33:55 +010058assertEquals(-271821, dMin.getFullYear());
59assertEquals(3, dMin.getMonth());
60assertEquals(20, dMin.getUTCDate());
Steve Blocka7e24c12009-10-30 11:49:00 +000061
62var dUnderflow = new Date(-8.64e15-1);
63assertTrue(isNaN(dUnderflow.getTime()));
64
65
66// Tests inspired by js1_5/Date/regress-346363.js
67
68// Year
69var a = new Date();
70a.setFullYear();
71a.setFullYear(2006);
72assertEquals(2006, a.getFullYear());
73
74var b = new Date();
75b.setUTCFullYear();
76b.setUTCFullYear(2006);
77assertEquals(2006, b.getUTCFullYear());
78
79// Month
80var c = new Date();
81c.setMonth();
82c.setMonth(2);
83assertTrue(isNaN(c.getMonth()));
84
85var d = new Date();
86d.setUTCMonth();
87d.setUTCMonth(2);
88assertTrue(isNaN(d.getUTCMonth()));
89
90// Date
91var e = new Date();
92e.setDate();
93e.setDate(2);
94assertTrue(isNaN(e.getDate()));
95
96var f = new Date();
97f.setUTCDate();
98f.setUTCDate(2);
99assertTrue(isNaN(f.getUTCDate()));
100
101// Hours
102var g = new Date();
103g.setHours();
104g.setHours(2);
105assertTrue(isNaN(g.getHours()));
106
107var h = new Date();
108h.setUTCHours();
109h.setUTCHours(2);
110assertTrue(isNaN(h.getUTCHours()));
111
112// Minutes
113var g = new Date();
114g.setMinutes();
115g.setMinutes(2);
116assertTrue(isNaN(g.getMinutes()));
117
118var h = new Date();
119h.setUTCHours();
120h.setUTCHours(2);
121assertTrue(isNaN(h.getUTCHours()));
122
123
124// Seconds
125var i = new Date();
126i.setSeconds();
127i.setSeconds(2);
128assertTrue(isNaN(i.getSeconds()));
129
130var j = new Date();
131j.setUTCSeconds();
132j.setUTCSeconds(2);
133assertTrue(isNaN(j.getUTCSeconds()));
134
135
136// Milliseconds
137var k = new Date();
138k.setMilliseconds();
139k.setMilliseconds(2);
140assertTrue(isNaN(k.getMilliseconds()));
141
142var l = new Date();
143l.setUTCMilliseconds();
144l.setUTCMilliseconds(2);
145assertTrue(isNaN(l.getUTCMilliseconds()));
146
147// Test that toLocaleTimeString only returns the time portion of the
148// date without the timezone information.
149function testToLocaleTimeString() {
150 var d = new Date();
151 var s = d.toLocaleTimeString();
152 assertEquals(8, s.length);
153}
154
155testToLocaleTimeString();
Steve Block6ded16b2010-05-10 14:33:55 +0100156
Steve Block8defd9f2010-07-08 12:39:36 +0100157// Test that -0 is treated correctly in MakeDay.
158var d = new Date();
159assertDoesNotThrow("d.setDate(-0)");
160assertDoesNotThrow("new Date(-0, -0, -0, -0, -0, -0. -0)");
161assertDoesNotThrow("new Date(0x40000000, 0x40000000, 0x40000000," +
162 "0x40000000, 0x40000000, 0x40000000, 0x40000000)")
163assertDoesNotThrow("new Date(-0x40000001, -0x40000001, -0x40000001," +
164 "-0x40000001, -0x40000001, -0x40000001, -0x40000001)")
165
Steve Block6ded16b2010-05-10 14:33:55 +0100166
167// Modified test from WebKit
168// LayoutTests/fast/js/script-tests/date-utc-timeclip.js:
169
Ben Murdochb0fe1622011-05-05 13:52:32 +0100170assertEquals(8639999999999999, Date.UTC(275760, 8, 12, 23, 59, 59, 999));
171assertEquals(8640000000000000, Date.UTC(275760, 8, 13));
Steve Block6ded16b2010-05-10 14:33:55 +0100172assertTrue(isNaN(Date.UTC(275760, 8, 13, 0, 0, 0, 1)));
173assertTrue(isNaN(Date.UTC(275760, 8, 14)));
174
175assertEquals(Date.UTC(-271821, 3, 20, 0, 0, 0, 1), -8639999999999999);
176assertEquals(Date.UTC(-271821, 3, 20), -8640000000000000);
177assertTrue(isNaN(Date.UTC(-271821, 3, 19, 23, 59, 59, 999)));
178assertTrue(isNaN(Date.UTC(-271821, 3, 19)));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100179
180
181// Test creation of large date values.
182d = new Date(1969, 12, 1, 99999999999);
183assertTrue(isNaN(d.getTime()));
184d = new Date(1969, 12, 1, -99999999999);
185assertTrue(isNaN(d.getTime()));
186d = new Date(1969, 12, 1, Infinity);
187assertTrue(isNaN(d.getTime()));
188d = new Date(1969, 12, 1, -Infinity);
189assertTrue(isNaN(d.getTime()));