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