• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright JS Foundation and other contributors, http://js.foundation
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15assert (new Date (NaN) == "Invalid Date");
16assert (new Date (Infinity, 1, 1, 0, 0, 0) == "Invalid Date");
17assert (new Date (2015, Infinity, 1, 0, 0, 0) == "Invalid Date");
18assert (new Date (2015, 7, 1, 0, Infinity, 0) == "Invalid Date");
19assert (new Date (NaN, 1, 1, 0, 0, 0) == "Invalid Date");
20assert (new Date (2015, NaN, 1, 0, 0, 0) == "Invalid Date");
21assert (new Date (2015, 7, 1, 0, NaN, 0) == "Invalid Date");
22assert (/Fri Feb 13 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (new Date ("2015-02-13")));
23assert (/Wed Jul 08 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (new Date ("2015-07-08T11:29:05.023")));
24
25try
26{
27  Date.prototype.toString.call(-1);
28  assert (false);
29}
30catch (e)
31{
32  assert (e instanceof TypeError);
33}
34
35var date = new Date(0);
36assert (/Thu Jan 01 1970 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (date.toString()));
37assert (date.toUTCString() === "Thu, 01 Jan 1970 00:00:00 GMT");
38assert (date.toISOString() === "1970-01-01T00:00:00.000Z");
39
40date = new Date("2015-08-12T09:40:20.000Z")
41assert (/Wed Aug 12 2015 \d{2}:\d{2}:\d{2} GMT\+\d{2}\d{2}/.test (date.toString()));
42assert (date.toUTCString() === "Wed, 12 Aug 2015 09:40:20 GMT");
43assert (date.toISOString() === "2015-08-12T09:40:20.000Z");
44
45assert (new Date (NaN).toDateString () == "Invalid Date");
46assert (new Date ("2015-02-13").toDateString () == "2015-02-13");
47assert (new Date ("2015-07-08T11:29:05.023").toDateString () == "2015-07-08");
48
49try
50{
51  Date.prototype.toDateString.call(-1);
52  assert (false);
53}
54catch (e)
55{
56  assert (e instanceof TypeError);
57}
58
59assert (new Date (NaN).toTimeString () == "Invalid Date");
60assert (new Date (Number.POSITIVE_INFINITY).toString () === "Invalid Date");
61assert (new Date ("2015-02-13").toTimeString () == "00:00:00.000");
62assert (new Date ("2015-07-08T11:29:05.023").toTimeString () == "11:29:05.023");
63
64try
65{
66  Date.prototype.toTimeString.call(-1);
67  assert (false);
68}
69catch (e)
70{
71  assert (e instanceof TypeError);
72}
73
74assert (new Date ("2015-07-16").toISOString () == "2015-07-16T00:00:00.000Z");
75assert (new Date ("2015-07-16T11:29:05.023").toISOString () == "2015-07-16T11:29:05.023Z");
76
77try
78{
79  new Date (NaN).toISOString ();
80  assert (false);
81}
82catch (e)
83{
84  assert (e instanceof RangeError);
85}
86
87try
88{
89  new Date (Number.POSITIVE_INFINITY).toISOString ();
90  assert (false);
91}
92catch (e)
93{
94  assert (e instanceof RangeError);
95}
96
97try
98{
99  Date.prototype.toISOString.call(-1);
100  assert (false);
101}
102catch (e)
103{
104  assert (e instanceof TypeError);
105}
106
107assert (new Date (NaN).toUTCString () == "Invalid Date");
108assert (new Date ("2015-07-16").toUTCString () == "Thu, 16 Jul 2015 00:00:00 GMT");
109assert (new Date ("2015-07-16T11:29:05.023").toUTCString () == "Thu, 16 Jul 2015 11:29:05 GMT");
110
111try
112{
113  Date.prototype.toUTCString.call(-1);
114  assert (false);
115}
116catch (e)
117{
118  assert (e instanceof TypeError);
119}
120
121assert (new Date (NaN).toJSON () == null);
122assert (new Date ("2015-07-16").toJSON () == "2015-07-16T00:00:00.000Z");
123assert (new Date ("2015-07-16T11:29:05.023").toJSON () == "2015-07-16T11:29:05.023Z");
124
125try
126{
127  Date.prototype.toJSON.call(-1);
128  assert (false);
129}
130catch (e)
131{
132  assert (e instanceof TypeError);
133}
134
135date_time = new Date ("2015-07-08T11:29:05.023").toJSON ();
136assert (new Date (date_time).toISOString () == "2015-07-08T11:29:05.023Z");
137
138assert (typeof Date (2015) == "string");
139assert (typeof Date() != typeof (new Date ()));
140assert (Date () == (new Date ()).toString ());
141assert (Date (2015, 1, 1) == (new Date ()).toString ());
142assert (Date (Number.NaN) == Date ());
143
144assert (new Date ("2015-07-08T11:29:05.023Z").toISOString() == "2015-07-08T11:29:05.023Z");
145
146// corner cases
147assert (new Date (-8640000000000001).toString() == "Invalid Date")
148assert (new Date (-8640000000000000).toString() == "Tue Apr 20 -271821 00:00:00 GMT+0000")
149
150assert (new Date(-62167219200001).toString() == "Fri Dec 31 -0001 23:59:59 GMT+0000")
151assert (new Date(-62167219200000).toString() == "Sat Jan 01 0000 00:00:00 GMT+0000")
152
153assert (new Date(-61851600000001).toString() == "Thu Dec 31 0009 23:59:59 GMT+0000")
154assert (new Date(-61851600000000).toString() == "Fri Jan 01 0010 00:00:00 GMT+0000")
155
156assert (new Date(-59011459200001).toString() == "Thu Dec 31 0099 23:59:59 GMT+0000")
157assert (new Date(-59011459200000).toString() == "Fri Jan 01 0100 00:00:00 GMT+0000")
158
159assert (new Date(-30610224000001).toString() == "Tue Dec 31 0999 23:59:59 GMT+0000")
160assert (new Date(-30610224000000).toString() == "Wed Jan 01 1000 00:00:00 GMT+0000")
161
162assert (new Date(-1).toString() == "Wed Dec 31 1969 23:59:59 GMT+0000")
163assert (new Date(0).toString() == "Thu Jan 01 1970 00:00:00 GMT+0000")
164assert (new Date(1).toString() == "Thu Jan 01 1970 00:00:00 GMT+0000")
165
166assert (new Date(253402300799999).toString() == "Fri Dec 31 9999 23:59:59 GMT+0000")
167assert (new Date(253402300800000).toString() == "Sat Jan 01 10000 00:00:00 GMT+0000")
168
169assert (new Date (8640000000000000).toString() == "Sat Sep 13 275760 00:00:00 GMT+0000")
170assert (new Date (8640000000000001).toString() == "Invalid Date")
171