PageRenderTime 30ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/test/test_time.rb

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