[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / test / test_time.rb
blob23e8e104a165fc859e48f185c8b4d97ac31c537d
1 # frozen_string_literal: true
3 require 'time'
4 require 'test/unit'
6 class TestTimeExtension < Test::Unit::TestCase # :nodoc:
7   def test_rfc822
8     t = Time.rfc2822("26 Aug 76 14:30 EDT")
9     assert_equal(Time.utc(1976, 8, 26, 14, 30) + 4 * 3600, t)
10     assert_equal(-4 * 3600, t.utc_offset)
11     t = Time.rfc2822("27 Aug 76 09:32 PDT")
12     assert_equal(Time.utc(1976, 8, 27, 9, 32) + 7 * 3600, t)
13     assert_equal(-7 * 3600, t.utc_offset)
14   end
16   def test_rfc2822
17     t = Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600")
18     assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, t)
19     assert_equal(-6 * 3600, t.utc_offset)
20     t = Time.rfc2822("Tue, 1 Jul 2003 10:52:37 +0200")
21     assert_equal(Time.utc(2003, 7, 1, 10, 52, 37) - 2 * 3600, t)
22     assert_equal(2 * 3600, t.utc_offset)
23     t = Time.rfc2822("Fri, 21 Nov 1997 10:01:10 -0600")
24     assert_equal(Time.utc(1997, 11, 21, 10, 1, 10) + 6 * 3600, t)
25     assert_equal(-6 * 3600, t.utc_offset)
26     t = Time.rfc2822("Fri, 21 Nov 1997 11:00:00 -0600")
27     assert_equal(Time.utc(1997, 11, 21, 11, 0, 0) + 6 * 3600, t)
28     assert_equal(-6 * 3600, t.utc_offset)
29     t = Time.rfc2822("Mon, 24 Nov 1997 14:22:01 -0800")
30     assert_equal(Time.utc(1997, 11, 24, 14, 22, 1) + 8 * 3600, t)
31     assert_equal(-8 * 3600, t.utc_offset)
32     begin
33       Time.at(-1)
34     rescue ArgumentError
35       # ignore
36     else
37       t = Time.rfc2822("Thu, 13 Feb 1969 23:32:54 -0330")
38       assert_equal(Time.utc(1969, 2, 13, 23, 32, 54) + 3 * 3600 + 30 * 60, t)
39       assert_equal(-3 * 3600 - 30 * 60, t.utc_offset)
40       t = Time.rfc2822(" Thu,
41       13
42         Feb
43           1969
44       23:32
45                -0330 (Newfoundland Time)")
46       assert_equal(Time.utc(1969, 2, 13, 23, 32, 0) + 3 * 3600 + 30 * 60, t)
47       assert_equal(-3 * 3600 - 30 * 60, t.utc_offset)
48     end
49     t = Time.rfc2822("21 Nov 97 09:55:06 GMT")
50     assert_equal(Time.utc(1997, 11, 21, 9, 55, 6), t)
51     assert_equal(0, t.utc_offset)
52     t = Time.rfc2822("Fri, 21 Nov 1997 09 :   55  :  06 -0600")
53     assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, t)
54     assert_equal(-6 * 3600, t.utc_offset)
55     assert_raise(ArgumentError) {
56       # inner comment is not supported.
57       Time.rfc2822("Fri, 21 Nov 1997 09(comment):   55  :  06 -0600")
58     }
59     t = Time.rfc2822("Mon, 01 Jan 0001 00:00:00 -0000")
60     assert_equal(Time.utc(1, 1, 1, 0, 0, 0), t)
61     assert_equal(0, t.utc_offset)
62     assert_equal(true, t.utc?)
63   end
65   def test_rfc2822_nonlinear
66     pre = ->(n) {"0 Feb 00 00 :00" + " " * n}
67     assert_linear_performance([100, 500, 5000, 50_000], pre: pre) do |s|
68       assert_raise(ArgumentError) do
69         Time.rfc2822(s)
70       end
71     end
72   end
74   if defined?(Ractor)
75     def test_rfc2822_ractor
76       assert_ractor(<<~RUBY, require: 'time')
77         actual = Ractor.new { Time.rfc2822("Fri, 21 Nov 1997 09:55:06 -0600") }.take
78         assert_equal(Time.utc(1997, 11, 21, 9, 55, 6) + 6 * 3600, actual)
79       RUBY
80     end
81   end
83   def test_encode_rfc2822
84     t = Time.utc(1)
85     assert_equal("Mon, 01 Jan 0001 00:00:00 -0000", t.rfc2822)
86   end
88   def test_rfc2616
89     t = Time.utc(1994, 11, 6, 8, 49, 37)
90     assert_equal(t, Time.httpdate("Sun, 06 Nov 1994 08:49:37 GMT"))
91     assert_equal(t, Time.httpdate("Sunday, 06-Nov-94 08:49:37 GMT"))
92     assert_equal(t, Time.httpdate("Sun Nov  6 08:49:37 1994"))
93     assert_equal(Time.utc(1995, 11, 15, 6, 25, 24),
94                  Time.httpdate("Wed, 15 Nov 1995 06:25:24 GMT"))
95     assert_equal(Time.utc(1995, 11, 15, 4, 58, 8),
96                  Time.httpdate("Wed, 15 Nov 1995 04:58:08 GMT"))
97     assert_equal(Time.utc(1994, 11, 15, 8, 12, 31),
98                  Time.httpdate("Tue, 15 Nov 1994 08:12:31 GMT"))
99     assert_equal(Time.utc(1994, 12, 1, 16, 0, 0),
100                  Time.httpdate("Thu, 01 Dec 1994 16:00:00 GMT"))
101     assert_equal(Time.utc(1994, 10, 29, 19, 43, 31),
102                  Time.httpdate("Sat, 29 Oct 1994 19:43:31 GMT"))
103     assert_equal(Time.utc(1994, 11, 15, 12, 45, 26),
104                  Time.httpdate("Tue, 15 Nov 1994 12:45:26 GMT"))
105     assert_equal(Time.utc(1999, 12, 31, 23, 59, 59),
106                  Time.httpdate("Fri, 31 Dec 1999 23:59:59 GMT"))
108     assert_equal(Time.utc(2007, 12, 23, 11, 22, 33),
109                  Time.httpdate('Sunday, 23-Dec-07 11:22:33 GMT'))
110   end
112   def test_encode_httpdate
113     t = Time.utc(1)
114     assert_equal("Mon, 01 Jan 0001 00:00:00 GMT", t.httpdate)
115   end
117   def subtest_xmlschema_alias(method)
118     t = Time.utc(1985, 4, 12, 23, 20, 50, 520000)
119     s = "1985-04-12T23:20:50.52Z"
120     assert_equal(t, Time.__send__(method, s))
121     assert_equal(s, t.__send__(method, 2))
123     t = Time.utc(1996, 12, 20, 0, 39, 57)
124     s = "1996-12-19T16:39:57-08:00"
125     assert_equal(t, Time.__send__(method, s))
126     assert_equal(t, Time.__send__(method, s.sub(/:(?=00\z)/, '')))
127     assert_equal(t, Time.__send__(method, s.sub(/:00\z/, '')))
128     # There is no way to generate time string with arbitrary timezone.
129     s = "1996-12-20T00:39:57Z"
130     assert_equal(t, Time.__send__(method, s))
131     assert_equal(s, t.iso8601)
133     t = Time.utc(1990, 12, 31, 23, 59, 60)
134     s = "1990-12-31T23:59:60Z"
135     assert_equal(t, Time.__send__(method, s))
136     # leap second is representable only if timezone file has it.
137     s = "1990-12-31T15:59:60-08:00"
138     assert_equal(t, Time.__send__(method, s))
140     begin
141       Time.at(-1)
142     rescue ArgumentError
143       # ignore
144     else
145       t = Time.utc(1937, 1, 1, 11, 40, 27, 870000)
146       s = "1937-01-01T12:00:27.87+00:20"
147       assert_equal(t, Time.__send__(method, s))
148     end
149   end
151   # http://www.w3.org/TR/xmlschema-2/
152   def subtest_xmlschema(method)
153     assert_equal(Time.utc(1999, 5, 31, 13, 20, 0) + 5 * 3600,
154                  Time.__send__(method, "1999-05-31T13:20:00-05:00"))
155     assert_equal(Time.local(2000, 1, 20, 12, 0, 0),
156                  Time.__send__(method, "2000-01-20T12:00:00"))
157     assert_equal(Time.utc(2000, 1, 20, 12, 0, 0),
158                  Time.__send__(method, "2000-01-20T12:00:00Z"))
159     assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) - 12 * 3600,
160                  Time.__send__(method, "2000-01-20T12:00:00+12:00"))
161     assert_equal(Time.utc(2000, 1, 20, 12, 0, 0) + 13 * 3600,
162                  Time.__send__(method, "2000-01-20T12:00:00-13:00"))
163     assert_equal(Time.utc(2000, 3, 4, 23, 0, 0) - 3 * 3600,
164                  Time.__send__(method, "2000-03-04T23:00:00+03:00"))
165     assert_equal(Time.utc(2000, 3, 4, 20, 0, 0),
166                  Time.__send__(method, "2000-03-04T20:00:00Z"))
167     assert_equal(Time.local(2000, 1, 15, 0, 0, 0),
168                  Time.__send__(method, "2000-01-15T00:00:00"))
169     assert_equal(Time.local(2000, 2, 15, 0, 0, 0),
170                  Time.__send__(method, "2000-02-15T00:00:00"))
171     assert_equal(Time.local(2000, 1, 15, 12, 0, 0),
172                  Time.__send__(method, "2000-01-15T12:00:00"))
173     assert_equal(Time.utc(2000, 1, 16, 12, 0, 0),
174                  Time.__send__(method, "2000-01-16T12:00:00Z"))
175     assert_equal(Time.local(2000, 1, 1, 12, 0, 0),
176                  Time.__send__(method, "2000-01-01T12:00:00"))
177     assert_equal(Time.utc(1999, 12, 31, 23, 0, 0),
178                  Time.__send__(method, "1999-12-31T23:00:00Z"))
179     assert_equal(Time.local(2000, 1, 16, 12, 0, 0),
180                  Time.__send__(method, "2000-01-16T12:00:00"))
181     assert_equal(Time.local(2000, 1, 16, 0, 0, 0),
182                  Time.__send__(method, "2000-01-16T00:00:00"))
183     assert_equal(Time.utc(2000, 1, 12, 12, 13, 14),
184                  Time.__send__(method, "2000-01-12T12:13:14Z"))
185     assert_equal(Time.utc(2001, 4, 17, 19, 23, 17, 300000),
186                  Time.__send__(method, "2001-04-17T19:23:17.3Z"))
187     assert_equal(Time.utc(2000, 1, 2, 0, 0, 0),
188                  Time.__send__(method, "2000-01-01T24:00:00Z"))
189     assert_raise(ArgumentError) { Time.__send__(method, "2000-01-01T00:00:00.+00:00") }
190   end
192   def subtest_xmlschema_encode(method)
193     bug6100 = '[ruby-core:42997]'
195     t = Time.utc(2001, 4, 17, 19, 23, 17, 300000)
196     assert_equal("2001-04-17T19:23:17Z", t.__send__(method))
197     assert_equal("2001-04-17T19:23:17.3Z", t.__send__(method, 1))
198     assert_equal("2001-04-17T19:23:17.300000Z", t.__send__(method, 6))
199     assert_equal("2001-04-17T19:23:17.3000000Z", t.__send__(method, 7))
200     assert_equal("2001-04-17T19:23:17.3Z", t.__send__(method, 1.9), bug6100)
202     t = Time.utc(2001, 4, 17, 19, 23, 17, 123456)
203     assert_equal("2001-04-17T19:23:17.1234560Z", t.__send__(method, 7))
204     assert_equal("2001-04-17T19:23:17.123456Z", t.__send__(method, 6))
205     assert_equal("2001-04-17T19:23:17.12345Z", t.__send__(method, 5))
206     assert_equal("2001-04-17T19:23:17.1Z", t.__send__(method, 1))
207     assert_equal("2001-04-17T19:23:17.1Z", t.__send__(method, 1.9), bug6100)
209     t = Time.at(2.quo(3)).getlocal("+09:00")
210     assert_equal("1970-01-01T09:00:00.666+09:00", t.__send__(method, 3))
211     assert_equal("1970-01-01T09:00:00.6666666666+09:00", t.__send__(method, 10))
212     assert_equal("1970-01-01T09:00:00.66666666666666666666+09:00", t.__send__(method, 20))
213     assert_equal("1970-01-01T09:00:00.6+09:00", t.__send__(method, 1.1), bug6100)
214     assert_equal("1970-01-01T09:00:00.666+09:00", t.__send__(method, 3.2), bug6100)
216     t = Time.at(123456789.quo(9999999999)).getlocal("+09:00")
217     assert_equal("1970-01-01T09:00:00.012+09:00", t.__send__(method, 3))
218     assert_equal("1970-01-01T09:00:00.012345678+09:00", t.__send__(method, 9))
219     assert_equal("1970-01-01T09:00:00.0123456789+09:00", t.__send__(method, 10))
220     assert_equal("1970-01-01T09:00:00.0123456789012345678+09:00", t.__send__(method, 19))
221     assert_equal("1970-01-01T09:00:00.01234567890123456789+09:00", t.__send__(method, 20))
222     assert_equal("1970-01-01T09:00:00.012+09:00", t.__send__(method, 3.8), bug6100)
224     t = Time.utc(1)
225     assert_equal("0001-01-01T00:00:00Z", t.__send__(method))
227     begin
228       Time.at(-1)
229     rescue ArgumentError
230       # ignore
231     else
232       t = Time.utc(1960, 12, 31, 23, 0, 0, 123456)
233       assert_equal("1960-12-31T23:00:00.123456Z", t.__send__(method, 6))
234     end
236     assert_equal(249, Time.__send__(method, "2008-06-05T23:49:23.000249+09:00").usec)
238     assert_equal("10000-01-01T00:00:00Z", Time.utc(10000).__send__(method))
239     assert_equal("9999-01-01T00:00:00Z", Time.utc(9999).__send__(method))
240     assert_equal("0001-01-01T00:00:00Z", Time.utc(1).__send__(method)) # 1 AD
241     assert_equal("0000-01-01T00:00:00Z", Time.utc(0).__send__(method)) # 1 BC
242     assert_equal("-0001-01-01T00:00:00Z", Time.utc(-1).__send__(method)) # 2 BC
243     assert_equal("-0004-01-01T00:00:00Z", Time.utc(-4).__send__(method)) # 5 BC
244     assert_equal("-9999-01-01T00:00:00Z", Time.utc(-9999).__send__(method))
245     assert_equal("-10000-01-01T00:00:00Z", Time.utc(-10000).__send__(method))
246   end
248   def test_completion
249     now = Time.local(2001,11,29,21,26,35)
250     assert_equal(Time.local( 2001,11,29,21,12),
251                  Time.parse("2001/11/29 21:12", now))
252     assert_equal(Time.local( 2001,11,29),
253                  Time.parse("2001/11/29", now))
254     assert_equal(Time.local( 2001,11,29),
255                  Time.parse(     "11/29", now))
256     #assert_equal(Time.local(2001,11,1), Time.parse("Nov", now))
257     assert_equal(Time.local( 2001,11,29,10,22),
258                  Time.parse(           "10:22", now))
259     assert_raise(ArgumentError) { Time.parse("foo", now) }
260   end
262   def test_completion_with_different_timezone
263     now = Time.new(2001,2,3,0,0,0,"+09:00") # 2001-02-02 15:00:00 UTC
264     t = Time.parse("10:20:30 GMT", now)
265     assert_equal(Time.utc(2001,2,2,10,20,30), t)
266     assert_equal(false, t.utc?)
267     assert_equal(0, t.utc_offset)
268   end
270   def test_invalid
271     # They were actually used in some web sites.
272     assert_raise(ArgumentError) { Time.httpdate("1 Dec 2001 10:23:57 GMT") }
273     assert_raise(ArgumentError) { Time.httpdate("Sat, 1 Dec 2001 10:25:42 GMT") }
274     assert_raise(ArgumentError) { Time.httpdate("Sat,  1-Dec-2001 10:53:55 GMT") }
275     assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-2001 10:15:34 GMT") }
276     assert_raise(ArgumentError) { Time.httpdate("Saturday, 01-Dec-101 11:10:07 GMT") }
277     assert_raise(ArgumentError) { Time.httpdate("Fri, 30 Nov 2001 21:30:00 JST") }
279     # They were actually used in some mails.
280     assert_raise(ArgumentError) { Time.rfc2822("01-5-20") }
281     assert_raise(ArgumentError) { Time.rfc2822("7/21/00") }
282     assert_raise(ArgumentError) { Time.rfc2822("2001-8-28") }
283     assert_raise(ArgumentError) { Time.rfc2822("00-5-6 1:13:06") }
284     assert_raise(ArgumentError) { Time.rfc2822("2001-9-27 9:36:49") }
285     assert_raise(ArgumentError) { Time.rfc2822("2000-12-13 11:01:11") }
286     assert_raise(ArgumentError) { Time.rfc2822("2001/10/17 04:29:55") }
287     assert_raise(ArgumentError) { Time.rfc2822("9/4/2001 9:23:19 PM") }
288     assert_raise(ArgumentError) { Time.rfc2822("01 Nov 2001 09:04:31") }
289     assert_raise(ArgumentError) { Time.rfc2822("13 Feb 2001 16:4 GMT") }
290     assert_raise(ArgumentError) { Time.rfc2822("01 Oct 00 5:41:19 PM") }
291     assert_raise(ArgumentError) { Time.rfc2822("2 Jul 00 00:51:37 JST") }
292     assert_raise(ArgumentError) { Time.rfc2822("01 11 2001 06:55:57 -0500") }
293     assert_raise(ArgumentError) { Time.rfc2822("18 \343\366\356\341\370 2000") }
294     assert_raise(ArgumentError) { Time.rfc2822("Fri, Oct 2001  18:53:32") }
295     assert_raise(ArgumentError) { Time.rfc2822("Fri, 2 Nov 2001 03:47:54") }
296     assert_raise(ArgumentError) { Time.rfc2822("Fri, 27 Jul 2001 11.14.14 +0200") }
297     assert_raise(ArgumentError) { Time.rfc2822("Thu, 2 Nov 2000 04:13:53 -600") }
298     assert_raise(ArgumentError) { Time.rfc2822("Wed, 5 Apr 2000 22:57:09 JST") }
299     assert_raise(ArgumentError) { Time.rfc2822("Mon, 11 Sep 2000 19:47:33 00000") }
300     assert_raise(ArgumentError) { Time.rfc2822("Fri, 28 Apr 2000 20:40:47 +-900") }
301     assert_raise(ArgumentError) { Time.rfc2822("Fri, 19 Jan 2001 8:15:36 AM -0500") }
302     assert_raise(ArgumentError) { Time.rfc2822("Thursday, Sep 27 2001 7:42:35 AM EST") }
303     assert_raise(ArgumentError) { Time.rfc2822("3/11/2001 1:31:57 PM Pacific Daylight Time") }
304     assert_raise(ArgumentError) { Time.rfc2822("Mi, 28 Mrz 2001 11:51:36") }
305     assert_raise(ArgumentError) { Time.rfc2822("P, 30 sept 2001 23:03:14") }
306     assert_raise(ArgumentError) { Time.rfc2822("fr, 11 aug 2000 18:39:22") }
307     assert_raise(ArgumentError) { Time.rfc2822("Fr, 21 Sep 2001 17:44:03 -1000") }
308     assert_raise(ArgumentError) { Time.rfc2822("Mo, 18 Jun 2001 19:21:40 -1000") }
309     assert_raise(ArgumentError) { Time.rfc2822("l\366, 12 aug 2000 18:53:20") }
310     assert_raise(ArgumentError) { Time.rfc2822("l\366, 26 maj 2001 00:15:58") }
311     assert_raise(ArgumentError) { Time.rfc2822("Dom, 30 Sep 2001 17:36:30") }
312     assert_raise(ArgumentError) { Time.rfc2822("%&, 31 %2/ 2000 15:44:47 -0500") }
313     assert_raise(ArgumentError) { Time.rfc2822("dom, 26 ago 2001 03:57:07 -0300") }
314     assert_raise(ArgumentError) { Time.rfc2822("ter, 04 set 2001 16:27:58 -0300") }
315     assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
316     assert_raise(ArgumentError) { Time.rfc2822("Wen, 3 oct 2001 23:17:49 -0400") }
317     assert_raise(ArgumentError) { Time.rfc2822("ele, 11 h: 2000 12:42:15 -0500") }
318     assert_raise(ArgumentError) { Time.rfc2822("Tue, 14 Aug 2001 3:55:3 +0200") }
319     assert_raise(ArgumentError) { Time.rfc2822("Fri, 25 Aug 2000 9:3:48 +0800") }
320     assert_raise(ArgumentError) { Time.rfc2822("Fri, 1 Dec 2000 0:57:50 EST") }
321     assert_raise(ArgumentError) { Time.rfc2822("Mon, 7 May 2001 9:39:51 +0200") }
322     assert_raise(ArgumentError) { Time.rfc2822("Wed, 1 Aug 2001 16:9:15 +0200") }
323     assert_raise(ArgumentError) { Time.rfc2822("Wed, 23 Aug 2000 9:17:36 +0800") }
324     assert_raise(ArgumentError) { Time.rfc2822("Fri, 11 Aug 2000 10:4:42 +0800") }
325     assert_raise(ArgumentError) { Time.rfc2822("Sat, 15 Sep 2001 13:22:2 +0300") }
326     assert_raise(ArgumentError) { Time.rfc2822("Wed,16 \276\305\324\302 2001 20:06:25 +0800") }
327     assert_raise(ArgumentError) { Time.rfc2822("Wed,7 \312\256\322\273\324\302 2001 23:47:22 +0800") }
328     assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=C5=DA),?= 10   2 2001 23:32:26 +0900 (JST)") }
329     assert_raise(ArgumentError) { Time.rfc2822("\307\341\314\343\332\311, 30 \344\346\335\343\310\321 2001 10:01:06") }
330     assert_raise(ArgumentError) { Time.rfc2822("=?iso-8859-1?Q?(=BF=E5),?= 12  =?iso-8859-1?Q?9=B7=EE?= 2001 14:52:41\n+0900 (JST)") }
332     # Out of range arguments
333     assert_raise(ArgumentError) { Time.parse("2014-13-13T18:00:00-0900") }
334   end
336   def test_zone_0000
337     assert_equal(true, Time.parse("2000-01-01T00:00:00Z").utc?)
338     assert_equal(true, Time.parse("2000-01-01T00:00:00-00:00").utc?)
339     assert_equal(false, Time.parse("2000-01-01T00:00:00+00:00").utc?)
340     assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
341     assert_equal(true, Time.parse("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
342     assert_equal(false, Time.parse("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
343     assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 GMT").utc?)
344     assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 -0000").utc?)
345     assert_equal(false, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 +0000").utc?)
346     assert_equal(true, Time.rfc2822("Sat, 01 Jan 2000 00:00:00 UTC").utc?)
347   end
349   def test_rfc2822_utc_roundtrip_winter
350     t1 = Time.local(2008,12,1)
351     t2 = Time.rfc2822(t1.rfc2822)
352     assert_equal(t1.utc?, t2.utc?, "[ruby-dev:37126]")
353   end
355   def test_rfc2822_utc_roundtrip_summer
356     t1 = Time.local(2008,8,1)
357     t2 = Time.rfc2822(t1.rfc2822)
358     assert_equal(t1.utc?, t2.utc?)
359   end
361   def test_parse_now_nil
362     assert_equal(Time.new(2000,1,1,0,0,0,"+11:00"),
363                  Time.parse("2000-01-01T00:00:00+11:00", nil))
364   end
366   def test_parse_offset_hour_minute_second
367     t = Time.at(-100000000000).utc
368     assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00"))
369     assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00"))
370     assert_equal(t, Time.parse("1200-02-15 BC 14:13:20-00:00:00"))
371   end
373   def test_parse_leap_second
374     t = Time.utc(1998,12,31,23,59,59)
375     assert_equal(t, Time.parse("Thu Dec 31 23:59:59 UTC 1998"))
376     assert_equal(t, Time.parse("Fri Dec 31 23:59:59 -0000 1998"));t.localtime
377     assert_equal(t, Time.parse("Fri Jan  1 08:59:59 +0900 1999"))
378     assert_equal(t, Time.parse("Fri Jan  1 00:59:59 +0100 1999"))
379     assert_equal(t, Time.parse("Fri Dec 31 23:59:59 +0000 1998"))
380     assert_equal(t, Time.parse("Fri Dec 31 22:59:59 -0100 1998"));t.utc
381     t += 1
382     assert_equal(t, Time.parse("Thu Dec 31 23:59:60 UTC 1998"))
383     assert_equal(t, Time.parse("Fri Dec 31 23:59:60 -0000 1998"));t.localtime
384     assert_equal(t, Time.parse("Fri Jan  1 08:59:60 +0900 1999"))
385     assert_equal(t, Time.parse("Fri Jan  1 00:59:60 +0100 1999"))
386     assert_equal(t, Time.parse("Fri Dec 31 23:59:60 +0000 1998"))
387     assert_equal(t, Time.parse("Fri Dec 31 22:59:60 -0100 1998"));t.utc
388     t += 1 if t.sec == 60
389     assert_equal(t, Time.parse("Thu Jan  1 00:00:00 UTC 1999"))
390     assert_equal(t, Time.parse("Fri Jan  1 00:00:00 -0000 1999"));t.localtime
391     assert_equal(t, Time.parse("Fri Jan  1 09:00:00 +0900 1999"))
392     assert_equal(t, Time.parse("Fri Jan  1 01:00:00 +0100 1999"))
393     assert_equal(t, Time.parse("Fri Jan  1 00:00:00 +0000 1999"))
394     assert_equal(t, Time.parse("Fri Dec 31 23:00:00 -0100 1998"))
395   end
397   def test_rfc2822_leap_second
398     t = Time.utc(1998,12,31,23,59,59)
399     assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:59 UTC"))
400     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 -0000"));t.localtime
401     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 08:59:59 +0900"))
402     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 00:59:59 +0100"))
403     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:59 +0000"))
404     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:59 -0100"));t.utc
405     t += 1
406     assert_equal(t, Time.rfc2822("Thu, 31 Dec 1998 23:59:60 UTC"))
407     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 -0000"));t.localtime
408     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 08:59:60 +0900"))
409     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 00:59:60 +0100"))
410     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:59:60 +0000"))
411     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 22:59:60 -0100"));t.utc
412     t += 1 if t.sec == 60
413     assert_equal(t, Time.rfc2822("Thu,  1 Jan 1999 00:00:00 UTC"))
414     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 00:00:00 -0000"));t.localtime
415     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 09:00:00 +0900"))
416     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 01:00:00 +0100"))
417     assert_equal(t, Time.rfc2822("Fri,  1 Jan 1999 00:00:00 +0000"))
418     assert_equal(t, Time.rfc2822("Fri, 31 Dec 1998 23:00:00 -0100"))
419   end
421   def subtest_xmlschema_leap_second(method)
422     t = Time.utc(1998,12,31,23,59,59)
423     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:59Z"))
424     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:59-00:00"));t.localtime
425     assert_equal(t, Time.__send__(method, "1999-01-01T08:59:59+09:00"))
426     assert_equal(t, Time.__send__(method, "1999-01-01T00:59:59+01:00"))
427     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:59+00:00"))
428     assert_equal(t, Time.__send__(method, "1998-12-31T22:59:59-01:00"));t.utc
429     t += 1
430     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:60Z"))
431     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:60-00:00"));t.localtime
432     assert_equal(t, Time.__send__(method, "1999-01-01T08:59:60+09:00"))
433     assert_equal(t, Time.__send__(method, "1999-01-01T00:59:60+01:00"))
434     assert_equal(t, Time.__send__(method, "1998-12-31T23:59:60+00:00"))
435     assert_equal(t, Time.__send__(method, "1998-12-31T22:59:60-01:00"));t.utc
436     t += 1 if t.sec == 60
437     assert_equal(t, Time.__send__(method, "1999-01-01T00:00:00Z"))
438     assert_equal(t, Time.__send__(method, "1999-01-01T00:00:00-00:00"));t.localtime
439     assert_equal(t, Time.__send__(method, "1999-01-01T09:00:00+09:00"))
440     assert_equal(t, Time.__send__(method, "1999-01-01T01:00:00+01:00"))
441     assert_equal(t, Time.__send__(method, "1999-01-01T00:00:00+00:00"))
442     assert_equal(t, Time.__send__(method, "1998-12-31T23:00:00-01:00"))
443   end
445   def subtest_xmlschema_fraction(method)
446     assert_equal(500000, Time.__send__(method, "2000-01-01T00:00:00.5+00:00").tv_usec)
447   end
449   def test_ruby_talk_152866
450     t = Time::xmlschema('2005-08-30T22:48:00-07:00')
451     assert_equal(30, t.day)
452     assert_equal(8, t.mon)
453     assert_equal(-7*3600, t.utc_offset)
454     t.utc
455     assert_equal(31, t.day)
456     assert_equal(8, t.mon)
457     assert_equal(0, t.utc_offset)
458   end
460   def test_parse_fraction
461     assert_equal(500000, Time.parse("2000-01-01T00:00:00.5+00:00").tv_usec)
462   end
464   def test_strptime
465     assert_equal(Time.utc(2005, 8, 28, 06, 54, 20), Time.strptime("28/Aug/2005:06:54:20 +0000", "%d/%b/%Y:%T %z"))
466     assert_equal(Time.at(1).localtime, Time.strptime("1", "%s"))
467     assert_equal(false, Time.strptime('0', '%s').utc?)
468   end
470   def test_strptime_empty
471     assert_raise(ArgumentError) { Time.strptime('', '') }
472     assert_raise(ArgumentError) { Time.strptime('+09:00', '%z') } # [ruby-core:62351] by matz.
473   end
475   def test_strptime_s_z
476     t = Time.strptime('0 +0100', '%s %z')
477     assert_equal(0, t.to_r)
478     assert_equal(3600, t.utc_offset)
479     t = Time.strptime('0 UTC', '%s %z')
480     assert_equal(0, t.to_r)
481     assert_equal(0, t.utc_offset)
482     assert_equal(true, t.utc?)
483   end
485   def test_strptime_s_N
486     assert_equal(Time.at(1, 500000), Time.strptime("1.5", "%s.%N"))
487     assert_equal(Time.at(-2, 500000), Time.strptime("-1.5", "%s.%N"))
488     t = Time.strptime("1.000000000001", "%s.%N")
489     assert_equal(1, t.to_i)
490     assert_equal(Rational("0.000000000001"), t.subsec)
491     t = Time.strptime("-1.000000000001", "%s.%N")
492     assert_equal(-2, t.to_i)
493     assert_equal(1-Rational("0.000000000001"), t.subsec)
494   end
496   def test_strptime_Ymd_z
497     t = Time.strptime('20010203 -0200', '%Y%m%d %z')
498     assert_equal(2001, t.year)
499     assert_equal(2, t.mon)
500     assert_equal(3, t.day)
501     assert_equal(0, t.hour)
502     assert_equal(0, t.min)
503     assert_equal(0, t.sec)
504     assert_equal(-7200, t.utc_offset)
505     t = Time.strptime('20010203 UTC', '%Y%m%d %z')
506     assert_equal(2001, t.year)
507     assert_equal(2, t.mon)
508     assert_equal(3, t.day)
509     assert_equal(0, t.hour)
510     assert_equal(0, t.min)
511     assert_equal(0, t.sec)
512     assert_equal(0, t.utc_offset)
513     assert_equal(true, t.utc?)
514   end
516   def test_strptime_j
517     t = Time.strptime("2018-365", "%Y-%j")
518     assert_equal(2018, t.year)
519     assert_equal(12, t.mon)
520     assert_equal(31, t.day)
521     assert_equal(0, t.hour)
522     assert_equal(0, t.min)
523     assert_equal(0, t.sec)
524     t = Time.strptime("2018-091", "%Y-%j")
525     assert_equal(2018, t.year)
526     assert_equal(4, t.mon)
527     assert_equal(1, t.day)
528   end
530   def test_strptime_p
531     t = Time.strptime("3am", "%I%p")
532     assert_equal(3, t.hour)
533     t = Time.strptime("3pm", "%I%p")
534     assert_equal(15, t.hour)
535     t = Time.strptime("3a.m.", "%I%p")
536     assert_equal(3, t.hour)
537     t = Time.strptime("3p.m.", "%I%p")
538     assert_equal(15, t.hour)
539     t = Time.strptime("3AM", "%I%p")
540     assert_equal(3, t.hour)
541     t = Time.strptime("3PM", "%I%p")
542     assert_equal(15, t.hour)
543     t = Time.strptime("3A.M.", "%I%p")
544     assert_equal(3, t.hour)
545     t = Time.strptime("3P.M.", "%I%p")
546     assert_equal(15, t.hour)
547   end
549   def test_strptime_wuvg
550     assert_equal(Time.local(2019, 1, 30), Time.strptime("3 4 2019", "%w %W %Y"))
551     assert_equal(Time.local(2019, 2, 7), Time.strptime("4 5 2019", "%u %U %Y"))
552     assert_equal(Time.local(2019, 1, 28), Time.strptime("4 2019", "%W %Y"))
553     assert_equal(Time.local(2019, 2, 3), Time.strptime("5 2019", "%U %Y"))
554     assert_equal(Time.local(2019, 1, 1), Time.strptime("1 2 2019", "%V %w %G"))
555     assert_equal(Time.local(2016, 1, 1), Time.strptime("53 5 15", "%V %w %g"))
556     assert_equal(Time.local(2018, 12, 31), Time.strptime("1 2019", "%V %G"))
557     assert_equal(Time.local(2015, 12, 28), Time.strptime("53 15", "%V %g"))
558   end
560   def test_nsec
561     assert_equal(123456789, Time.parse("2000-01-01T00:00:00.123456789+00:00").tv_nsec)
562   end
564   def subtest_xmlschema_nsec(method)
565     assert_equal(123456789, Time.__send__(method, "2000-01-01T00:00:00.123456789+00:00").tv_nsec)
566   end
568   def test_huge_precision
569     bug4456 = '[ruby-dev:43284]'
570     assert_normal_exit %q{ Time.now.strftime("%1000000000F") }, bug4456
571   end
573   instance_methods(false).grep(/\Asub(test_xmlschema.*)/) do |sub|
574     test = $1
575     define_method(test) {__send__(sub, :xmlschema)}
576     define_method(test.sub(/xmlschema/, 'iso8601')) {__send__(sub, :iso8601)}
577   end
579   def test_parse_with_various_object
580     d  = Date.new(2010, 10, 28)
581     dt = DateTime.new(2010, 10, 28)
582     md = MyDate.new(10, 28, 2010)
584     t = Time.local(2010, 10, 28, 21, 26, 00)
585     assert_equal(t, Time.parse("21:26",  d))
586     assert_equal(t, Time.parse("21:26", dt))
587     assert_equal(t, Time.parse("21:26", md))
588   end
590   class MyDate
591     attr_reader :mon, :day, :year
593     def initialize(mon, day, year)
594       @mon, @day, @year = mon, day, year
595     end
596   end