Debian Codegraph @

js/src/tests/non262/Date/15.9.4.2.js

raw
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 //-----------------------------------------------------------------------------
7 var BUGNUMBER = 430930;
8
9
10 //-----------------------------------------------------------------------------
11 test();
12 //-----------------------------------------------------------------------------
13
14 function iso(d)
15 {
16 return new Date(d).toISOString();
17 }
18
19 function check(s, millis){
20 description = "Date.parse('"+s+"') == '"+iso(millis)+"'";
21 expected = millis;
22 actual = Date.parse(s);
23 reportCompare(expected, actual, description);
24 }
25
26 function checkInvalid(s)
27 {
28 description = "Date.parse('"+s+"') produces invalid date";
29 expected = NaN;
30 actual = Date.parse(s);
31 reportCompare(expected, actual, description);
32 }
33
34 function dd(year, month, day, hour, minute, second, millis){
35 return Date.UTC(year, month-1, day, hour, minute, second, millis);
36 }
37
38 function TZAtDate(d){
39 return d.getTimezoneOffset() * 60000;
40 }
41
42 function TZInMonth(month, year){
43 return TZAtDate(new Date(dd(year,month,1,0,0,0,0)));
44 }
45
46 function test()
47 {
48 printBugNumber(BUGNUMBER);
49
50 Jan1970TZ = TZInMonth(1, 1970);
51 Jan2009TZ = TZInMonth(1, 2009);
52 Jul2009TZ = TZInMonth(7, 2009);
53 CurrTZ = TZAtDate(new Date());
54
55 // formats with explicit timezone
56 check("2009-07-23T19:53:21.001+12:00", dd(2009,7,23,7,53,21,1));
57 check("2009-07-23T19:53:21+12:00", dd(2009,7,23,7,53,21,0));
58 check("2009-07-23T19:53+12:00", dd(2009,7,23,7,53,0,0));
59
60 check("2009-07T19:53:21.001+12:00", dd(2009,7,1,7,53,21,1));
61 check("2009-07T19:53:21+12:00", dd(2009,7,1,7,53,21,0));
62 check("2009-07T19:53+12:00", dd(2009,7,1,7,53,0,0));
63
64 check("2009T19:53:21.001+12:00", dd(2009,1,1,7,53,21,1));
65 check("2009T19:53:21+12:00", dd(2009,1,1,7,53,21,0));
66 check("2009T19:53+12:00", dd(2009,1,1,7,53,0,0));
67
68 checkInvalid("T19:53:21.001+12:00");
69 checkInvalid("T19:53:21+12:00");
70 checkInvalid("T19:53+12:00");
71
72 // formats without timezone uses the timezone as at that date
73 check("2009-07-23T19:53:21.001", dd(2009,7,23,19,53,21,1)+Jul2009TZ);
74 check("2009-07-23T19:53:21", dd(2009,7,23,19,53,21,0)+Jul2009TZ);
75 check("2009-07-23T19:53", dd(2009,7,23,19,53,0,0)+Jul2009TZ);
76
77 check("2009-07T19:53:21.001", dd(2009,7,1,19,53,21,1)+Jul2009TZ);
78 check("2009-07T19:53:21", dd(2009,7,1,19,53,21,0)+Jul2009TZ);
79 check("2009-07T19:53", dd(2009,7,1,19,53,0,0)+Jul2009TZ);
80
81 check("2009T19:53:21.001", dd(2009,1,1,19,53,21,1)+Jan2009TZ);
82 check("2009T19:53:21", dd(2009,1,1,19,53,21,0)+Jan2009TZ);
83 check("2009T19:53", dd(2009,1,1,19,53,0,0)+Jan2009TZ);
84
85 checkInvalid("T19:53:21.001");
86 checkInvalid("T19:53:21");
87 checkInvalid("T19:53");
88
89 // with no time at all assume UTC
90 check("2009-07-23", dd(2009,7,23,0,0,0,0));
91 check("2009-07", dd(2009,7,1,0,0,0,0));
92 check("2009", dd(2009,1,1,0,0,0,0));
93
94 // one field too big
95 checkInvalid("2009-13-23T19:53:21.001+12:00");
96 checkInvalid("2009-07-32T19:53:21.001+12:00");
97 checkInvalid("2009-07-23T25:53:21.001+12:00");
98 checkInvalid("2009-07-23T19:60:21.001+12:00");
99 checkInvalid("2009-07-23T19:53:60.001+12:00");
100 checkInvalid("2009-07-23T19:53:21.001+24:00");
101 checkInvalid("2009-07-23T19:53:21.001+12:60");
102
103 // other month ends
104 check("2009-06-30T19:53:21.001+12:00", dd(2009,6,30,7,53,21,1));
105 check("2009-06-31T19:53:21.001+12:00", dd(2009,7,1,7,53,21,1));
106 check("2009-02-28T19:53:21.001+12:00", dd(2009,2,28,7,53,21,1));
107 check("2009-02-29T19:53:21.001+12:00", dd(2009,3,1,7,53,21,1));
108 check("2008-02-29T19:53:21.001+12:00", dd(2008,2,29,7,53,21,1));
109 check("2008-02-30T19:53:21.001+12:00", dd(2008,3,1,7,53,21,1));
110
111 // limits of representation
112 checkInvalid("-271821-04-19T23:59:59.999Z");
113 check("-271821-04-20", -8.64e15);
114 check("+275760-09-13", 8.64e15);
115 checkInvalid("+275760-09-13T00:00:00.001Z");
116
117 check("-269845-07-23T19:53:21.001+12:00", dd(-269845,7,23,7,53,21,1));
118 check("+273785-07-23T19:53:21.001+12:00", dd(273785,7,23,7,53,21,1));
119
120 // explicit UTC
121 check("2009-07-23T19:53:21.001Z", dd(2009,7,23,19,53,21,1));
122 check("+002009-07-23T19:53:21.001Z", dd(2009,7,23,19,53,21,1));
123
124 // different timezones
125 check("2009-07-23T19:53:21.001+12:00", dd(2009,7,23,7,53,21,1));
126 check("2009-07-23T00:53:21.001-07:00", dd(2009,7,23,7,53,21,1));
127
128 // 00:00 and 24:00
129 check("2009-07-23T00:00:00.000-07:00", dd(2009,7,23,7,0,0,0));
130 check("2009-07-23T24:00:00.000-07:00", dd(2009,7,24,7,0,0,0));
131
132 // Bug 730838 - non-zero fraction part for midnight should produce NaN
133 checkInvalid("1970-01-01T24:00:00.500Z");
134 }