-
Notifications
You must be signed in to change notification settings - Fork 9
/
rfc3339date_with_datejs-test.htm
399 lines (350 loc) · 14.6 KB
/
rfc3339date_with_datejs-test.htm
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>rfc3339date.js - Unit Tests</title>
<meta name="description" content="Adds ISO 8601 / RFC 3339 date parsing to the Javascript Date object"/>
<meta name="keywords" content="javascript,date,rfc2229,ISO8601"/>
<!-- START: for jsunittest -->
<script src="unit_test_assets/jsunittest.js" type="text/javascript"></script>
<link rel="stylesheet" href="unit_test_assets/unittest.css" type="text/css" />
<!-- END: for jsunittest -->
<!-- START: for datejs -->
<script src="unit_test_assets/libs/date.js" type="text/javascript"></script>
<!-- END: for datejs -->
<!-- START: for rfc3339date -->
<script src="rfc3339date.js" type="text/javascript"></script>
<!-- END: for jtab -->
</head>
<body>
<div id="content">
<div id="header">
<h1>rfc3339date - Javascript ISO 8601 / RFC 3339 date parsing</h1>
<p>
This file tests compatibility of <strong>rfc3339date.js</strong> with <a href="http://www.datejs.com/">datejs</a>.
</p>
</div>
<!-- Log output (one per Runner, via {testLog: "testlog"} option)-->
<div id="testlog"></div>
<!-- Put sample/test html here -->
<div id="sample">
</div>
</div>
<script type="text/javascript">
// <![CDATA[
new Test.Unit.Runner({
setup: function() {
},
teardown: function() {
},
testUnitTestFrameworkOperational: function() { with(this) {
assert(true);
}},
testFallbackOnStandardParse: function() { with(this) {
var sourceString = "Jul 8, 2005";
var actual = new Date(Date.parse( sourceString ));
var expected = new Date();
expected.setFullYear(2005);
expected.setMonth(7 - 1);
expected.setDate(8);
expected.setHours(0);
expected.setMinutes(0);
expected.setSeconds(0);
expected.setMilliseconds(0);
assertEqual( expected.toString(), actual.toString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testBasicUTC: function() { with(this) {
var sourceString = "2010-07-20T15:00:00Z";
var resultDate = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), resultDate.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testBasicLocaltime: function() { with(this) {
var sourceString = "2010-07-20T15:00:00";
var actual = Date.parse( sourceString );
var expected = new Date(2010, 7-1 , 20, 15, 0, 0, 0);
assertEqual( expected.toString(), actual.toString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testBasicLocaltimeWithZone: function() { with(this) {
var sourceString = "2010-07-20T15:00:00+08:00";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(7);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testAbbreviateZoneNoColon: function() { with(this) {
var sourceString = "2010-07-20T15:00:00+0800";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(7);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testAbbreviateZoneOnlyHours: function() { with(this) {
var sourceString = "2010-07-20T15:00:00+08";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(7);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testAbbreviateUTCNoPunctuation: function() { with(this) {
var sourceString = "20100720T150000Z";
var resultDate = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), resultDate.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testNotCaseSensitive: function() { with(this) {
var sourceString = "20100720t150000z";
var resultDate = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), resultDate.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testFractionalSeconds: function() { with(this) {
var sourceString = "2010-07-20T15:00:00.559Z";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(559);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testFractionalSecondsAlternateSeparator: function() { with(this) {
var sourceString = "2010-07-20T15:00:00,559Z";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(559);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testUTCOptionalMinutes: function() { with(this) {
var sourceString = "2010-07-20T15Z";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(0);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testUTCOptionalSeconds: function() { with(this) {
var sourceString = "2010-07-20T15:30Z";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setUTCFullYear(2010);
expected.setUTCMonth(7 - 1);
expected.setUTCDate(20);
expected.setUTCHours(15);
expected.setUTCMinutes(30);
expected.setUTCSeconds(0);
expected.setUTCMilliseconds(0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testLocaltimeOptionalMinutes: function() { with(this) {
var sourceString = "2010-07-20T15";
var actual = Date.parse( sourceString );
var expected = new Date(2010, 7-1 , 20, 15, 0, 0, 0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testLocaltimeOptionalSeconds: function() { with(this) {
var sourceString = "2010-07-20T1530";
var actual = Date.parse( sourceString );
var expected = new Date(2010, 7-1 , 20, 15, 30, 0, 0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "incorrect conversion of: '" + sourceString + "'" );
}},
testBasicFormatUTC: function() { with(this) {
var expected = "2010-07-20T15:00:00Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(0);
assertEqual( expected, actual.toRFC3339UTCString(), "incorrect toRFC3339UTCString format" );
}},
testFormatUTCSupressFormating: function() { with(this) {
var expected = "20100720T150000Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(0);
assertEqual( expected, actual.toRFC3339UTCString( true ), "incorrect toRFC3339UTCString format" );
}},
testFormatUTCForceFormating: function() { with(this) {
var expected = "2010-07-20T15:00:00Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(0);
assertEqual( expected, actual.toRFC3339UTCString( false ), "incorrect toRFC3339UTCString format" );
}},
testFormatUTCSupressFormatingAndMillis: function() { with(this) {
var expected = "20100720T150000Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(333);
assertEqual( expected, actual.toRFC3339UTCString( true, true ), "incorrect toRFC3339UTCString format" );
}},
testFormatUTCForceFormatingAndMillis: function() { with(this) {
var expected = "2010-07-20T15:00:00.333Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(333);
assertEqual( expected, actual.toRFC3339UTCString( false, false ), "incorrect toRFC3339UTCString format" );
}},
testFormatUTCSupressFormatingAndForceMillis: function() { with(this) {
var expected = "20100720T150000.333Z";
var actual = new Date();
actual.setUTCFullYear(2010);
actual.setUTCMonth(7 - 1);
actual.setUTCDate(20);
actual.setUTCHours(15);
actual.setUTCMinutes(0);
actual.setUTCSeconds(0);
actual.setUTCMilliseconds(333);
assertEqual( expected, actual.toRFC3339UTCString( true, false ), "incorrect toRFC3339UTCString format" );
}},
testBasicFormatLocal: function() { with(this) {
// it is a bit tricky to test this in a way that works in any time zone,
// so we are testing that parsing the formated date should get you back the original date
var expected = new Date(2010, 7-1 , 20, 15, 0, 0, 0);
var resultLocal = expected.toRFC3339LocaleString();
var resultReconstituted = Date.parse( resultLocal );
assertEqual( expected.toUTCString(), resultReconstituted.toUTCString(), "incorrect toRFC3339LocaleString format" );
}},
testDatejsMonthAndYearFormat: function() { with(this) {
var sourceString = "July 2008";
var actual = Date.parse( sourceString );
var expected = new Date(2008, 7-1 , 1, 0, 0, 0, 0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "datejs parse method probably not invoked correctly to convert: '" + sourceString + "'" );
}},
testDatejsVerboseFormat: function() { with(this) {
var sourceString = "February 20th 1973";
var actual = Date.parse( sourceString );
var expected = new Date(1973, 2-1 , 20, 0, 0, 0, 0);
assertEqual( expected.toUTCString(), actual.toUTCString(), "datejs parse method probably not invoked correctly to convert: '" + sourceString + "'" );
}},
testDatejsRelativeFormat: function() { with(this) {
var sourceString = "t + 5 d";
var actual = Date.parse( sourceString );
var expected = new Date();
expected.setHours(0);expected.setMinutes(0);expected.setSeconds(0);expected.setMilliseconds(0);
expected.setTime( expected.getTime() + 5 * 24 * 60 * 60 * 1000 );
assertEqual( expected.toUTCString(), actual.toUTCString(), "datejs parse method probably not invoked correctly to convert: '" + sourceString + "'" );
}},
testDayOutOfBounds: function() { with(this){
/* case:
* if today to 31st, the month to be parsed
* doesnt have the 31st day, it rolls over to the next month
*/
//ref old date
var _Date = window.Date;
//date mock
window.Date = function(){
if (arguments.length){
//pass arguments through
return new _Date(arguments);
} else {
//force today to be...
return new _Date(2012, 0, 31, 15, 0, 0, 0);
}
}
Date.parseRFC3339 = _Date.parseRFC3339;
Date.parse = _Date.parse;
//sourceString's month doesnt have a 31st
var sourceString = "2012-04-31T23:30:00Z";
var actual = Date.parse( sourceString );
var expected = new Date(0);
var year = 2012;
var mon = 4;
var day = 1;
var hour = 23;
var mins = 30;
var secs = 0;
var millis = 0;
//set utc
expected.setUTCFullYear(year);
expected.setUTCMonth(mon);
expected.setUTCDate(day);
expected.setUTCHours(hour);
expected.setUTCMinutes(mins);
expected.setUTCSeconds(secs);
expected.setUTCMilliseconds(millis);
assertEqual( expected.toUTCString(), actual.toUTCString(), "datejs parse method probably not invoked correctly to convert: '" + sourceString + "'" );
window.Date = _Date;
}}
});
// ]]>
</script>
</body>
</html>