The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.

Please select the desired protocol below to get the URL.

This URL has Read-Only access.

Statistics
| Branch: | Revision:

main_repo / deps / v8 / test / mjsunit / date-parse.js @ 40c0f755

History | View | Annotate | Download (9.55 KB)

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 that we can parse dates in all the different formats that we
29
// have to support.
30
//
31
// These formats are all supported by KJS but a lot of them are not
32
// supported by Spidermonkey.
33

    
34
function testDateParse(string) {
35
  var d = Date.parse(string);
36
  assertEquals(946713600000, d, string);
37
};
38

    
39

    
40
// For local time we just test that parsing returns non-NaN positive
41
// number of milliseconds to make it timezone independent.
42
function testDateParseLocalTime(string) {
43
  var d = Date.parse(string);
44
  assertTrue(!isNaN(d), string + " is NaN.");
45
  assertTrue(d > 0, string + " <= 0.");
46
};
47

    
48

    
49
function testDateParseMisc(array) {
50
  assertEquals(2, array.length, "array [" + array + "] length != 2.");
51
  var string = array[0];
52
  var expected = array[1];
53
  var d = Date.parse(string);
54
  assertEquals(expected, d, string);
55
}
56

    
57

    
58
//
59
// Test all the formats in UT timezone.
60
//
61
var testCasesUT = [
62
    'Sat, 01-Jan-2000 08:00:00 UT',
63
    'Sat, 01 Jan 2000 08:00:00 UT',
64
    'Jan 01 2000 08:00:00 UT',
65
    'Jan 01 08:00:00 UT 2000',
66
    'Saturday, 01-Jan-00 08:00:00 UT',
67
    '01 Jan 00 08:00 +0000',
68
    // Ignore weekdays.
69
    'Mon, 01 Jan 2000 08:00:00 UT',
70
    'Tue, 01 Jan 2000 08:00:00 UT',
71
    // Ignore prefix that is not part of a date.
72
    '[Saturday] Jan 01 08:00:00 UT 2000',
73
    'Ignore all of this stuff because it is annoying 01 Jan 2000 08:00:00 UT',
74
    '[Saturday] Jan 01 2000 08:00:00 UT',
75
    'All of this stuff is really annnoying, so it will be ignored Jan 01 2000 08:00:00 UT',
76
    // If the three first letters of the month is a
77
    // month name we are happy - ignore the rest.
78
    'Sat, 01-Janisamonth-2000 08:00:00 UT',
79
    'Sat, 01 Janisamonth 2000 08:00:00 UT',
80
    'Janisamonth 01 2000 08:00:00 UT',
81
    'Janisamonth 01 08:00:00 UT 2000',
82
    'Saturday, 01-Janisamonth-00 08:00:00 UT',
83
    '01 Janisamonth 00 08:00 +0000',
84
    // Allow missing space between month and day.
85
    'Janisamonthandtherestisignored01 2000 08:00:00 UT',
86
    'Jan01 2000 08:00:00 UT',
87
    // Allow year/month/day format.
88
    'Sat, 2000/01/01 08:00:00 UT',
89
    // Allow month/day/year format.
90
    'Sat, 01/01/2000 08:00:00 UT',
91
    // Allow month/day year format.
92
    'Sat, 01/01 2000 08:00:00 UT',
93
    // Allow comma instead of space after day, month and year.
94
    'Sat, 01,Jan,2000,08:00:00 UT',
95
    // Seconds are optional.
96
    'Sat, 01-Jan-2000 08:00 UT',
97
    'Sat, 01 Jan 2000 08:00 UT',
98
    'Jan 01 2000 08:00 UT',
99
    'Jan 01 08:00 UT 2000',
100
    'Saturday, 01-Jan-00 08:00 UT',
101
    '01 Jan 00 08:00 +0000',
102
    // Allow AM/PM after the time.
103
    'Sat, 01-Jan-2000 08:00 AM UT',
104
    'Sat, 01 Jan 2000 08:00 AM UT',
105
    'Jan 01 2000 08:00 AM UT',
106
    'Jan 01 08:00 AM UT 2000',
107
    'Saturday, 01-Jan-00 08:00 AM UT',
108
    '01 Jan 00 08:00 AM +0000',
109
    // White space and stuff in parenthesis is
110
    // apparently allowed in most places where white
111
    // space is allowed.
112
    '   Sat,   01-Jan-2000   08:00:00   UT  ',
113
    '  Sat,   01   Jan   2000   08:00:00   UT  ',
114
    '  Saturday,   01-Jan-00   08:00:00   UT  ',
115
    '  01    Jan   00    08:00   +0000   ',
116
    ' ()(Sat, 01-Jan-2000)  Sat,   01-Jan-2000   08:00:00   UT  ',
117
    '  Sat()(Sat, 01-Jan-2000)01   Jan   2000   08:00:00   UT  ',
118
    '  Sat,(02)01   Jan   2000   08:00:00   UT  ',
119
    '  Sat,  01(02)Jan   2000   08:00:00   UT  ',
120
    '  Sat,  01  Jan  2000 (2001)08:00:00   UT  ',
121
    '  Sat,  01  Jan  2000 (01)08:00:00   UT  ',
122
    '  Sat,  01  Jan  2000 (01:00:00)08:00:00   UT  ',
123
    '  Sat,  01  Jan  2000  08:00:00 (CDT)UT  ',
124
    '  Sat,  01  Jan  2000  08:00:00  UT((((CDT))))',
125
    '  Saturday,   01-Jan-00 ()(((asfd)))(Sat, 01-Jan-2000)08:00:00   UT  ',
126
    '  01    Jan   00    08:00 ()(((asdf)))(Sat, 01-Jan-2000)+0000   ',
127
    '  01    Jan   00    08:00   +0000()((asfd)(Sat, 01-Jan-2000)) '];
128

    
129
//
130
// Test that we do the right correction for different time zones.
131
// I'll assume that we can handle the same formats as for UT and only
132
// test a few formats for each of the timezones.
133
//
134

    
135
// GMT = UT
136
var testCasesGMT = [
137
    'Sat, 01-Jan-2000 08:00:00 GMT',
138
    'Sat, 01-Jan-2000 08:00:00 GMT+0',
139
    'Sat, 01-Jan-2000 08:00:00 GMT+00',
140
    'Sat, 01-Jan-2000 08:00:00 GMT+000',
141
    'Sat, 01-Jan-2000 08:00:00 GMT+0000',
142
    'Sat, 01-Jan-2000 08:00:00 GMT+00:00', // Interestingly, KJS cannot handle this.
143
    'Sat, 01 Jan 2000 08:00:00 GMT',
144
    'Saturday, 01-Jan-00 08:00:00 GMT',
145
    '01 Jan 00 08:00 -0000',
146
    '01 Jan 00 08:00 +0000'];
147

    
148
// EST = UT minus 5 hours.
149
var testCasesEST = [
150
    'Sat, 01-Jan-2000 03:00:00 UTC-0500',
151
    'Sat, 01-Jan-2000 03:00:00 UTC-05:00', // Interestingly, KJS cannot handle this.
152
    'Sat, 01-Jan-2000 03:00:00 EST',
153
    'Sat, 01 Jan 2000 03:00:00 EST',
154
    'Saturday, 01-Jan-00 03:00:00 EST',
155
    '01 Jan 00 03:00 -0500'];
156

    
157
// EDT = UT minus 4 hours.
158
var testCasesEDT = [
159
    'Sat, 01-Jan-2000 04:00:00 EDT',
160
    'Sat, 01 Jan 2000 04:00:00 EDT',
161
    'Saturday, 01-Jan-00 04:00:00 EDT',
162
    '01 Jan 00 04:00 -0400'];
163

    
164
// CST = UT minus 6 hours.
165
var testCasesCST = [
166
    'Sat, 01-Jan-2000 02:00:00 CST',
167
    'Sat, 01 Jan 2000 02:00:00 CST',
168
    'Saturday, 01-Jan-00 02:00:00 CST',
169
    '01 Jan 00 02:00 -0600'];
170

    
171
// CDT = UT minus 5 hours.
172
var testCasesCDT = [
173
    'Sat, 01-Jan-2000 03:00:00 CDT',
174
    'Sat, 01 Jan 2000 03:00:00 CDT',
175
    'Saturday, 01-Jan-00 03:00:00 CDT',
176
    '01 Jan 00 03:00 -0500'];
177

    
178
// MST = UT minus 7 hours.
179
var testCasesMST = [
180
    'Sat, 01-Jan-2000 01:00:00 MST',
181
    'Sat, 01 Jan 2000 01:00:00 MST',
182
    'Saturday, 01-Jan-00 01:00:00 MST',
183
    '01 Jan 00 01:00 -0700'];
184

    
185
// MDT = UT minus 6 hours.
186
var testCasesMDT = [
187
    'Sat, 01-Jan-2000 02:00:00 MDT',
188
    'Sat, 01 Jan 2000 02:00:00 MDT',
189
    'Saturday, 01-Jan-00 02:00:00 MDT',
190
    '01 Jan 00 02:00 -0600'];
191

    
192
// PST = UT minus 8 hours.
193
var testCasesPST = [
194
    'Sat, 01-Jan-2000 00:00:00 PST',
195
    'Sat, 01 Jan 2000 00:00:00 PST',
196
    'Saturday, 01-Jan-00 00:00:00 PST',
197
    '01 Jan 00 00:00 -0800',
198
    // Allow missing time.
199
    'Sat, 01-Jan-2000 PST'];
200

    
201
// PDT = UT minus 7 hours.
202
var testCasesPDT = [
203
    'Sat, 01-Jan-2000 01:00:00 PDT',
204
    'Sat, 01 Jan 2000 01:00:00 PDT',
205
    'Saturday, 01-Jan-00 01:00:00 PDT',
206
    '01 Jan 00 01:00 -0700'];
207

    
208

    
209
// Local time cases.
210
var testCasesLocalTime = [
211
    // Allow timezone ommision.
212
    'Sat, 01-Jan-2000 08:00:00',
213
    'Sat, 01 Jan 2000 08:00:00',
214
    'Jan 01 2000 08:00:00',
215
    'Jan 01 08:00:00 2000',
216
    'Saturday, 01-Jan-00 08:00:00',
217
    '01 Jan 00 08:00'];
218

    
219

    
220
// Misc. test cases that result in a different time value.
221
var testCasesMisc = [
222
    // Special handling for years in the [0, 100) range.
223
    ['Sat, 01 Jan 0 08:00:00 UT', 946713600000], // year 2000
224
    ['Sat, 01 Jan 49 08:00:00 UT', 2493100800000], // year 2049
225
    ['Sat, 01 Jan 50 08:00:00 UT', -631123200000], // year 1950
226
    ['Sat, 01 Jan 99 08:00:00 UT', 915177600000], // year 1999
227
    ['Sat, 01 Jan 100 08:00:00 UT', -59011430400000], // year 100
228
    // Test PM after time.
229
    ['Sat, 01-Jan-2000 08:00 PM UT', 946756800000],
230
    ['Sat, 01 Jan 2000 08:00 PM UT', 946756800000],
231
    ['Jan 01 2000 08:00 PM UT', 946756800000],
232
    ['Jan 01 08:00 PM UT 2000', 946756800000],
233
    ['Saturday, 01-Jan-00 08:00 PM UT', 946756800000],
234
    ['01 Jan 00 08:00 PM +0000', 946756800000]];
235

    
236

    
237
// Run all the tests.
238
testCasesUT.forEach(testDateParse);
239
testCasesGMT.forEach(testDateParse);
240
testCasesEST.forEach(testDateParse);
241
testCasesEDT.forEach(testDateParse);
242
testCasesCST.forEach(testDateParse);
243
testCasesCDT.forEach(testDateParse);
244
testCasesMST.forEach(testDateParse);
245
testCasesMDT.forEach(testDateParse);
246
testCasesPST.forEach(testDateParse);
247
testCasesPDT.forEach(testDateParse);
248
testCasesLocalTime.forEach(testDateParseLocalTime);
249
testCasesMisc.forEach(testDateParseMisc);
250

    
251

    
252
// Test that we can parse our own date format.
253
// (Dates from 1970 to ~2070 with 95h steps.)
254
for (var i = 0; i < 24 * 365 * 100; i += 95) {
255
  var ms = i * (3600 * 1000);
256
  var s = (new Date(ms)).toString();
257
  assertEquals(ms, Date.parse(s), s);
258
}
259

    
260
// Negative tests.
261
var testCasesNegative = [
262
    'May 25 2008 1:30 (PM)) UTC',
263
    'May 25 2008 1:30( )AM (PM)',
264
    'May 25 2008 AAA (GMT)'];
265

    
266
testCasesNegative.forEach(function (s) {
267
    assertTrue(isNaN(Date.parse(s)), s + " is not NaN.");
268
});