Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / tests / ecma_5 / Date / 15.9.4.2.js
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is JavaScript Engine testing utilities.
16  *
17  * The Initial Developer of the Original Code is
18  * Mozilla Foundation.
19  * Portions created by the Initial Developer are Copyright (C) 2008
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s): Bruce Hoult
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 //-----------------------------------------------------------------------------
39 var BUGNUMBER = 430930;
40
41
42 //-----------------------------------------------------------------------------
43 test();
44 //-----------------------------------------------------------------------------
45
46 function iso(d){
47   return new Date(d).toISOString();
48 }
49
50 function check(s, millis){
51   description = "Date.parse('"+s+"') == '"+iso(millis)+"'";
52   expected = millis;
53   actual = Date.parse(s);
54   reportCompare(expected, actual, description);
55 }
56
57 function dd(year, month, day, hour, minute, second, millis){
58   return Date.UTC(year, month-1, day, hour, minute, second, millis);
59 }
60
61 function TZAtDate(d){
62   return d.getTimezoneOffset() * 60000;
63 }
64
65 function TZInMonth(month){
66   return TZAtDate(new Date(dd(2009,month,1,0,0,0,0)));
67 }
68
69 function test()
70 {
71   enterFunc ('test');
72   printBugNumber(BUGNUMBER);
73  
74   JanTZ = TZInMonth(1);
75   JulTZ = TZInMonth(7);
76   CurrTZ = TZAtDate(new Date());
77
78   // formats with explicit timezone
79   check("2009-07-23T19:53:21.001+12:00", dd(2009,7,23,7,53,21,1));
80   check("2009-07-23T19:53:21+12:00",     dd(2009,7,23,7,53,21,0));
81   check("2009-07-23T19:53+12:00",        dd(2009,7,23,7,53,0,0));
82
83   check("2009-07T19:53:21.001+12:00",    dd(2009,7,1,7,53,21,1));
84   check("2009-07T19:53:21+12:00",        dd(2009,7,1,7,53,21,0));
85   check("2009-07T19:53+12:00",           dd(2009,7,1,7,53,0,0));
86
87   check("2009T19:53:21.001+12:00",       dd(2009,1,1,7,53,21,1));
88   check("2009T19:53:21+12:00",           dd(2009,1,1,7,53,21,0));
89   check("2009T19:53+12:00",              dd(2009,1,1,7,53,0,0));
90
91   check("T19:53:21.001+12:00",           dd(1970,1,1,7,53,21,1));
92   check("T19:53:21+12:00",               dd(1970,1,1,7,53,21,0));
93   check("T19:53+12:00",                  dd(1970,1,1,7,53,0,0));
94
95   // formats without timezone uses the timezone as at that date
96   check("2009-07-23T19:53:21.001",       dd(2009,7,23,19,53,21,1)+JulTZ);
97   check("2009-07-23T19:53:21",           dd(2009,7,23,19,53,21,0)+JulTZ);
98   check("2009-07-23T19:53",              dd(2009,7,23,19,53,0,0)+JulTZ);
99
100   check("2009-07T19:53:21.001",          dd(2009,7,1,19,53,21,1)+JulTZ);
101   check("2009-07T19:53:21",              dd(2009,7,1,19,53,21,0)+JulTZ);
102   check("2009-07T19:53",                 dd(2009,7,1,19,53,0,0)+JulTZ);
103
104   check("2009T19:53:21.001",             dd(2009,1,1,19,53,21,1)+JanTZ);
105   check("2009T19:53:21",                 dd(2009,1,1,19,53,21,0)+JanTZ);
106   check("2009T19:53",                    dd(2009,1,1,19,53,0,0)+JanTZ);
107
108   check("T19:53:21.001",                 dd(1970,1,1,19,53,21,1)+JanTZ);
109   check("T19:53:21",                     dd(1970,1,1,19,53,21,0)+JanTZ);
110   check("T19:53",                        dd(1970,1,1,19,53,0,0)+JanTZ);
111
112   // with no time at all assume UTC
113   check("2009-07-23",                    dd(2009,7,23,0,0,0,0));
114   check("2009-07",                       dd(2009,7,1,0,0,0,0));
115   check("2009",                          dd(2009,1,1,0,0,0,0));
116   check("",                              NaN);
117
118   // one field too big
119   check("2009-13-23T19:53:21.001+12:00", NaN);
120   check("2009-07-32T19:53:21.001+12:00", NaN);
121   check("2009-07-23T25:53:21.001+12:00", NaN);
122   check("2009-07-23T19:60:21.001+12:00", NaN);
123   check("2009-07-23T19:53:60.001+12:00", NaN);
124   check("2009-07-23T19:53:21.001+24:00", NaN);
125   check("2009-07-23T19:53:21.001+12:60", NaN);
126
127   // other month ends
128   check("2009-06-30T19:53:21.001+12:00", dd(2009,6,30,7,53,21,1));
129   check("2009-06-31T19:53:21.001+12:00", NaN);
130   check("2009-02-28T19:53:21.001+12:00", dd(2009,2,28,7,53,21,1));
131   check("2009-02-29T19:53:21.001+12:00", NaN);
132   check("2008-02-29T19:53:21.001+12:00", dd(2008,2,29,7,53,21,1));
133   check("2008-02-30T19:53:21.001+12:00", NaN);
134
135   // limits of representation
136   check("-271821-04-19T23:59:59.999Z", NaN);
137   check("-271821-04-20", -8.64e15);
138   check("+275760-09-13", 8.64e15);
139   check("+275760-09-13T00:00:00.001Z", NaN);
140
141   check("-269845-07-23T19:53:21.001+12:00", dd(-269845,7,23,7,53,21,1));
142   check("+273785-07-23T19:53:21.001+12:00", dd(273785,7,23,7,53,21,1));
143
144   // explicit UTC
145   check("2009-07-23T19:53:21.001Z", dd(2009,7,23,19,53,21,1));
146   check("+002009-07-23T19:53:21.001Z", dd(2009,7,23,19,53,21,1));
147
148   // different timezones
149   check("2009-07-23T19:53:21.001+12:00", dd(2009,7,23,7,53,21,1));
150   check("2009-07-23T00:53:21.001-07:00", dd(2009,7,23,7,53,21,1));
151
152   // 00:00 and 24:00
153   check("2009-07-23T00:00:00.000-07:00", dd(2009,7,23,7,0,0,0));
154   check("2009-07-23T24:00:00.000-07:00", dd(2009,7,24,7,0,0,0));
155
156   exitFunc ('test');
157 }