summaryrefslogtreecommitdiffstats
path: root/tests/auto/qscriptv8testsuite/tests/date-parse.js
blob: 0430a26bd1d9dd8aaa44b407eb1a6b9801488d0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2008 Google Inc. All Rights Reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Test that we can parse dates in all the different formats that we
// have to support.
//
// These formats are all supported by KJS but a lot of them are not
// supported by Spidermonkey.

function testDateParse(string) {
  var d = Date.parse(string);
  assertEquals(946713600000, d, string);
};


// For local time we just test that parsing returns non-NaN positive
// number of milliseconds to make it timezone independent.
function testDateParseLocalTime(string) {
  var d = Date.parse(string);
  assertTrue(d > 0 && !isNaN(d));
};


function testDateParseMisc(array) {
  assertTrue(array.length == 2);
  var string = array[0];
  var expected = array[1];
  var d = Date.parse(string);
  assertEquals(expected, d, string);
}


//
// Test all the formats in UT timezone.
//
var testCasesUT = [
    'Sat, 01-Jan-2000 08:00:00 UT',
    'Sat, 01 Jan 2000 08:00:00 UT',
    'Jan 01 2000 08:00:00 UT',
    'Jan 01 08:00:00 UT 2000',
    'Saturday, 01-Jan-00 08:00:00 UT',
    '01 Jan 00 08:00 +0000',
    // Ignore weekdays.
    'Mon, 01 Jan 2000 08:00:00 UT',
    'Tue, 01 Jan 2000 08:00:00 UT',
    // Ignore prefix that is not part of a date.
    '[Saturday] Jan 01 08:00:00 UT 2000',
    'Ignore all of this stuff because it is annoying 01 Jan 2000 08:00:00 UT',
    '[Saturday] Jan 01 2000 08:00:00 UT',
    'All of this stuff is really annnoying, so it will be ignored Jan 01 2000 08:00:00 UT',
    // If the three first letters of the month is a
    // month name we are happy - ignore the rest.
    'Sat, 01-Janisamonth-2000 08:00:00 UT',
    'Sat, 01 Janisamonth 2000 08:00:00 UT',
    'Janisamonth 01 2000 08:00:00 UT',
    'Janisamonth 01 08:00:00 UT 2000',
    'Saturday, 01-Janisamonth-00 08:00:00 UT',
    '01 Janisamonth 00 08:00 +0000',
    // Allow missing space between month and day.
    'Janisamonthandtherestisignored01 2000 08:00:00 UT',
    'Jan01 2000 08:00:00 UT',
    // Allow year/month/day format.
    'Sat, 2000/01/01 08:00:00 UT',
    // Allow month/day/year format.
    'Sat, 01/01/2000 08:00:00 UT',
    // Allow month/day year format.
    'Sat, 01/01 2000 08:00:00 UT',
    // Allow comma instead of space after day, month and year.
    'Sat, 01,Jan,2000,08:00:00 UT',
    // Seconds are optional.
    'Sat, 01-Jan-2000 08:00 UT',
    'Sat, 01 Jan 2000 08:00 UT',
    'Jan 01 2000 08:00 UT',
    'Jan 01 08:00 UT 2000',
    'Saturday, 01-Jan-00 08:00 UT',
    '01 Jan 00 08:00 +0000',
    // Allow AM/PM after the time.
    'Sat, 01-Jan-2000 08:00 AM UT',
    'Sat, 01 Jan 2000 08:00 AM UT',
    'Jan 01 2000 08:00 AM UT',
    'Jan 01 08:00 AM UT 2000',
    'Saturday, 01-Jan-00 08:00 AM UT',
    '01 Jan 00 08:00 AM +0000',
    // White space and stuff in parenthesis is
    // apparently allowed in most places where white
    // space is allowed.
    '   Sat,   01-Jan-2000   08:00:00   UT  ',
    '  Sat,   01   Jan   2000   08:00:00   UT  ',
    '  Saturday,   01-Jan-00   08:00:00   UT  ',
    '  01    Jan   00    08:00   +0000   ',
    ' ()(Sat, 01-Jan-2000)  Sat,   01-Jan-2000   08:00:00   UT  ',
    '  Sat()(Sat, 01-Jan-2000)01   Jan   2000   08:00:00   UT  ',
    '  Sat,(02)01   Jan   2000   08:00:00   UT  ',
    '  Sat,  01(02)Jan   2000   08:00:00   UT  ',
    '  Sat,  01  Jan  2000 (2001)08:00:00   UT  ',
    '  Sat,  01  Jan  2000 (01)08:00:00   UT  ',
    '  Sat,  01  Jan  2000 (01:00:00)08:00:00   UT  ',
    '  Sat,  01  Jan  2000  08:00:00 (CDT)UT  ',
    '  Sat,  01  Jan  2000  08:00:00  UT((((CDT))))',
    '  Saturday,   01-Jan-00 ()(((asfd)))(Sat, 01-Jan-2000)08:00:00   UT  ',
    '  01    Jan   00    08:00 ()(((asdf)))(Sat, 01-Jan-2000)+0000   ',
    '  01    Jan   00    08:00   +0000()((asfd)(Sat, 01-Jan-2000)) '];

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

// GMT = UT
var testCasesGMT = [
    'Sat, 01-Jan-2000 08:00:00 GMT',
    'Sat, 01-Jan-2000 08:00:00 GMT+0',
    'Sat, 01-Jan-2000 08:00:00 GMT+00',
    'Sat, 01-Jan-2000 08:00:00 GMT+000',
    'Sat, 01-Jan-2000 08:00:00 GMT+0000',
    'Sat, 01-Jan-2000 08:00:00 GMT+00:00', // Interestingly, KJS cannot handle this.
    'Sat, 01 Jan 2000 08:00:00 GMT',
    'Saturday, 01-Jan-00 08:00:00 GMT',
    '01 Jan 00 08:00 -0000',
    '01 Jan 00 08:00 +0000'];

// EST = UT minus 5 hours.
var testCasesEST = [
    'Sat, 01-Jan-2000 03:00:00 UTC-0500',
    'Sat, 01-Jan-2000 03:00:00 UTC-05:00', // Interestingly, KJS cannot handle this.
    'Sat, 01-Jan-2000 03:00:00 EST',
    'Sat, 01 Jan 2000 03:00:00 EST',
    'Saturday, 01-Jan-00 03:00:00 EST',
    '01 Jan 00 03:00 -0500'];

// EDT = UT minus 4 hours.
var testCasesEDT = [
    'Sat, 01-Jan-2000 04:00:00 EDT',
    'Sat, 01 Jan 2000 04:00:00 EDT',
    'Saturday, 01-Jan-00 04:00:00 EDT',
    '01 Jan 00 04:00 -0400'];

// CST = UT minus 6 hours.
var testCasesCST = [
    'Sat, 01-Jan-2000 02:00:00 CST',
    'Sat, 01 Jan 2000 02:00:00 CST',
    'Saturday, 01-Jan-00 02:00:00 CST',
    '01 Jan 00 02:00 -0600'];

// CDT = UT minus 5 hours.
var testCasesCDT = [
    'Sat, 01-Jan-2000 03:00:00 CDT',
    'Sat, 01 Jan 2000 03:00:00 CDT',
    'Saturday, 01-Jan-00 03:00:00 CDT',
    '01 Jan 00 03:00 -0500'];

// MST = UT minus 7 hours.
var testCasesMST = [
    'Sat, 01-Jan-2000 01:00:00 MST',
    'Sat, 01 Jan 2000 01:00:00 MST',
    'Saturday, 01-Jan-00 01:00:00 MST',
    '01 Jan 00 01:00 -0700'];

// MDT = UT minus 6 hours.
var testCasesMDT = [
    'Sat, 01-Jan-2000 02:00:00 MDT',
    'Sat, 01 Jan 2000 02:00:00 MDT',
    'Saturday, 01-Jan-00 02:00:00 MDT',
    '01 Jan 00 02:00 -0600'];

// PST = UT minus 8 hours.
var testCasesPST = [
    'Sat, 01-Jan-2000 00:00:00 PST',
    'Sat, 01 Jan 2000 00:00:00 PST',
    'Saturday, 01-Jan-00 00:00:00 PST',
    '01 Jan 00 00:00 -0800',
    // Allow missing time.
    'Sat, 01-Jan-2000 PST'];

// PDT = UT minus 7 hours.
var testCasesPDT = [
    'Sat, 01-Jan-2000 01:00:00 PDT',
    'Sat, 01 Jan 2000 01:00:00 PDT',
    'Saturday, 01-Jan-00 01:00:00 PDT',
    '01 Jan 00 01:00 -0700'];


// Local time cases.
var testCasesLocalTime = [
    // Allow timezone ommision.
    'Sat, 01-Jan-2000 08:00:00',
    'Sat, 01 Jan 2000 08:00:00',
    'Jan 01 2000 08:00:00',
    'Jan 01 08:00:00 2000',
    'Saturday, 01-Jan-00 08:00:00',
    '01 Jan 00 08:00'];


// Misc. test cases that result in a different time value.
var testCasesMisc = [
    // Special handling for years in the [0, 100) range.
    ['Sat, 01 Jan 0 08:00:00 UT', 946713600000], // year 2000
    ['Sat, 01 Jan 49 08:00:00 UT', 2493100800000], // year 2049
    ['Sat, 01 Jan 50 08:00:00 UT', -631123200000], // year 1950
    ['Sat, 01 Jan 99 08:00:00 UT', 915177600000], // year 1999
    ['Sat, 01 Jan 100 08:00:00 UT', -59011430400000], // year 100
    // Test PM after time.
    ['Sat, 01-Jan-2000 08:00 PM UT', 946756800000],
    ['Sat, 01 Jan 2000 08:00 PM UT', 946756800000],
    ['Jan 01 2000 08:00 PM UT', 946756800000],
    ['Jan 01 08:00 PM UT 2000', 946756800000],
    ['Saturday, 01-Jan-00 08:00 PM UT', 946756800000],
    ['01 Jan 00 08:00 PM +0000', 946756800000]];


// Run all the tests.
testCasesUT.forEach(testDateParse);
testCasesGMT.forEach(testDateParse);
testCasesEST.forEach(testDateParse);
testCasesEDT.forEach(testDateParse);
testCasesCST.forEach(testDateParse);
testCasesCDT.forEach(testDateParse);
testCasesMST.forEach(testDateParse);
testCasesMDT.forEach(testDateParse);
testCasesPST.forEach(testDateParse);
testCasesPDT.forEach(testDateParse);
testCasesLocalTime.forEach(testDateParseLocalTime);
testCasesMisc.forEach(testDateParseMisc);


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

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

testCasesNegative.forEach(function (s) { assertTrue(isNaN(Date.parse(s))); });