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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
|
require 'test/unit'
require 'date'
class TestDateStrptime < Test::Unit::TestCase
STRFTIME_2001_02_03 = {
'%A'=>['Saturday',{:wday=>6}],
'%a'=>['Sat',{:wday=>6}],
'%B'=>['February',{:mon=>2}],
'%b'=>['Feb',{:mon=>2}],
'%c'=>['Sat Feb 3 00:00:00 2001',
{:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}],
'%d'=>['03',{:mday=>3}],
'%e'=>[' 3',{:mday=>3}],
'%H'=>['00',{:hour=>0}],
'%I'=>['12',{:hour=>0}],
'%j'=>['034',{:yday=>34}],
'%M'=>['00',{:min=>0}],
'%m'=>['02',{:mon=>2}],
'%p'=>['AM',{}],
'%S'=>['00',{:sec=>0}],
'%U'=>['04',{:wnum0=>4}],
'%W'=>['05',{:wnum1=>5}],
'%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
'%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
'%Y'=>['2001',{:year=>2001}],
'%y'=>['01',{:year=>2001}],
'%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
'%%'=>['%',{}],
'%C'=>['20',{}],
'%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
'%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}],
'%G'=>['2001',{:cwyear=>2001}],
'%g'=>['01',{:cwyear=>2001}],
'%h'=>['Feb',{:mon=>2}],
'%k'=>[' 0',{:hour=>0}],
'%L'=>['000',{:sec_fraction=>0}],
'%l'=>['12',{:hour=>0}],
'%N'=>['000000000',{:sec_fraction=>0}],
'%n'=>["\n",{}],
'%P'=>['am',{}],
'%Q'=>['981158400000',{:seconds=>981158400.to_r}],
'%R'=>['00:00',{:hour=>0,:min=>0}],
'%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}],
'%s'=>['981158400',{:seconds=>981158400}],
'%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
'%t'=>["\t",{}],
'%u'=>['6',{:cwday=>6}],
'%V'=>['05',{:cweek=>5}],
'%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}],
'%z'=>['+0000',{:zone=>'+0000',:offset=>0}],
'%+'=>['Sat Feb 3 00:00:00 +00:00 2001',
{:wday=>6,:mon=>2,:mday=>3,
:hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}],
}
STRFTIME_2001_02_03_CVS19 = {
}
STRFTIME_2001_02_03_GNUext = {
'%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
'%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}],
'%:::z'=>['+00',{:zone=>'+00',:offset=>0}],
}
STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19)
STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext)
def test__strptime
STRFTIME_2001_02_03.each do |f, s|
if (f == '%I' and s[0] == '12') or
(f == '%l' and s[0] == '12') # hour w/o merid
s[1][:hour] = 12
end
assert_equal(s[1], Date._strptime(s[0], f), [f, s].inspect)
case f[-1,1]
when 'c', 'C', 'x', 'X', 'y', 'Y'
f2 = f.sub(/\A%/, '%E')
assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect)
else
f2 = f.sub(/\A%/, '%E')
assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect)
assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect)
end
case f[-1,1]
when 'd', 'e', 'H', 'I', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y'
f2 = f.sub(/\A%/, '%O')
assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect)
else
f2 = f.sub(/\A%/, '%O')
assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect)
assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect)
end
end
end
def test__strptime__2
h = Date._strptime('2001-02-03')
assert_equal([2001,2,3], h.values_at(:year,:mon,:mday))
h = DateTime._strptime('2001-02-03T12:13:14Z')
assert_equal([2001,2,3,12,13,14],
h.values_at(:year,:mon,:mday,:hour,:min,:sec))
assert_equal({}, Date._strptime('', ''))
assert_equal({:leftover=>"\s"*3}, Date._strptime("\s"*3, ''))
assert_equal({:leftover=>'x'}, Date._strptime("\nx", "\n"))
assert_equal({}, Date._strptime('', "\s"*3))
assert_equal({}, Date._strptime("\s"*3, "\s"*3))
assert_equal({}, Date._strptime("\tfoo\n\000\r", "\tfoo\n\000\r"))
assert_equal({}, Date._strptime("foo\n\nbar", "foo\sbar"))
assert_equal({}, Date._strptime("%\n", "%\n")) # gnu
assert_equal({}, Date._strptime('%%', '%%%'))
assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%A'*1024 + ','))
assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%a'*1024 + ','))
assert_equal({}, Date._strptime('Anton von Webern', 'Anton von Webern'))
end
def test__strptime__3
[
# iso8601
[['2001-02-03', '%Y-%m-%d'], [2001,2,3,nil,nil,nil,nil,nil,nil], __LINE__],
[['2001-02-03T23:59:60', '%Y-%m-%dT%H:%M:%S'], [2001,2,3,23,59,60,nil,nil,nil], __LINE__],
[['2001-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [2001,2,3,23,59,60,'+09:00',9*3600,nil], __LINE__],
[['-2001-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [-2001,2,3,23,59,60,'+09:00',9*3600,nil], __LINE__],
[['+012345-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [12345,2,3,23,59,60,'+09:00',9*3600,nil], __LINE__],
[['-012345-02-03T23:59:60+09:00', '%Y-%m-%dT%H:%M:%S%Z'], [-12345,2,3,23,59,60,'+09:00',9*3600,nil], __LINE__],
# ctime(3), asctime(3)
[['Thu Jul 29 14:47:19 1999', '%c'], [1999,7,29,14,47,19,nil,nil,4], __LINE__],
[['Thu Jul 29 14:47:19 -1999', '%c'], [-1999,7,29,14,47,19,nil,nil,4], __LINE__],
# date(1)
[['Thu Jul 29 16:39:41 EST 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'EST',-5*3600,4], __LINE__],
[['Thu Jul 29 16:39:41 MET DST 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'MET DST',2*3600,4], __LINE__],
[['Thu Jul 29 16:39:41 AMT 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'AMT',nil,4], __LINE__],
[['Thu Jul 29 16:39:41 AMT -1999', '%a %b %d %H:%M:%S %Z %Y'], [-1999,7,29,16,39,41,'AMT',nil,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT+09 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+09',9*3600,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT+0908 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+0908',9*3600+8*60,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT+090807 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT+090807',9*3600+8*60+7,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT-09 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09',-9*3600,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT-09:08 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09:08',-9*3600-8*60,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT-09:08:07 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-09:08:07',-9*3600-8*60-7,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT-3.5 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-3.5',-3*3600-30*60,4], __LINE__],
[['Thu Jul 29 16:39:41 GMT-3,5 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'GMT-3,5',-3*3600-30*60,4], __LINE__],
[['Thu Jul 29 16:39:41 Mountain Daylight Time 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'Mountain Daylight Time',-6*3600,4], __LINE__],
[['Thu Jul 29 16:39:41 E. Australia Standard Time 1999', '%a %b %d %H:%M:%S %Z %Y'], [1999,7,29,16,39,41,'E. Australia Standard Time',10*3600,4], __LINE__],
# rfc822
[['Thu, 29 Jul 1999 09:54:21 UT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'UT',0,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 GMT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'GMT',0,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 PDT', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'PDT',-7*3600,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 z', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'z',0,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 +0900', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'+0900',9*3600,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 +0430', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'+0430',4*3600+30*60,4], __LINE__],
[['Thu, 29 Jul 1999 09:54:21 -0430', '%a, %d %b %Y %H:%M:%S %Z'], [1999,7,29,9,54,21,'-0430',-4*3600-30*60,4], __LINE__],
[['Thu, 29 Jul -1999 09:54:21 -0430', '%a, %d %b %Y %H:%M:%S %Z'], [-1999,7,29,9,54,21,'-0430',-4*3600-30*60,4], __LINE__],
# etc
[['06-DEC-99', '%d-%b-%y'], [1999,12,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['sUnDay oCtoBer 31 01', '%A %B %d %y'], [2001,10,31,nil,nil,nil,nil,nil,0], __LINE__],
[["October\t\n\v\f\r 15,\t\n\v\f\r99", '%B %d, %y'], [1999,10,15,nil,nil,nil,nil,nil,nil], __LINE__],
[["October\t\n\v\f\r 15,\t\n\v\f\r99", '%B%t%d,%n%y'], [1999,10,15,nil,nil,nil,nil,nil,nil], __LINE__],
[['09:02:11 AM', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil], __LINE__],
[['09:02:11 A.M.', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil], __LINE__],
[['09:02:11 PM', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil], __LINE__],
[['09:02:11 P.M.', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil], __LINE__],
[['12:33:44 AM', '%r'], [nil,nil,nil,0,33,44,nil,nil,nil], __LINE__],
[['01:33:44 AM', '%r'], [nil,nil,nil,1,33,44,nil,nil,nil], __LINE__],
[['11:33:44 AM', '%r'], [nil,nil,nil,11,33,44,nil,nil,nil], __LINE__],
[['12:33:44 PM', '%r'], [nil,nil,nil,12,33,44,nil,nil,nil], __LINE__],
[['01:33:44 PM', '%r'], [nil,nil,nil,13,33,44,nil,nil,nil], __LINE__],
[['11:33:44 PM', '%r'], [nil,nil,nil,23,33,44,nil,nil,nil], __LINE__],
[['11:33:44 PM AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil], __LINE__],
[['11:33:44 P.M. AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil], __LINE__],
[['fri1feb034pm+5', '%a%d%b%y%H%p%Z'], [2003,2,1,16,nil,nil,'+5',5*3600,5]]
].each do |x, y|
h = Date._strptime(*x)
a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday)
if y[1] == -1
a[1] = -1
a[2] = h[:yday]
end
assert_equal(y, a, [x, y, a].inspect)
end
end
def test__strptime__width
[
[['99', '%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['01', '%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['19 99', '%C %y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['20 01', '%C %y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['30 99', '%C %y'], [3099,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['30 01', '%C %y'], [3001,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['1999', '%C%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['2001', '%C%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['3099', '%C%y'], [3099,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['3001', '%C%y'], [3001,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['20060806', '%Y'], [20060806,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['20060806', "%Y\s"], [20060806,nil,nil,nil,nil,nil,nil,nil,nil], __LINE__],
[['20060806', '%Y%m%d'], [2006,8,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['2006908906', '%Y9%m9%d'], [2006,8,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['12006 08 06', '%Y %m %d'], [12006,8,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['12006-08-06', '%Y-%m-%d'], [12006,8,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['200608 6', '%Y%m%e'], [2006,8,6,nil,nil,nil,nil,nil,nil], __LINE__],
[['2006333', '%Y%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil], __LINE__],
[['20069333', '%Y9%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil], __LINE__],
[['12006 333', '%Y %j'], [12006,-1,333,nil,nil,nil,nil,nil,nil], __LINE__],
[['12006-333', '%Y-%j'], [12006,-1,333,nil,nil,nil,nil,nil,nil], __LINE__],
[['232425', '%H%M%S'], [nil,nil,nil,23,24,25,nil,nil,nil], __LINE__],
[['23924925', '%H9%M9%S'], [nil,nil,nil,23,24,25,nil,nil,nil], __LINE__],
[['23 24 25', '%H %M %S'], [nil,nil,nil,23,24,25,nil,nil,nil], __LINE__],
[['23:24:25', '%H:%M:%S'], [nil,nil,nil,23,24,25,nil,nil,nil], __LINE__],
[[' 32425', '%k%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil], __LINE__],
[[' 32425', '%l%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil], __LINE__],
[['FriAug', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5], __LINE__],
[['FriAug', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5], __LINE__],
[['FridayAugust', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5], __LINE__],
[['FridayAugust', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5], __LINE__],
].each do |x,y,l|
h = Date._strptime(*x)
a = (h || {}).values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday)
if y[1] == -1
a[1] = -1
a[2] = h[:yday]
end
assert_equal(y, a, format('<failed at line %d>', l))
end
end
def test__strptime__fail
assert_not_nil(Date._strptime('2001.', '%Y.'))
assert_not_nil(Date._strptime("2001.\s", '%Y.'))
assert_not_nil(Date._strptime('2001.', "%Y.\s"))
assert_not_nil(Date._strptime("2001.\s", "%Y.\s"))
assert_nil(Date._strptime('2001', '%Y.'))
assert_nil(Date._strptime("2001\s", '%Y.'))
assert_nil(Date._strptime('2001', "%Y.\s"))
assert_nil(Date._strptime("2001\s", "%Y.\s"))
assert_nil(Date._strptime('2001-13-31', '%Y-%m-%d'))
assert_nil(Date._strptime('2001-12-00', '%Y-%m-%d'))
assert_nil(Date._strptime('2001-12-32', '%Y-%m-%d'))
assert_nil(Date._strptime('2001-12-00', '%Y-%m-%e'))
assert_nil(Date._strptime('2001-12-32', '%Y-%m-%e'))
assert_nil(Date._strptime('2001-12-31', '%y-%m-%d'))
assert_nil(Date._strptime('2004-000', '%Y-%j'))
assert_nil(Date._strptime('2004-367', '%Y-%j'))
assert_nil(Date._strptime('2004-366', '%y-%j'))
assert_not_nil(Date._strptime('24:59:59', '%H:%M:%S'))
assert_not_nil(Date._strptime('24:59:59', '%k:%M:%S'))
assert_not_nil(Date._strptime('24:59:60', '%H:%M:%S'))
assert_not_nil(Date._strptime('24:59:60', '%k:%M:%S'))
assert_nil(Date._strptime('24:60:59', '%H:%M:%S'))
assert_nil(Date._strptime('24:60:59', '%k:%M:%S'))
assert_nil(Date._strptime('24:59:61', '%H:%M:%S'))
assert_nil(Date._strptime('24:59:61', '%k:%M:%S'))
assert_nil(Date._strptime('00:59:59', '%I:%M:%S'))
assert_nil(Date._strptime('13:59:59', '%I:%M:%S'))
assert_nil(Date._strptime('00:59:59', '%l:%M:%S'))
assert_nil(Date._strptime('13:59:59', '%l:%M:%S'))
assert_not_nil(Date._strptime('0', '%U'))
assert_nil(Date._strptime('54', '%U'))
assert_not_nil(Date._strptime('0', '%W'))
assert_nil(Date._strptime('54', '%W'))
assert_nil(Date._strptime('0', '%V'))
assert_nil(Date._strptime('54', '%V'))
assert_nil(Date._strptime('0', '%u'))
assert_not_nil(Date._strptime('7', '%u'))
assert_not_nil(Date._strptime('0', '%w'))
assert_nil(Date._strptime('7', '%w'))
assert_nil(Date._strptime('Sanday', '%A'))
assert_nil(Date._strptime('Jenuary', '%B'))
assert_not_nil(Date._strptime('Sundai', '%A'))
assert_not_nil(Date._strptime('Januari', '%B'))
assert_nil(Date._strptime('Sundai,', '%A,'))
assert_nil(Date._strptime('Januari,', '%B,'))
end
def test_strptime
assert_equal(Date.new, Date.strptime)
d = Date.new(2002,3,14)
assert_equal(d, Date.strptime(d.to_s))
assert_equal(Date.new(2002,3,14), Date.strptime('2002-03-14'))
d = DateTime.new(2002,3,14,11,22,33, 0)
assert_equal(d, DateTime.strptime(d.to_s))
assert_equal(DateTime.new(2002,3,14,11,22,33, 0),
DateTime.strptime('2002-03-14T11:22:33Z'))
assert_equal(DateTime.new(2002,3,14,11,22,33, 0),
DateTime.strptime('2002-03-14T11:22:33Z', '%Y-%m-%dT%H:%M:%S%Z'))
assert_equal(DateTime.new(2002,3,14,11,22,33, 9.to_r/24),
DateTime.strptime('2002-03-14T11:22:33+09:00', '%Y-%m-%dT%H:%M:%S%Z'))
assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24),
DateTime.strptime('2002-03-14T11:22:33-09:00', '%FT%T%Z'))
assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24) + 123456789.to_r/1000000000/86400,
DateTime.strptime('2002-03-14T11:22:33.123456789-09:00', '%FT%T.%N%Z'))
end
def test_strptime__2
(Date.new(2006,6,1)..Date.new(2007,6,1)).each do |d|
[
'%Y %m %d',
'%C %y %m %d',
'%Y %j',
'%C %y %j',
'%G %V %w',
'%G %V %u',
'%C %g %V %w',
'%C %g %V %u',
'%Y %U %w',
'%Y %U %u',
'%Y %W %w',
'%Y %W %u',
'%C %y %U %w',
'%C %y %U %u',
'%C %y %W %w',
'%C %y %W %u',
].each do |fmt|
s = d.strftime(fmt)
d2 = Date.strptime(s, fmt)
assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect)
end
[
'%Y %m %d %H %M %S',
'%Y %m %d %H %M %S %N',
'%C %y %m %d %H %M %S',
'%C %y %m %d %H %M %S %N',
'%Y %j %H %M %S',
'%Y %j %H %M %S %N',
'%C %y %j %H %M %S',
'%C %y %j %H %M %S %N',
'%s',
'%s %N',
'%Q',
'%Q %N',
].each do |fmt|
s = d.strftime(fmt)
d2 = DateTime.strptime(s, fmt)
assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect)
end
end
end
def test_strptime__minus
d = DateTime.strptime('-1', '%s')
assert_equal([1969, 12, 31, 23, 59, 59],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('-86400', '%s')
assert_equal([1969, 12, 31, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('-999', '%Q')
assert_equal([1969, 12, 31, 23, 59, 59, 1.to_r/10**3],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
d = DateTime.strptime('-1000', '%Q')
assert_equal([1969, 12, 31, 23, 59, 59, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
end
def test_strptime__comp
n = DateTime.now
d = DateTime.strptime('073', '%j')
assert_equal([n.year, 73, 0, 0, 0],
[d.year, d.yday, d.hour, d.min, d.sec])
d = DateTime.strptime('13', '%d')
assert_equal([n.year, n.mon, 13, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('Mar', '%b')
assert_equal([n.year, 3, 1, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('2004', '%Y')
assert_equal([2004, 1, 1, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('Mar 13', '%b %d')
assert_equal([n.year, 3, 13, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('Mar 2004', '%b %Y')
assert_equal([2004, 3, 1, 0, 0, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('23:55', '%H:%M')
assert_equal([n.year, n.mon, n.mday, 23, 55, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('23:55:30', '%H:%M:%S')
assert_equal([n.year, n.mon, n.mday, 23, 55, 30],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('Sun 23:55', '%a %H:%M')
d2 = d - d.wday
assert_equal([d2.year, d2.mon, d2.mday, 23, 55, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('Aug 23:55', '%b %H:%M')
assert_equal([n.year, 8, 1, 23, 55, 0],
[d.year, d.mon, d.mday, d.hour, d.min, d.sec])
d = DateTime.strptime('2004', '%G')
assert_equal([2004, 1, 1, 0, 0, 0],
[d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
d = DateTime.strptime('11', '%V')
assert_equal([n.cwyear, 11, 1, 0, 0, 0],
[d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
d = DateTime.strptime('6', '%u')
assert_equal([n.cwyear, n.cweek, 6, 0, 0, 0],
[d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
d = DateTime.strptime('11-6', '%V-%u')
assert_equal([n.cwyear, 11, 6, 0, 0, 0],
[d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
d = DateTime.strptime('2004-11', '%G-%V')
assert_equal([2004, 11, 1, 0, 0, 0],
[d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
d = DateTime.strptime('11-6', '%U-%w')
assert_equal([n.year, 11, 6, 0, 0, 0],
[d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec])
d = DateTime.strptime('2004-11', '%Y-%U')
assert_equal([2004, 11, 0, 0, 0, 0],
[d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec])
d = DateTime.strptime('11-6', '%W-%w')
assert_equal([n.year, 11, 6, 0, 0, 0],
[d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec])
d = DateTime.strptime('2004-11', '%Y-%W')
assert_equal([2004, 11, 1, 0, 0, 0],
[d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec])
end
def test_strptime__d_to_s
d = Date.new(2002,3,14)
assert_equal(d, Date.strptime(d.to_s))
d = DateTime.new(2002,3,14,11,22,33, 9.to_r/24)
assert_equal(d, DateTime.strptime(d.to_s))
end
def test_strptime__ex
assert_raise(ArgumentError) do
Date.strptime('')
end
assert_raise(ArgumentError) do
DateTime.strptime('')
end
assert_raise(ArgumentError) do
Date.strptime('2001-02-29', '%F')
end
assert_raise(ArgumentError) do
DateTime.strptime('2001-02-29T23:59:60', '%FT%T')
end
assert_nothing_raised(ArgumentError) do
DateTime.strptime('2001-03-01T23:59:60', '%FT%T')
end
assert_raise(ArgumentError) do
DateTime.strptime('2001-03-01T23:59:61', '%FT%T')
end
assert_raise(ArgumentError) do
Date.strptime('23:55', '%H:%M')
end
assert_raise(ArgumentError) do
Date.strptime('01-31-2011', '%m/%d/%Y')
end
end
def test_given_string
s = '2001-02-03T04:05:06Z'
s0 = s.dup
assert_not_equal({}, Date._strptime(s, '%FT%T%Z'))
assert_equal(s0, s)
end
def test_sz
d = DateTime.strptime('0 -0200', '%s %z')
assert_equal([1969, 12, 31, 22, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
assert_equal(Rational(-2, 24), d.offset)
d = DateTime.strptime('9 +0200', '%s %z')
assert_equal([1970, 1, 1, 2, 0, 9], [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
assert_equal(Rational(2, 24), d.offset)
d = DateTime.strptime('0 -0200', '%Q %z')
assert_equal([1969, 12, 31, 22, 0, 0], [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
assert_equal(Rational(-2, 24), d.offset)
d = DateTime.strptime('9000 +0200', '%Q %z')
assert_equal([1970, 1, 1, 2, 0, 9], [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
assert_equal(Rational(2, 24), d.offset)
end
end
|