date-constructor 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,401 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class TestDateStrftime < Test::Unit::TestCase
5
+
6
+ STRFTIME_2001_02_03 = {
7
+ '%A'=>['Saturday',{:wday=>6}],
8
+ '%a'=>['Sat',{:wday=>6}],
9
+ '%B'=>['February',{:mon=>2}],
10
+ '%b'=>['Feb',{:mon=>2}],
11
+ '%c'=>['Sat Feb 3 00:00:00 2001',
12
+ {:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}],
13
+ '%d'=>['03',{:mday=>3}],
14
+ '%e'=>[' 3',{:mday=>3}],
15
+ '%H'=>['00',{:hour=>0}],
16
+ '%I'=>['12',{:hour=>0}],
17
+ '%j'=>['034',{:yday=>34}],
18
+ '%M'=>['00',{:min=>0}],
19
+ '%m'=>['02',{:mon=>2}],
20
+ '%p'=>['AM',{}],
21
+ '%S'=>['00',{:sec=>0}],
22
+ '%U'=>['04',{:wnum0=>4}],
23
+ '%W'=>['05',{:wnum1=>5}],
24
+ '%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
25
+ '%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
26
+ '%Y'=>['2001',{:year=>2001}],
27
+ '%y'=>['01',{:year=>2001}],
28
+ '%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
29
+ '%%'=>['%',{}],
30
+ '%C'=>['20',{}],
31
+ '%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
32
+ '%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}],
33
+ '%G'=>['2001',{:cwyear=>2001}],
34
+ '%g'=>['01',{:cwyear=>2001}],
35
+ '%h'=>['Feb',{:mon=>2}],
36
+ '%k'=>[' 0',{:hour=>0}],
37
+ '%L'=>['000',{:sec_fraction=>0}],
38
+ '%l'=>['12',{:hour=>0}],
39
+ '%N'=>['000000000',{:sec_fraction=>0}],
40
+ '%n'=>["\n",{}],
41
+ '%P'=>['am',{}],
42
+ '%Q'=>['981158400000',{:seconds=>981158400.to_r}],
43
+ '%R'=>['00:00',{:hour=>0,:min=>0}],
44
+ '%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}],
45
+ '%s'=>['981158400',{:seconds=>981158400}],
46
+ '%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
47
+ '%t'=>["\t",{}],
48
+ '%u'=>['6',{:cwday=>6}],
49
+ '%V'=>['05',{:cweek=>5}],
50
+ '%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}],
51
+ '%z'=>['+0000',{:zone=>'+0000',:offset=>0}],
52
+ '%+'=>['Sat Feb 3 00:00:00 +00:00 2001',
53
+ {:wday=>6,:mon=>2,:mday=>3,
54
+ :hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}],
55
+ }
56
+
57
+ STRFTIME_2001_02_03_CVS19 = {
58
+ }
59
+
60
+ STRFTIME_2001_02_03_GNUext = {
61
+ '%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
62
+ '%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}],
63
+ '%:::z'=>['+00',{:zone=>'+00',:offset=>0}],
64
+ }
65
+
66
+ STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19)
67
+ STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext)
68
+
69
+ def test_strftime
70
+ d = Date.new(2001,2,3)
71
+ STRFTIME_2001_02_03.each do |f, s|
72
+ assert_equal(s[0], d.strftime(f), [f, s].inspect)
73
+ case f[-1,1]
74
+ when 'c', 'C', 'x', 'X', 'y', 'Y'
75
+ f2 = f.sub(/\A%/, '%E')
76
+ assert_equal(s[0], d.strftime(f2), [f2, s].inspect)
77
+ else
78
+ f2 = f.sub(/\A%/, '%E')
79
+ assert_equal(f2, d.strftime(f2), [f2, s].inspect)
80
+ end
81
+ case f[-1,1]
82
+ when 'd', 'e', 'H', 'I', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y'
83
+ f2 = f.sub(/\A%/, '%O')
84
+ assert_equal(s[0], d.strftime(f2), [f2, s].inspect)
85
+ else
86
+ f2 = f.sub(/\A%/, '%O')
87
+ assert_equal(f2, d.strftime(f2), [f2, s].inspect)
88
+ end
89
+ end
90
+ end
91
+
92
+ def test_strftime__2
93
+ d = Date.new(2001,2,3)
94
+ assert_equal('2001-02-03', d.strftime)
95
+
96
+ d = DateTime.new(2001,2,3)
97
+ assert_equal('2001-02-03T00:00:00+00:00', d.strftime)
98
+
99
+ assert_equal('', d.strftime(''))
100
+ assert_equal("\s"*3, d.strftime("\s"*3))
101
+ assert_equal("\tfoo\n\000\r", d.strftime("\tfoo\n\000\r"))
102
+ assert_equal("%\n", d.strftime("%\n")) # gnu
103
+ assert_equal('Saturday'*1024 + ',', d.strftime('%A'*1024 + ','))
104
+ assert_equal('%%', d.strftime('%%%'))
105
+ assert_equal('Anton von Webern', d.strftime('Anton von Webern'))
106
+
107
+ d = DateTime.new(2001,2,3, 1,2,3)
108
+ assert_equal('2001-02-03T01:02:03+00:00', d.strftime)
109
+ assert_equal('AM', d.strftime('%p'))
110
+ assert_equal('am', d.strftime('%P'))
111
+ d = DateTime.new(2001,2,3, 13,14,15)
112
+ assert_equal('2001-02-03T13:14:15+00:00', d.strftime)
113
+ assert_equal('PM', d.strftime('%p'))
114
+ assert_equal('pm', d.strftime('%P'))
115
+ end
116
+
117
+ def test_strftime__3_1
118
+ (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d|
119
+ t = Time.utc(d.year,d.mon,d.mday)
120
+ assert_equal(t.strftime('%U'), d.strftime('%U'))
121
+ assert_equal(t.strftime('%W'), d.strftime('%W'))
122
+ end
123
+ end
124
+
125
+ def test_strftime__3_2
126
+ s = Time.now.strftime('%G')
127
+ skip if s.empty? || s == '%G'
128
+ (Date.new(1970,1,1)..Date.new(2037,12,31)).each do |d|
129
+ t = Time.utc(d.year,d.mon,d.mday)
130
+ assert_equal(t.strftime('%G'), d.strftime('%G'))
131
+ assert_equal(t.strftime('%g'), d.strftime('%g'))
132
+ assert_equal(t.strftime('%V'), d.strftime('%V'))
133
+ assert_equal(t.strftime('%u'), d.strftime('%u'))
134
+ end
135
+ end
136
+
137
+ def test_strftime__4
138
+ s = '2006-08-08T23:15:33.123456789'
139
+ f = '%FT%T.%N'
140
+ d = DateTime.parse(s)
141
+ assert_equal(s, d.strftime(f))
142
+ d = DateTime.strptime(s, f)
143
+ assert_equal(s, d.strftime(f))
144
+
145
+ s = '2006-08-08T23:15:33.123456789'
146
+ f = '%FT%T.%N'
147
+ d = DateTime.parse(s + '123456789')
148
+ assert_equal(s, d.strftime(f))
149
+ d = DateTime.strptime(s + '123456789', f)
150
+ assert_equal(s, d.strftime(f))
151
+
152
+ si = '2006-08-08T23:15:33.9'
153
+ so = '2006-08-08T23:15:33.900000000'
154
+ f = '%FT%T.%N'
155
+ d = DateTime.parse(si)
156
+ assert_equal(so, d.strftime(f))
157
+ d = DateTime.strptime(si, f)
158
+ assert_equal(so, d.strftime(f))
159
+
160
+ s = '2006-08-08T23:15:33.123'
161
+ f = '%FT%T.%L'
162
+ d = DateTime.parse(s)
163
+ assert_equal(s, d.strftime(f))
164
+ d = DateTime.strptime(s, f)
165
+ assert_equal(s, d.strftime(f))
166
+
167
+ s = '2006-08-08T23:15:33.123'
168
+ f = '%FT%T.%L'
169
+ d = DateTime.parse(s + '123')
170
+ assert_equal(s, d.strftime(f))
171
+ d = DateTime.strptime(s + '123', f)
172
+ assert_equal(s, d.strftime(f))
173
+
174
+ si = '2006-08-08T23:15:33.9'
175
+ so = '2006-08-08T23:15:33.900'
176
+ f = '%FT%T.%L'
177
+ d = DateTime.parse(si)
178
+ assert_equal(so, d.strftime(f))
179
+ d = DateTime.strptime(si, f)
180
+ assert_equal(so, d.strftime(f))
181
+ end
182
+
183
+ def test_strftime__offset
184
+ s = '2006-08-08T23:15:33'
185
+ (-24..24).collect{|x| '%+.2d' % x}.each do |hh|
186
+ %w(00 30).each do |mm|
187
+ r = hh + mm
188
+ if r[-4,4] == '2430'
189
+ r = '+0000'
190
+ end
191
+ d = DateTime.parse(s + hh + mm)
192
+ assert_equal(r, d.strftime('%z'))
193
+ end
194
+ end
195
+ end
196
+
197
+ def test_strftime_milli
198
+ s = '1970-01-01T00:00:00.123456789'
199
+ d = DateTime.parse(s)
200
+ assert_equal('123', d.strftime('%Q'))
201
+ s = '1970-01-02T02:03:04.123456789'
202
+ d = DateTime.parse(s)
203
+ assert_equal('93784123', d.strftime('%Q'))
204
+ end
205
+
206
+ def test_strftime__minus
207
+ d = DateTime.new(1969, 12, 31, 23, 59, 59)
208
+ assert_equal('-1', d.strftime('%s'))
209
+ assert_equal('-1000', d.strftime('%Q'))
210
+ end
211
+
212
+ def test_strftime__gnuext # coreutils
213
+ d = DateTime.new(2006,8,8,23,15,33,9.to_r/24)
214
+
215
+ assert_equal('2006', d.strftime('%-Y'))
216
+ assert_equal('2006', d.strftime('%-5Y'))
217
+ assert_equal('02006', d.strftime('%5Y'))
218
+ assert_equal('2006', d.strftime('%_Y'))
219
+ assert_equal(' 2006', d.strftime('%_5Y'))
220
+ assert_equal('02006', d.strftime('%05Y'))
221
+
222
+ assert_equal('8', d.strftime('%-d'))
223
+ assert_equal('8', d.strftime('%-3d'))
224
+ assert_equal('008', d.strftime('%3d'))
225
+ assert_equal(' 8', d.strftime('%_d'))
226
+ assert_equal(' 8', d.strftime('%_3d'))
227
+ assert_equal('008', d.strftime('%03d'))
228
+
229
+ assert_equal('8', d.strftime('%-e'))
230
+ assert_equal('8', d.strftime('%-3e'))
231
+ assert_equal(' 8', d.strftime('%3e'))
232
+ assert_equal(' 8', d.strftime('%_e'))
233
+ assert_equal(' 8', d.strftime('%_3e'))
234
+ assert_equal('008', d.strftime('%03e'))
235
+
236
+ assert_equal('Tuesday', d.strftime('%-10A'))
237
+ assert_equal(' Tuesday', d.strftime('%10A'))
238
+ assert_equal(' Tuesday', d.strftime('%_10A'))
239
+ assert_equal('000Tuesday', d.strftime('%010A'))
240
+ assert_equal('TUESDAY', d.strftime('%^A'))
241
+ assert_equal('TUESDAY', d.strftime('%#A'))
242
+
243
+ assert_equal('Tue', d.strftime('%-6a'))
244
+ assert_equal(' Tue', d.strftime('%6a'))
245
+ assert_equal(' Tue', d.strftime('%_6a'))
246
+ assert_equal('000Tue', d.strftime('%06a'))
247
+ assert_equal('TUE', d.strftime('%^a'))
248
+ assert_equal('TUE', d.strftime('%#a'))
249
+ assert_equal(' TUE', d.strftime('%#6a'))
250
+
251
+ assert_equal('August', d.strftime('%-10B'))
252
+ assert_equal(' August', d.strftime('%10B'))
253
+ assert_equal(' August', d.strftime('%_10B'))
254
+ assert_equal('0000August', d.strftime('%010B'))
255
+ assert_equal('AUGUST', d.strftime('%^B'))
256
+ assert_equal('AUGUST', d.strftime('%#B'))
257
+
258
+ assert_equal('Aug', d.strftime('%-6b'))
259
+ assert_equal(' Aug', d.strftime('%6b'))
260
+ assert_equal(' Aug', d.strftime('%_6b'))
261
+ assert_equal('000Aug', d.strftime('%06b'))
262
+ assert_equal('AUG', d.strftime('%^b'))
263
+ assert_equal('AUG', d.strftime('%#b'))
264
+ assert_equal(' AUG', d.strftime('%#6b'))
265
+
266
+ assert_equal('Aug', d.strftime('%-6h'))
267
+ assert_equal(' Aug', d.strftime('%6h'))
268
+ assert_equal(' Aug', d.strftime('%_6h'))
269
+ assert_equal('000Aug', d.strftime('%06h'))
270
+ assert_equal('AUG', d.strftime('%^h'))
271
+ assert_equal('AUG', d.strftime('%#h'))
272
+ assert_equal(' AUG', d.strftime('%#6h'))
273
+
274
+ assert_equal('PM', d.strftime('%^p'))
275
+ assert_equal('pm', d.strftime('%#p'))
276
+ assert_equal('PM', d.strftime('%^P'))
277
+ assert_equal('PM', d.strftime('%#P'))
278
+
279
+ assert_equal('+000900', d.strftime('%7z'))
280
+ assert_equal(' +900', d.strftime('%_7z'))
281
+ assert_equal('+09:00', d.strftime('%:z'))
282
+ assert_equal('+0009:00', d.strftime('%8:z'))
283
+ assert_equal(' +9:00', d.strftime('%_8:z'))
284
+ assert_equal('+09:00:00', d.strftime('%::z'))
285
+ assert_equal('+0009:00:00', d.strftime('%11::z'))
286
+ assert_equal(' +9:00:00', d.strftime('%_11::z'))
287
+ assert_equal('+09', d.strftime('%:::z'))
288
+ assert_equal('+0009', d.strftime('%5:::z'))
289
+ assert_equal(' +9', d.strftime('%_5:::z'))
290
+ assert_equal('+9', d.strftime('%-:::z'))
291
+
292
+ d = DateTime.new(-200,8,8,23,15,33,9.to_r/24)
293
+
294
+ assert_equal('-0200', d.strftime('%Y'))
295
+ assert_equal('-200', d.strftime('%-Y'))
296
+ assert_equal('-200', d.strftime('%-5Y'))
297
+ assert_equal('-0200', d.strftime('%5Y'))
298
+ assert_equal(' -200', d.strftime('%_Y'))
299
+ assert_equal(' -200', d.strftime('%_5Y'))
300
+ assert_equal('-0200', d.strftime('%05Y'))
301
+
302
+ d = DateTime.new(-2000,8,8,23,15,33,9.to_r/24)
303
+
304
+ assert_equal('-2000', d.strftime('%Y'))
305
+ assert_equal('-2000', d.strftime('%-Y'))
306
+ assert_equal('-2000', d.strftime('%-5Y'))
307
+ assert_equal('-2000', d.strftime('%5Y'))
308
+ assert_equal('-2000', d.strftime('%_Y'))
309
+ assert_equal('-2000', d.strftime('%_5Y'))
310
+ assert_equal('-2000', d.strftime('%05Y'))
311
+ end
312
+
313
+ def test_strftime__gnuext_LN # coreutils
314
+ d = DateTime.parse('2008-11-25T00:11:22.0123456789')
315
+ assert_equal('012', d.strftime('%L'))
316
+ assert_equal('012', d.strftime('%0L'))
317
+ assert_equal('0', d.strftime('%1L'))
318
+ assert_equal('01', d.strftime('%2L'))
319
+ assert_equal('01234567890', d.strftime('%11L'))
320
+ assert_equal('01234567890', d.strftime('%011L'))
321
+ assert_equal('01234567890', d.strftime('%_11L'))
322
+ assert_equal('012345678', d.strftime('%N'))
323
+ assert_equal('012345678', d.strftime('%0N'))
324
+ assert_equal('0', d.strftime('%1N'))
325
+ assert_equal('01', d.strftime('%2N'))
326
+ assert_equal('01234567890', d.strftime('%11N'))
327
+ assert_equal('01234567890', d.strftime('%011N'))
328
+ assert_equal('01234567890', d.strftime('%_11N'))
329
+ end
330
+
331
+ def test_strftime__gnuext_z # coreutils
332
+ d = DateTime.parse('2006-08-08T23:15:33+09:08:07')
333
+ assert_equal('+0908', d.strftime('%z'))
334
+ assert_equal('+09:08', d.strftime('%:z'))
335
+ assert_equal('+09:08:07', d.strftime('%::z'))
336
+ assert_equal('+09:08:07', d.strftime('%:::z'))
337
+ end
338
+
339
+ def test__different_format
340
+ d = Date.new(2001,2,3)
341
+
342
+ assert_equal('Sat Feb 3 00:00:00 2001', d.ctime)
343
+ assert_equal(d.ctime, d.asctime)
344
+
345
+ assert_equal('2001-02-03', d.iso8601)
346
+ assert_equal(d.xmlschema, d.iso8601)
347
+ assert_equal('2001-02-03T00:00:00+00:00', d.rfc3339)
348
+ assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822)
349
+ assert_equal(d.rfc822, d.rfc2822)
350
+ assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate)
351
+ assert_equal('H13.02.03', d.jisx0301)
352
+
353
+ d = DateTime.new(2001,2,3)
354
+
355
+ assert_equal('Sat Feb 3 00:00:00 2001', d.ctime)
356
+ assert_equal(d.ctime, d.asctime)
357
+
358
+ assert_equal('2001-02-03T00:00:00+00:00', d.iso8601)
359
+ assert_equal(d.rfc3339, d.iso8601)
360
+ assert_equal(d.xmlschema, d.iso8601)
361
+ assert_equal('Sat, 3 Feb 2001 00:00:00 +0000', d.rfc2822)
362
+ assert_equal(d.rfc822, d.rfc2822)
363
+ assert_equal('Sat, 03 Feb 2001 00:00:00 GMT', d.httpdate)
364
+ assert_equal('H13.02.03T00:00:00+00:00', d.jisx0301)
365
+
366
+ d2 = DateTime.parse('2001-02-03T04:05:06.123456')
367
+ assert_equal('2001-02-03T04:05:06.123+00:00', d2.iso8601(3))
368
+ assert_equal('2001-02-03T04:05:06.123+00:00', d2.rfc3339(3))
369
+ assert_equal('H13.02.03T04:05:06.123+00:00', d2.jisx0301(3))
370
+ assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.iso8601(9))
371
+ assert_equal('2001-02-03T04:05:06.123456000+00:00', d2.rfc3339(9))
372
+ assert_equal('H13.02.03T04:05:06.123456000+00:00', d2.jisx0301(9))
373
+
374
+ assert_equal('1800-01-01T00:00:00+00:00', DateTime.new(1800).jisx0301)
375
+
376
+ assert_equal('1868-01-25', Date.parse('1868-01-25').jisx0301)
377
+ assert_equal('1872-12-31', Date.parse('1872-12-31').jisx0301)
378
+
379
+ assert_equal('M06.01.01', Date.parse('1873-01-01').jisx0301)
380
+ assert_equal('M45.07.29', Date.parse('1912-07-29').jisx0301)
381
+ assert_equal('T01.07.30', Date.parse('1912-07-30').jisx0301)
382
+ assert_equal('T15.12.24', Date.parse('1926-12-24').jisx0301)
383
+ assert_equal('S01.12.25', Date.parse('1926-12-25').jisx0301)
384
+ assert_equal('S64.01.07', Date.parse('1989-01-07').jisx0301)
385
+ assert_equal('H01.01.08', Date.parse('1989-01-08').jisx0301)
386
+ assert_equal('H18.09.01', Date.parse('2006-09-01').jisx0301)
387
+
388
+ %w(M06.01.01
389
+ M45.07.29
390
+ T01.07.30
391
+ T15.12.24
392
+ S01.12.25
393
+ S64.01.07
394
+ H01.01.08
395
+ H18.09.01).each do |s|
396
+ assert_equal(s, Date.parse(s).jisx0301)
397
+ end
398
+
399
+ end
400
+
401
+ end
@@ -0,0 +1,486 @@
1
+ require 'test/unit'
2
+ require 'date'
3
+
4
+ class TestDateStrptime < Test::Unit::TestCase
5
+
6
+ STRFTIME_2001_02_03 = {
7
+ '%A'=>['Saturday',{:wday=>6}],
8
+ '%a'=>['Sat',{:wday=>6}],
9
+ '%B'=>['February',{:mon=>2}],
10
+ '%b'=>['Feb',{:mon=>2}],
11
+ '%c'=>['Sat Feb 3 00:00:00 2001',
12
+ {:wday=>6,:mon=>2,:mday=>3,:hour=>0,:min=>0,:sec=>0,:year=>2001}],
13
+ '%d'=>['03',{:mday=>3}],
14
+ '%e'=>[' 3',{:mday=>3}],
15
+ '%H'=>['00',{:hour=>0}],
16
+ '%I'=>['12',{:hour=>0}],
17
+ '%j'=>['034',{:yday=>34}],
18
+ '%M'=>['00',{:min=>0}],
19
+ '%m'=>['02',{:mon=>2}],
20
+ '%p'=>['AM',{}],
21
+ '%S'=>['00',{:sec=>0}],
22
+ '%U'=>['04',{:wnum0=>4}],
23
+ '%W'=>['05',{:wnum1=>5}],
24
+ '%X'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
25
+ '%x'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
26
+ '%Y'=>['2001',{:year=>2001}],
27
+ '%y'=>['01',{:year=>2001}],
28
+ '%Z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
29
+ '%%'=>['%',{}],
30
+ '%C'=>['20',{}],
31
+ '%D'=>['02/03/01',{:mon=>2,:mday=>3,:year=>2001}],
32
+ '%F'=>['2001-02-03',{:year=>2001,:mon=>2,:mday=>3}],
33
+ '%G'=>['2001',{:cwyear=>2001}],
34
+ '%g'=>['01',{:cwyear=>2001}],
35
+ '%h'=>['Feb',{:mon=>2}],
36
+ '%k'=>[' 0',{:hour=>0}],
37
+ '%L'=>['000',{:sec_fraction=>0}],
38
+ '%l'=>['12',{:hour=>0}],
39
+ '%N'=>['000000000',{:sec_fraction=>0}],
40
+ '%n'=>["\n",{}],
41
+ '%P'=>['am',{}],
42
+ '%Q'=>['981158400000',{:seconds=>981158400.to_r}],
43
+ '%R'=>['00:00',{:hour=>0,:min=>0}],
44
+ '%r'=>['12:00:00 AM',{:hour=>0,:min=>0,:sec=>0}],
45
+ '%s'=>['981158400',{:seconds=>981158400}],
46
+ '%T'=>['00:00:00',{:hour=>0,:min=>0,:sec=>0}],
47
+ '%t'=>["\t",{}],
48
+ '%u'=>['6',{:cwday=>6}],
49
+ '%V'=>['05',{:cweek=>5}],
50
+ '%v'=>[' 3-Feb-2001',{:mday=>3,:mon=>2,:year=>2001}],
51
+ '%z'=>['+0000',{:zone=>'+0000',:offset=>0}],
52
+ '%+'=>['Sat Feb 3 00:00:00 +00:00 2001',
53
+ {:wday=>6,:mon=>2,:mday=>3,
54
+ :hour=>0,:min=>0,:sec=>0,:zone=>'+00:00',:offset=>0,:year=>2001}],
55
+ }
56
+
57
+ STRFTIME_2001_02_03_CVS19 = {
58
+ }
59
+
60
+ STRFTIME_2001_02_03_GNUext = {
61
+ '%:z'=>['+00:00',{:zone=>'+00:00',:offset=>0}],
62
+ '%::z'=>['+00:00:00',{:zone=>'+00:00:00',:offset=>0}],
63
+ '%:::z'=>['+00',{:zone=>'+00',:offset=>0}],
64
+ }
65
+
66
+ STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_CVS19)
67
+ STRFTIME_2001_02_03.update(STRFTIME_2001_02_03_GNUext)
68
+
69
+ def test__strptime
70
+ STRFTIME_2001_02_03.each do |f, s|
71
+ if (f == '%I' and s[0] == '12') or
72
+ (f == '%l' and s[0] == '12') # hour w/o merid
73
+ s[1][:hour] = 12
74
+ end
75
+ assert_equal(s[1], Date._strptime(s[0], f), [f, s].inspect)
76
+ case f[-1,1]
77
+ when 'c', 'C', 'x', 'X', 'y', 'Y'
78
+ f2 = f.sub(/\A%/, '%E')
79
+ assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect)
80
+ else
81
+ f2 = f.sub(/\A%/, '%E')
82
+ assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect)
83
+ assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect)
84
+ end
85
+ case f[-1,1]
86
+ when 'd', 'e', 'H', 'I', 'm', 'M', 'S', 'u', 'U', 'V', 'w', 'W', 'y'
87
+ f2 = f.sub(/\A%/, '%O')
88
+ assert_equal(s[1], Date._strptime(s[0], f2), [f2, s].inspect)
89
+ else
90
+ f2 = f.sub(/\A%/, '%O')
91
+ assert_equal(nil, Date._strptime(s[0], f2), [f2, s].inspect)
92
+ assert_equal({}, Date._strptime(f2, f2), [f2, s].inspect)
93
+ end
94
+ end
95
+ end
96
+
97
+ def test__strptime__2
98
+ h = Date._strptime('2001-02-03')
99
+ assert_equal([2001,2,3], h.values_at(:year,:mon,:mday))
100
+
101
+ h = DateTime._strptime('2001-02-03T12:13:14Z')
102
+ assert_equal([2001,2,3,12,13,14],
103
+ h.values_at(:year,:mon,:mday,:hour,:min,:sec))
104
+
105
+ assert_equal({}, Date._strptime('', ''))
106
+ assert_equal({:leftover=>"\s"*3}, Date._strptime("\s"*3, ''))
107
+ assert_equal({:leftover=>'x'}, Date._strptime("\nx", "\n"))
108
+ assert_equal({}, Date._strptime('', "\s"*3))
109
+ assert_equal({}, Date._strptime("\s"*3, "\s"*3))
110
+ assert_equal({}, Date._strptime("\tfoo\n\000\r", "\tfoo\n\000\r"))
111
+ assert_equal({}, Date._strptime("foo\n\nbar", "foo\sbar"))
112
+ assert_equal({}, Date._strptime("%\n", "%\n")) # gnu
113
+ assert_equal({}, Date._strptime('%%', '%%%'))
114
+ assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%A'*1024 + ','))
115
+ assert_equal({:wday=>6}, Date._strptime('Saturday'*1024 + ',', '%a'*1024 + ','))
116
+ assert_equal({}, Date._strptime('Anton von Webern', 'Anton von Webern'))
117
+ end
118
+
119
+ def test__strptime__3
120
+ [
121
+ # iso8601
122
+ [['2001-02-03', '%Y-%m-%d'], [2001,2,3,nil,nil,nil,nil,nil,nil]],
123
+ [['2001-02-03T23:59:60', '%Y-%m-%dT%H:%M:%S'], [2001,2,3,23,59,60,nil,nil,nil]],
124
+ [['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]],
125
+ [['-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]],
126
+ [['+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]],
127
+ [['-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]],
128
+
129
+ # ctime(3), asctime(3)
130
+ [['Thu Jul 29 14:47:19 1999', '%c'], [1999,7,29,14,47,19,nil,nil,4]],
131
+ [['Thu Jul 29 14:47:19 -1999', '%c'], [-1999,7,29,14,47,19,nil,nil,4]],
132
+
133
+ # date(1)
134
+ [['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]],
135
+ [['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]],
136
+ [['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]],
137
+ [['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]],
138
+ [['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]],
139
+ [['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]],
140
+ [['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]],
141
+ [['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]],
142
+ [['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]],
143
+ [['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]],
144
+ [['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]],
145
+ [['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]],
146
+ [['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]],
147
+ [['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]],
148
+
149
+ # rfc822
150
+ [['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]],
151
+ [['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]],
152
+ [['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]],
153
+ [['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]],
154
+ [['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]],
155
+ [['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]],
156
+ [['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]],
157
+ [['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]],
158
+
159
+ # etc
160
+ [['06-DEC-99', '%d-%b-%y'], [1999,12,6,nil,nil,nil,nil,nil,nil]],
161
+ [['sUnDay oCtoBer 31 01', '%A %B %d %y'], [2001,10,31,nil,nil,nil,nil,nil,0]],
162
+ [["October\t\n\v\f\r 15,\t\n\v\f\r99", '%B %d, %y'], [1999,10,15,nil,nil,nil,nil,nil,nil]],
163
+ [["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]],
164
+
165
+ [['09:02:11 AM', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil]],
166
+ [['09:02:11 A.M.', '%I:%M:%S %p'], [nil,nil,nil,9,2,11,nil,nil,nil]],
167
+ [['09:02:11 PM', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil]],
168
+ [['09:02:11 P.M.', '%I:%M:%S %p'], [nil,nil,nil,21,2,11,nil,nil,nil]],
169
+
170
+ [['12:33:44 AM', '%r'], [nil,nil,nil,0,33,44,nil,nil,nil]],
171
+ [['01:33:44 AM', '%r'], [nil,nil,nil,1,33,44,nil,nil,nil]],
172
+ [['11:33:44 AM', '%r'], [nil,nil,nil,11,33,44,nil,nil,nil]],
173
+ [['12:33:44 PM', '%r'], [nil,nil,nil,12,33,44,nil,nil,nil]],
174
+ [['01:33:44 PM', '%r'], [nil,nil,nil,13,33,44,nil,nil,nil]],
175
+ [['11:33:44 PM', '%r'], [nil,nil,nil,23,33,44,nil,nil,nil]],
176
+
177
+ [['11:33:44 PM AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil]],
178
+ [['11:33:44 P.M. AMT', '%I:%M:%S %p %Z'], [nil,nil,nil,23,33,44,'AMT',nil,nil]],
179
+
180
+ [['fri1feb034pm+5', '%a%d%b%y%H%p%Z'], [2003,2,1,16,nil,nil,'+5',5*3600,5]]
181
+ ].each do |x, y|
182
+ h = Date._strptime(*x)
183
+ a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday)
184
+ if y[1] == -1
185
+ a[1] = -1
186
+ a[2] = h[:yday]
187
+ end
188
+ assert_equal(y, a, [x, y, a].inspect)
189
+ end
190
+ end
191
+
192
+ def test__strptime__width
193
+ [
194
+ [['99', '%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]],
195
+ [['01', '%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]],
196
+ [['19 99', '%C %y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]],
197
+ [['20 01', '%C %y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]],
198
+ [['1999', '%C%y'], [1999,nil,nil,nil,nil,nil,nil,nil,nil]],
199
+ [['2001', '%C%y'], [2001,nil,nil,nil,nil,nil,nil,nil,nil]],
200
+
201
+ [['20060806', '%Y'], [20060806,nil,nil,nil,nil,nil,nil,nil,nil]],
202
+ [['20060806', "%Y\s"], [20060806,nil,nil,nil,nil,nil,nil,nil,nil]],
203
+ [['20060806', '%Y%m%d'], [2006,8,6,nil,nil,nil,nil,nil,nil]],
204
+ [['2006908906', '%Y9%m9%d'], [2006,8,6,nil,nil,nil,nil,nil,nil]],
205
+ [['12006 08 06', '%Y %m %d'], [12006,8,6,nil,nil,nil,nil,nil,nil]],
206
+ [['12006-08-06', '%Y-%m-%d'], [12006,8,6,nil,nil,nil,nil,nil,nil]],
207
+ [['200608 6', '%Y%m%e'], [2006,8,6,nil,nil,nil,nil,nil,nil]],
208
+
209
+ [['2006333', '%Y%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil]],
210
+ [['20069333', '%Y9%j'], [2006,-1,333,nil,nil,nil,nil,nil,nil]],
211
+ [['12006 333', '%Y %j'], [12006,-1,333,nil,nil,nil,nil,nil,nil]],
212
+ [['12006-333', '%Y-%j'], [12006,-1,333,nil,nil,nil,nil,nil,nil]],
213
+
214
+ [['232425', '%H%M%S'], [nil,nil,nil,23,24,25,nil,nil,nil]],
215
+ [['23924925', '%H9%M9%S'], [nil,nil,nil,23,24,25,nil,nil,nil]],
216
+ [['23 24 25', '%H %M %S'], [nil,nil,nil,23,24,25,nil,nil,nil]],
217
+ [['23:24:25', '%H:%M:%S'], [nil,nil,nil,23,24,25,nil,nil,nil]],
218
+ [[' 32425', '%k%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil]],
219
+ [[' 32425', '%l%M%S'], [nil,nil,nil,3,24,25,nil,nil,nil]],
220
+
221
+ [['FriAug', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5]],
222
+ [['FriAug', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5]],
223
+ [['FridayAugust', '%A%B'], [nil,8,nil,nil,nil,nil,nil,nil,5]],
224
+ [['FridayAugust', '%a%b'], [nil,8,nil,nil,nil,nil,nil,nil,5]]
225
+ ].each do |x, y|
226
+ h = Date._strptime(*x)
227
+ a = h.values_at(:year,:mon,:mday,:hour,:min,:sec,:zone,:offset,:wday)
228
+ if y[1] == -1
229
+ a[1] = -1
230
+ a[2] = h[:yday]
231
+ end
232
+ assert_equal(y, a, [x, y, a].inspect)
233
+ end
234
+ end
235
+
236
+ def test__strptime__fail
237
+ assert_not_nil(Date._strptime('2001.', '%Y.'))
238
+ assert_not_nil(Date._strptime("2001.\s", '%Y.'))
239
+ assert_not_nil(Date._strptime('2001.', "%Y.\s"))
240
+ assert_not_nil(Date._strptime("2001.\s", "%Y.\s"))
241
+
242
+ assert_nil(Date._strptime('2001', '%Y.'))
243
+ assert_nil(Date._strptime("2001\s", '%Y.'))
244
+ assert_nil(Date._strptime('2001', "%Y.\s"))
245
+ assert_nil(Date._strptime("2001\s", "%Y.\s"))
246
+
247
+ assert_nil(Date._strptime('2001-13-31', '%Y-%m-%d'))
248
+ assert_nil(Date._strptime('2001-12-00', '%Y-%m-%d'))
249
+ assert_nil(Date._strptime('2001-12-32', '%Y-%m-%d'))
250
+ assert_nil(Date._strptime('2001-12-00', '%Y-%m-%e'))
251
+ assert_nil(Date._strptime('2001-12-32', '%Y-%m-%e'))
252
+ assert_nil(Date._strptime('2001-12-31', '%y-%m-%d'))
253
+
254
+ assert_nil(Date._strptime('2004-000', '%Y-%j'))
255
+ assert_nil(Date._strptime('2004-367', '%Y-%j'))
256
+ assert_nil(Date._strptime('2004-366', '%y-%j'))
257
+
258
+ assert_not_nil(Date._strptime('24:59:59', '%H:%M:%S'))
259
+ assert_not_nil(Date._strptime('24:59:59', '%k:%M:%S'))
260
+ assert_not_nil(Date._strptime('24:59:60', '%H:%M:%S'))
261
+ assert_not_nil(Date._strptime('24:59:60', '%k:%M:%S'))
262
+
263
+ assert_nil(Date._strptime('24:60:59', '%H:%M:%S'))
264
+ assert_nil(Date._strptime('24:60:59', '%k:%M:%S'))
265
+ assert_nil(Date._strptime('24:59:61', '%H:%M:%S'))
266
+ assert_nil(Date._strptime('24:59:61', '%k:%M:%S'))
267
+ assert_nil(Date._strptime('00:59:59', '%I:%M:%S'))
268
+ assert_nil(Date._strptime('13:59:59', '%I:%M:%S'))
269
+ assert_nil(Date._strptime('00:59:59', '%l:%M:%S'))
270
+ assert_nil(Date._strptime('13:59:59', '%l:%M:%S'))
271
+
272
+ assert_not_nil(Date._strptime('0', '%U'))
273
+ assert_nil(Date._strptime('54', '%U'))
274
+ assert_not_nil(Date._strptime('0', '%W'))
275
+ assert_nil(Date._strptime('54', '%W'))
276
+ assert_nil(Date._strptime('0', '%V'))
277
+ assert_nil(Date._strptime('54', '%V'))
278
+ assert_nil(Date._strptime('0', '%u'))
279
+ assert_not_nil(Date._strptime('7', '%u'))
280
+ assert_not_nil(Date._strptime('0', '%w'))
281
+ assert_nil(Date._strptime('7', '%w'))
282
+
283
+ assert_nil(Date._strptime('Sanday', '%A'))
284
+ assert_nil(Date._strptime('Jenuary', '%B'))
285
+ assert_not_nil(Date._strptime('Sundai', '%A'))
286
+ assert_not_nil(Date._strptime('Januari', '%B'))
287
+ assert_nil(Date._strptime('Sundai,', '%A,'))
288
+ assert_nil(Date._strptime('Januari,', '%B,'))
289
+ end
290
+
291
+ def test_strptime
292
+ assert_equal(Date.new, Date.strptime)
293
+ d = Date.new(2002,3,14)
294
+ assert_equal(d, Date.strptime(d.to_s))
295
+ assert_equal(Date.new(2002,3,14), Date.strptime('2002-03-14'))
296
+
297
+ d = DateTime.new(2002,3,14,11,22,33, 0)
298
+ assert_equal(d, DateTime.strptime(d.to_s))
299
+ assert_equal(DateTime.new(2002,3,14,11,22,33, 0),
300
+ DateTime.strptime('2002-03-14T11:22:33Z'))
301
+ assert_equal(DateTime.new(2002,3,14,11,22,33, 0),
302
+ DateTime.strptime('2002-03-14T11:22:33Z', '%Y-%m-%dT%H:%M:%S%Z'))
303
+ assert_equal(DateTime.new(2002,3,14,11,22,33, 9.to_r/24),
304
+ DateTime.strptime('2002-03-14T11:22:33+09:00', '%Y-%m-%dT%H:%M:%S%Z'))
305
+ assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24),
306
+ DateTime.strptime('2002-03-14T11:22:33-09:00', '%FT%T%Z'))
307
+ assert_equal(DateTime.new(2002,3,14,11,22,33, -9.to_r/24) + 123456789.to_r/1000000000/86400,
308
+ DateTime.strptime('2002-03-14T11:22:33.123456789-09:00', '%FT%T.%N%Z'))
309
+ end
310
+
311
+ def test_strptime__2
312
+ n = 10**9
313
+ (Date.new(2006,6,1)..Date.new(2007,6,1)).each do |d|
314
+ [
315
+ '%Y %m %d',
316
+ '%C %y %m %d',
317
+
318
+ '%Y %j',
319
+ '%C %y %j',
320
+
321
+ '%G %V %w',
322
+ '%G %V %u',
323
+ '%C %g %V %w',
324
+ '%C %g %V %u',
325
+
326
+ '%Y %U %w',
327
+ '%Y %U %u',
328
+ '%Y %W %w',
329
+ '%Y %W %u',
330
+ '%C %y %U %w',
331
+ '%C %y %U %u',
332
+ '%C %y %W %w',
333
+ '%C %y %W %u',
334
+ ].each do |fmt|
335
+ s = d.strftime(fmt)
336
+ d2 = Date.strptime(s, fmt)
337
+ assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect)
338
+ end
339
+
340
+ [
341
+ '%Y %m %d %H %M %S',
342
+ '%Y %m %d %H %M %S %N',
343
+ '%C %y %m %d %H %M %S',
344
+ '%C %y %m %d %H %M %S %N',
345
+
346
+ '%Y %j %H %M %S',
347
+ '%Y %j %H %M %S %N',
348
+ '%C %y %j %H %M %S',
349
+ '%C %y %j %H %M %S %N',
350
+
351
+ '%s',
352
+ '%s %N',
353
+ '%Q',
354
+ '%Q %N',
355
+ ].each do |fmt|
356
+ s = d.strftime(fmt)
357
+ d2 = DateTime.strptime(s, fmt)
358
+ assert_equal(d, d2, [fmt, d.to_s, d2.to_s].inspect)
359
+ end
360
+ end
361
+ end
362
+
363
+ def test_strptime__minus
364
+ d = DateTime.strptime('-1', '%s')
365
+ assert_equal([1969, 12, 31, 23, 59, 59],
366
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
367
+ d = DateTime.strptime('-86400', '%s')
368
+ assert_equal([1969, 12, 31, 0, 0, 0],
369
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
370
+
371
+ d = DateTime.strptime('-999', '%Q')
372
+ assert_equal([1969, 12, 31, 23, 59, 59, 1.to_r/10**3],
373
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
374
+ d = DateTime.strptime('-1000', '%Q')
375
+ assert_equal([1969, 12, 31, 23, 59, 59, 0],
376
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec, d.sec_fraction])
377
+ end
378
+
379
+ def test_strptime__comp
380
+ n = DateTime.now
381
+
382
+ d = DateTime.strptime('073', '%j')
383
+ assert_equal([n.year, 73, 0, 0, 0],
384
+ [d.year, d.yday, d.hour, d.min, d.sec])
385
+ d = DateTime.strptime('13', '%d')
386
+ assert_equal([n.year, n.mon, 13, 0, 0, 0],
387
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
388
+
389
+ d = DateTime.strptime('Mar', '%b')
390
+ assert_equal([n.year, 3, 1, 0, 0, 0],
391
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
392
+ d = DateTime.strptime('2004', '%Y')
393
+ assert_equal([2004, 1, 1, 0, 0, 0],
394
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
395
+
396
+ d = DateTime.strptime('Mar 13', '%b %d')
397
+ assert_equal([n.year, 3, 13, 0, 0, 0],
398
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
399
+ d = DateTime.strptime('Mar 2004', '%b %Y')
400
+ assert_equal([2004, 3, 1, 0, 0, 0],
401
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
402
+ d = DateTime.strptime('23:55', '%H:%M')
403
+ assert_equal([n.year, n.mon, n.mday, 23, 55, 0],
404
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
405
+ d = DateTime.strptime('23:55:30', '%H:%M:%S')
406
+ assert_equal([n.year, n.mon, n.mday, 23, 55, 30],
407
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
408
+
409
+ d = DateTime.strptime('Sun 23:55', '%a %H:%M')
410
+ d2 = d - d.wday
411
+ assert_equal([d2.year, d2.mon, d2.mday, 23, 55, 0],
412
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
413
+ d = DateTime.strptime('Aug 23:55', '%b %H:%M')
414
+ assert_equal([n.year, 8, 1, 23, 55, 0],
415
+ [d.year, d.mon, d.mday, d.hour, d.min, d.sec])
416
+
417
+ d = DateTime.strptime('2004', '%G')
418
+ assert_equal([2004, 1, 1, 0, 0, 0],
419
+ [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
420
+ d = DateTime.strptime('11', '%V')
421
+ assert_equal([n.cwyear, 11, 1, 0, 0, 0],
422
+ [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
423
+ d = DateTime.strptime('6', '%u')
424
+ assert_equal([n.cwyear, n.cweek, 6, 0, 0, 0],
425
+ [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
426
+
427
+ d = DateTime.strptime('11-6', '%V-%u')
428
+ assert_equal([n.cwyear, 11, 6, 0, 0, 0],
429
+ [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
430
+ d = DateTime.strptime('2004-11', '%G-%V')
431
+ assert_equal([2004, 11, 1, 0, 0, 0],
432
+ [d.cwyear, d.cweek, d.cwday, d.hour, d.min, d.sec])
433
+
434
+ d = DateTime.strptime('11-6', '%U-%w')
435
+ assert_equal([n.year, 11, 6, 0, 0, 0],
436
+ [d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec])
437
+ d = DateTime.strptime('2004-11', '%Y-%U')
438
+ assert_equal([2004, 11, 0, 0, 0, 0],
439
+ [d.year, d.strftime('%U').to_i, d.wday, d.hour, d.min, d.sec])
440
+
441
+ d = DateTime.strptime('11-6', '%W-%w')
442
+ assert_equal([n.year, 11, 6, 0, 0, 0],
443
+ [d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec])
444
+ d = DateTime.strptime('2004-11', '%Y-%W')
445
+ assert_equal([2004, 11, 1, 0, 0, 0],
446
+ [d.year, d.strftime('%W').to_i, d.wday, d.hour, d.min, d.sec])
447
+ end
448
+
449
+ def test_strptime__d_to_s
450
+ d = Date.new(2002,3,14)
451
+ assert_equal(d, Date.strptime(d.to_s))
452
+
453
+ d = DateTime.new(2002,3,14,11,22,33, 9.to_r/24)
454
+ assert_equal(d, DateTime.strptime(d.to_s))
455
+ end
456
+
457
+ def test_strptime__ex
458
+ assert_raise(ArgumentError) do
459
+ Date.strptime('')
460
+ end
461
+ assert_raise(ArgumentError) do
462
+ DateTime.strptime('')
463
+ end
464
+ assert_raise(ArgumentError) do
465
+ Date.strptime('2001-02-29', '%F')
466
+ end
467
+ assert_raise(ArgumentError) do
468
+ DateTime.strptime('2001-02-29T23:59:60', '%FT%T')
469
+ end
470
+ assert_raise(ArgumentError) do
471
+ Date.strptime('23:55', '%H:%M')
472
+ end
473
+ assert_raise(ArgumentError) do
474
+ Date.strptime('01-31-2011', '%m/%d/%Y')
475
+ end
476
+ end
477
+
478
+ def test_given_string
479
+ s = '2001-02-03T04:05:06Z'
480
+ s0 = s.dup
481
+
482
+ assert_not_equal({}, Date._strptime(s, '%FT%T%Z'))
483
+ assert_equal(s0, s)
484
+ end
485
+
486
+ end