PageRenderTime 40ms CodeModel.GetById 1ms RepoModel.GetById 1ms app.codeStats 0ms

/test/ruby/test_time.rb

http://github.com/ruby/ruby
Ruby | 1282 lines | 1239 code | 38 blank | 5 comment | 7 complexity | 289b6dd4b2e12bea3114bf0f2b46266c MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, AGPL-3.0
  1. # frozen_string_literal: false
  2. require 'test/unit'
  3. require 'delegate'
  4. require 'timeout'
  5. require 'delegate'
  6. class TestTime < Test::Unit::TestCase
  7. def setup
  8. @verbose = $VERBOSE
  9. $VERBOSE = nil
  10. end
  11. def teardown
  12. $VERBOSE = @verbose
  13. end
  14. def in_timezone(zone)
  15. orig_zone = ENV['TZ']
  16. ENV['TZ'] = zone
  17. yield
  18. ensure
  19. ENV['TZ'] = orig_zone
  20. end
  21. def no_leap_seconds?
  22. # 1972-06-30T23:59:60Z is the first leap second.
  23. Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
  24. end
  25. def get_t2000
  26. if no_leap_seconds?
  27. # Sat Jan 01 00:00:00 UTC 2000
  28. Time.at(946684800).gmtime
  29. else
  30. Time.utc(2000, 1, 1)
  31. end
  32. end
  33. def test_new
  34. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, 3600*11))
  35. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,9, 13,0,0, -3600*11))
  36. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, "+11:00"))
  37. assert_equal(Rational(1,2), Time.new(2000,2,10, 11,0,5.5, "+11:00").subsec)
  38. bug4090 = '[ruby-dev:42631]'
  39. tm = [2001,2,28,23,59,30]
  40. t = Time.new(*tm, "-12:00")
  41. assert_equal([2001,2,28,23,59,30,-43200], [t.year, t.month, t.mday, t.hour, t.min, t.sec, t.gmt_offset], bug4090)
  42. assert_raise(ArgumentError) { Time.new(2000,1,1, 0,0,0, "+01:60") }
  43. end
  44. def test_time_add()
  45. assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
  46. Time.utc(2000, 3, 21, 6, 30))
  47. assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
  48. Time.utc(2000, 3, 21, 0, 30))
  49. assert_equal(0, (Time.at(1.1) + 0.9).usec)
  50. assert_predicate((Time.utc(2000, 4, 1) + 24), :utc?)
  51. assert_not_predicate((Time.local(2000, 4, 1) + 24), :utc?)
  52. t = Time.new(2000, 4, 1, 0, 0, 0, "+01:00") + 24
  53. assert_not_predicate(t, :utc?)
  54. assert_equal(3600, t.utc_offset)
  55. t = Time.new(2000, 4, 1, 0, 0, 0, "+02:00") + 24
  56. assert_not_predicate(t, :utc?)
  57. assert_equal(7200, t.utc_offset)
  58. end
  59. def test_time_subt()
  60. assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
  61. Time.utc(2000, 3, 21, 0, 30))
  62. assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
  63. Time.utc(2000, 3, 21, 6, 30))
  64. assert_equal(900000, (Time.at(1.1) - 0.2).usec)
  65. end
  66. def test_time_time()
  67. assert_equal(Time.utc(2000, 3, 21, 3, 30) \
  68. -Time.utc(2000, 3, 21, 0, 30), 3*3600)
  69. assert_equal(Time.utc(2000, 3, 21, 0, 30) \
  70. -Time.utc(2000, 3, 21, 3, 30), -3*3600)
  71. end
  72. def negative_time_t?
  73. begin
  74. Time.at(-1)
  75. true
  76. rescue ArgumentError
  77. false
  78. end
  79. end
  80. def test_timegm
  81. if negative_time_t?
  82. assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
  83. assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
  84. assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
  85. end
  86. assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
  87. assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
  88. assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
  89. assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
  90. assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)
  91. if no_leap_seconds?
  92. assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  93. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  94. assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  95. # Giveup to try 2nd test because some state is changed.
  96. skip if Minitest::Unit.current_repeat_count > 0
  97. assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
  98. assert_equal(0x80000000, Time.utc(2038, 1, 19, 3, 14, 8).tv_sec)
  99. else
  100. assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
  101. assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
  102. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  103. assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  104. assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  105. end
  106. end
  107. def test_strtime
  108. t = nil
  109. assert_nothing_raised { t = Time.utc("2000", "1", "2" , "3", "4", "5") }
  110. assert_equal(Time.utc(2000,1,2,3,4,5), t)
  111. end
  112. def test_huge_difference
  113. if negative_time_t?
  114. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
  115. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
  116. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
  117. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
  118. end
  119. end
  120. def test_big_minus
  121. begin
  122. bigtime0 = Time.at(2**60)
  123. bigtime1 = Time.at(2**60+1)
  124. rescue RangeError
  125. return
  126. end
  127. assert_equal(1.0, bigtime1 - bigtime0)
  128. end
  129. def test_at
  130. assert_equal(100000, Time.at("0.1".to_r).usec)
  131. assert_equal(10000, Time.at("0.01".to_r).usec)
  132. assert_equal(1000, Time.at("0.001".to_r).usec)
  133. assert_equal(100, Time.at("0.0001".to_r).usec)
  134. assert_equal(10, Time.at("0.00001".to_r).usec)
  135. assert_equal(1, Time.at("0.000001".to_r).usec)
  136. assert_equal(100000000, Time.at("0.1".to_r).nsec)
  137. assert_equal(10000000, Time.at("0.01".to_r).nsec)
  138. assert_equal(1000000, Time.at("0.001".to_r).nsec)
  139. assert_equal(100000, Time.at("0.0001".to_r).nsec)
  140. assert_equal(10000, Time.at("0.00001".to_r).nsec)
  141. assert_equal(1000, Time.at("0.000001".to_r).nsec)
  142. assert_equal(100, Time.at("0.0000001".to_r).nsec)
  143. assert_equal(10, Time.at("0.00000001".to_r).nsec)
  144. assert_equal(1, Time.at("0.000000001".to_r).nsec)
  145. assert_equal(100000, Time.at(0.1).usec)
  146. assert_equal(10000, Time.at(0.01).usec)
  147. assert_equal(1000, Time.at(0.001).usec)
  148. assert_equal(100, Time.at(0.0001).usec)
  149. assert_equal(10, Time.at(0.00001).usec)
  150. assert_equal(3, Time.at(0.000003).usec)
  151. assert_equal(100000000, Time.at(0.1).nsec)
  152. assert_equal(10000000, Time.at(0.01).nsec)
  153. assert_equal(1000000, Time.at(0.001).nsec)
  154. assert_equal(100000, Time.at(0.0001).nsec)
  155. assert_equal(10000, Time.at(0.00001).nsec)
  156. assert_equal(3000, Time.at(0.000003).nsec)
  157. assert_equal(200, Time.at(0.0000002r).nsec)
  158. assert_in_delta(200, Time.at(0.0000002).nsec, 1, "should be within FP error")
  159. assert_equal(10, Time.at(0.00000001).nsec)
  160. assert_equal(1, Time.at(0.000000001).nsec)
  161. assert_equal(0, Time.at(1e-10).nsec)
  162. assert_equal(0, Time.at(4e-10).nsec)
  163. assert_equal(0, Time.at(6e-10).nsec)
  164. assert_equal(1, Time.at(14e-10).nsec)
  165. assert_equal(1, Time.at(16e-10).nsec)
  166. if negative_time_t?
  167. assert_equal(999999999, Time.at(-1e-10).nsec)
  168. assert_equal(999999999, Time.at(-4e-10).nsec)
  169. assert_equal(999999999, Time.at(-6e-10).nsec)
  170. assert_equal(999999998, Time.at(-14e-10).nsec)
  171. assert_equal(999999998, Time.at(-16e-10).nsec)
  172. end
  173. t = Time.at(-4611686019).utc
  174. assert_equal(1823, t.year)
  175. t = Time.at(4611686018, 999999).utc
  176. assert_equal(2116, t.year)
  177. assert_equal("0.999999".to_r, t.subsec)
  178. t = Time.at(2**40 + "1/3".to_r, 9999999999999).utc
  179. assert_equal(36812, t.year)
  180. t = Time.at(-0x3fff_ffff_ffff_ffff)
  181. assert_equal(-146138510344, t.year)
  182. t = Time.at(-0x4000_0000_0000_0000)
  183. assert_equal(-146138510344, t.year)
  184. t = Time.at(-0x4000_0000_0000_0001)
  185. assert_equal(-146138510344, t.year)
  186. t = Time.at(-0x5000_0000_0000_0001)
  187. assert_equal(-182673138422, t.year)
  188. t = Time.at(-0x6000_0000_0000_0000)
  189. assert_equal(-219207766501, t.year)
  190. t = Time.at(0).utc
  191. assert_equal([1970,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  192. t = Time.at(-86400).utc
  193. assert_equal([1969,12,31, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  194. t = Time.at(-86400 * (400 * 365 + 97)).utc
  195. assert_equal([1970-400,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  196. t = Time.at(-86400 * (400 * 365 + 97)*1000).utc
  197. assert_equal([1970-400*1000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  198. t = Time.at(-86400 * (400 * 365 + 97)*2421).utc
  199. assert_equal([1970-400*2421,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  200. t = Time.at(-86400 * (400 * 365 + 97)*1000000).utc
  201. assert_equal([1970-400*1000000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  202. t = Time.at(-30613683110400).utc
  203. assert_equal([-968139,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  204. t = Time.at(-30613683110401).utc
  205. assert_equal([-968140,12,31, 23,59,59], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  206. end
  207. def test_at2
  208. assert_equal(100, Time.at(0, 0.1).nsec)
  209. assert_equal(10, Time.at(0, 0.01).nsec)
  210. assert_equal(1, Time.at(0, 0.001).nsec)
  211. end
  212. def test_at_with_unit
  213. assert_equal(123456789, Time.at(0, 123456789, :nanosecond).nsec)
  214. assert_equal(123456789, Time.at(0, 123456789, :nsec).nsec)
  215. assert_equal(123456000, Time.at(0, 123456, :microsecond).nsec)
  216. assert_equal(123456000, Time.at(0, 123456, :usec).nsec)
  217. assert_equal(123000000, Time.at(0, 123, :millisecond).nsec)
  218. assert_raise(ArgumentError){ Time.at(0, 1, 2) }
  219. assert_raise(ArgumentError){ Time.at(0, 1, :invalid) }
  220. assert_raise(ArgumentError){ Time.at(0, 1, nil) }
  221. end
  222. def test_at_rational
  223. assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
  224. assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
  225. end
  226. def test_utc_subsecond
  227. assert_equal(500000, Time.utc(2007,1,1,0,0,1.5).usec)
  228. assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
  229. end
  230. def test_eq_nsec
  231. assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
  232. assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
  233. end
  234. def assert_marshal_roundtrip(t)
  235. iv_names = t.instance_variables
  236. iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
  237. m = Marshal.dump(t)
  238. t2 = Marshal.load(m)
  239. iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
  240. assert_equal(t, t2)
  241. assert_equal(iv_vals1, iv_vals2)
  242. t2
  243. end
  244. def test_marshal_nsec
  245. assert_marshal_roundtrip(Time.at(0, 0.123))
  246. assert_marshal_roundtrip(Time.at(0, 0.120))
  247. end
  248. def test_marshal_nsec_191
  249. # generated by ruby 1.9.1p376
  250. m = "\x04\bIu:\tTime\r \x80\x11\x80@\xE2\x01\x00\x06:\rsubmicro\"\ax\x90"
  251. t = Marshal.load(m)
  252. assert_equal(Time.at(Rational(123456789, 1000000000)), t, "[ruby-dev:40133]")
  253. end
  254. def test_marshal_rational
  255. assert_marshal_roundtrip(Time.at(0, Rational(1,3)))
  256. assert_not_match(/Rational/, Marshal.dump(Time.at(0, Rational(1,3))))
  257. end
  258. def test_marshal_ivar
  259. t = Time.at(123456789, 987654.321)
  260. t.instance_eval { @var = 135 }
  261. assert_marshal_roundtrip(t)
  262. assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
  263. end
  264. def test_marshal_timezone
  265. bug = '[ruby-dev:40063]'
  266. t1 = Time.gm(2000)
  267. m = Marshal.dump(t1.getlocal("-02:00"))
  268. t2 = Marshal.load(m)
  269. assert_equal(t1, t2)
  270. assert_equal(-7200, t2.utc_offset, bug)
  271. m = Marshal.dump(t1.getlocal("+08:15"))
  272. t2 = Marshal.load(m)
  273. assert_equal(t1, t2)
  274. assert_equal(29700, t2.utc_offset, bug)
  275. end
  276. def test_marshal_zone
  277. t = Time.utc(2013, 2, 24)
  278. assert_equal('UTC', t.zone)
  279. assert_equal('UTC', Marshal.load(Marshal.dump(t)).zone)
  280. in_timezone('JST-9') do
  281. t = Time.local(2013, 2, 24)
  282. assert_equal('JST', Time.local(2013, 2, 24).zone)
  283. t = Marshal.load(Marshal.dump(t))
  284. assert_equal('JST', t.zone)
  285. assert_equal('JST', (t+1).zone, '[ruby-core:81892] [Bug #13710]')
  286. end
  287. end
  288. def test_marshal_zone_gc
  289. assert_separately(%w(--disable-gems), <<-'end;', timeout: 30)
  290. ENV["TZ"] = "JST-9"
  291. s = Marshal.dump(Time.now)
  292. t = Marshal.load(s)
  293. n = 0
  294. done = 100000
  295. while t.zone.dup == "JST" && n < done
  296. n += 1
  297. end
  298. assert_equal done, n, "Bug #9652"
  299. assert_equal "JST", t.zone, "Bug #9652"
  300. end;
  301. end
  302. def test_marshal_to_s
  303. t1 = Time.new(2011,11,8, 0,42,25, 9*3600)
  304. t2 = Time.at(Marshal.load(Marshal.dump(t1)))
  305. assert_equal("2011-11-08 00:42:25 +0900", t2.to_s,
  306. "[ruby-dev:44827] [Bug #5586]")
  307. end
  308. Bug8795 = '[ruby-core:56648] [Bug #8795]'
  309. def test_marshal_broken_offset
  310. data = "\x04\bIu:\tTime\r\xEFF\x1C\x80\x00\x00\x00\x00\x06:\voffset"
  311. t1 = t2 = nil
  312. in_timezone('UTC') do
  313. assert_nothing_raised(TypeError, ArgumentError, Bug8795) do
  314. t1 = Marshal.load(data + "T")
  315. t2 = Marshal.load(data + "\"\x0ebadoffset")
  316. end
  317. assert_equal(0, t1.utc_offset)
  318. assert_equal(0, t2.utc_offset)
  319. end
  320. end
  321. def test_marshal_broken_zone
  322. data = "\x04\bIu:\tTime\r\xEFF\x1C\x80\x00\x00\x00\x00\x06:\tzone"
  323. t1 = t2 = nil
  324. in_timezone('UTC') do
  325. assert_nothing_raised(TypeError, ArgumentError, Bug8795) do
  326. t1 = Marshal.load(data + "T")
  327. t2 = Marshal.load(data + "\"\b\0\0\0")
  328. end
  329. assert_equal('UTC', t1.zone)
  330. assert_equal('UTC', t2.zone)
  331. end
  332. end
  333. def test_marshal_distant_past
  334. assert_marshal_roundtrip(Time.utc(1890, 1, 1))
  335. assert_marshal_roundtrip(Time.utc(-4.5e9, 1, 1))
  336. end
  337. def test_marshal_distant_future
  338. assert_marshal_roundtrip(Time.utc(30000, 1, 1))
  339. assert_marshal_roundtrip(Time.utc(5.67e9, 4, 8))
  340. end
  341. def test_at3
  342. t2000 = get_t2000
  343. assert_equal(t2000, Time.at(t2000))
  344. # assert_raise(RangeError) do
  345. # Time.at(2**31-1, 1_000_000)
  346. # Time.at(2**63-1, 1_000_000)
  347. # end
  348. # assert_raise(RangeError) do
  349. # Time.at(-2**31, -1_000_000)
  350. # Time.at(-2**63, -1_000_000)
  351. # end
  352. end
  353. def test_utc_or_local
  354. t2000 = get_t2000
  355. assert_equal(t2000, Time.gm(2000))
  356. assert_equal(t2000, Time.gm(0, 0, 0, 1, 1, 2000, :foo, :bar, false, :baz))
  357. assert_equal(t2000, Time.gm(2000, "jan"))
  358. assert_equal(t2000, Time.gm(2000, "1"))
  359. assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, 0, 0))
  360. assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, 0, "0"))
  361. assert_equal(t2000, Time.gm(2000, 1, 1, 0, 0, "0", :foo, :foo))
  362. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1, :foo, :foo) }
  363. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1.0, :foo, :foo) }
  364. assert_raise(RangeError) do
  365. Time.gm(2000, 1, 1, 0, 0, 10_000_000_000_000_000_001.0, :foo, :foo)
  366. end
  367. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -(2**31), :foo, :foo) }
  368. o = Object.new
  369. def o.to_int; 0; end
  370. def o.to_r; nil; end
  371. assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  372. def o.to_r; ""; end
  373. assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  374. def o.to_r; Rational(11); end
  375. assert_equal(11, Time.gm(2000, 1, 1, 0, 0, o).sec)
  376. o = Object.new
  377. def o.to_int; 10; end
  378. assert_equal(10, Time.gm(2000, 1, 1, 0, 0, o).sec)
  379. assert_raise(ArgumentError) { Time.gm(2000, 13) }
  380. t = Time.local(2000)
  381. assert_equal(t.gmt_offset, t2000 - t)
  382. assert_equal(-4427700000, Time.utc(-4427700000,12,1).year)
  383. assert_equal(-2**30+10, Time.utc(-2**30+10,1,1).year)
  384. assert_raise(ArgumentError) { Time.gm(2000, 1, -1) }
  385. assert_raise(ArgumentError) { Time.gm(2000, 1, 2**30 + 1) }
  386. assert_raise(ArgumentError) { Time.gm(2000, 1, -2**30 + 1) }
  387. end
  388. def test_time_interval
  389. m = Thread::Mutex.new.lock
  390. assert_nothing_raised {
  391. Timeout.timeout(10) {
  392. m.sleep(0)
  393. }
  394. }
  395. assert_raise(ArgumentError) { m.sleep(-1) }
  396. assert_raise(TypeError) { m.sleep("") }
  397. assert_raise(TypeError) { sleep("") }
  398. obj = eval("class C\u{1f5ff}; self; end").new
  399. assert_raise_with_message(TypeError, /C\u{1f5ff}/) {m.sleep(obj)}
  400. assert_raise_with_message(TypeError, /C\u{1f5ff}/) {sleep(obj)}
  401. end
  402. def test_to_f
  403. t2000 = Time.at(946684800).gmtime
  404. assert_equal(946684800.0, t2000.to_f)
  405. end
  406. def test_to_f_accuracy
  407. # https://bugs.ruby-lang.org/issues/10135#note-1
  408. f = 1381089302.195
  409. assert_equal(f, Time.at(f).to_f, "[ruby-core:64373] [Bug #10135] note-1")
  410. end
  411. def test_cmp
  412. t2000 = get_t2000
  413. assert_equal(-1, t2000 <=> Time.gm(2001))
  414. assert_equal(1, t2000 <=> Time.gm(1999))
  415. assert_nil(t2000 <=> 0)
  416. end
  417. def test_eql
  418. t2000 = get_t2000
  419. assert_operator(t2000, :eql?, t2000)
  420. assert_not_operator(t2000, :eql?, Time.gm(2001))
  421. end
  422. def test_utc_p
  423. assert_predicate(Time.gm(2000), :gmt?)
  424. assert_not_predicate(Time.local(2000), :gmt?)
  425. assert_not_predicate(Time.at(0), :gmt?)
  426. end
  427. def test_hash
  428. t2000 = get_t2000
  429. assert_kind_of(Integer, t2000.hash)
  430. end
  431. def test_reinitialize
  432. bug8099 = '[ruby-core:53436] [Bug #8099]'
  433. t2000 = get_t2000
  434. assert_raise(TypeError, bug8099) {
  435. t2000.send(:initialize, 2013, 03, 14)
  436. }
  437. assert_equal(get_t2000, t2000, bug8099)
  438. end
  439. def test_init_copy
  440. t2000 = get_t2000
  441. assert_equal(t2000, t2000.dup)
  442. assert_raise(TypeError) do
  443. t2000.instance_eval { initialize_copy(nil) }
  444. end
  445. end
  446. def test_localtime_gmtime
  447. assert_nothing_raised do
  448. t = Time.gm(2000)
  449. assert_predicate(t, :gmt?)
  450. t.localtime
  451. assert_not_predicate(t, :gmt?)
  452. t.localtime
  453. assert_not_predicate(t, :gmt?)
  454. t.gmtime
  455. assert_predicate(t, :gmt?)
  456. t.gmtime
  457. assert_predicate(t, :gmt?)
  458. end
  459. t1 = Time.gm(2000)
  460. t2 = t1.getlocal
  461. assert_equal(t1, t2)
  462. t3 = t1.getlocal("-02:00")
  463. assert_equal(t1, t3)
  464. assert_equal(-7200, t3.utc_offset)
  465. assert_equal([1999, 12, 31, 22, 0, 0], [t3.year, t3.mon, t3.mday, t3.hour, t3.min, t3.sec])
  466. t1.localtime
  467. assert_equal(t1, t2)
  468. assert_equal(t1.gmt?, t2.gmt?)
  469. assert_equal(t1, t3)
  470. t1 = Time.local(2000)
  471. t2 = t1.getgm
  472. assert_equal(t1, t2)
  473. t3 = t1.getlocal("-02:00")
  474. assert_equal(t1, t3)
  475. assert_equal(-7200, t3.utc_offset)
  476. t1.gmtime
  477. assert_equal(t1, t2)
  478. assert_equal(t1.gmt?, t2.gmt?)
  479. assert_equal(t1, t3)
  480. end
  481. def test_asctime
  482. t2000 = get_t2000
  483. assert_equal("Sat Jan 1 00:00:00 2000", t2000.asctime)
  484. assert_equal(Encoding::US_ASCII, t2000.asctime.encoding)
  485. assert_kind_of(String, Time.at(0).asctime)
  486. end
  487. def test_to_s
  488. t2000 = get_t2000
  489. assert_equal("2000-01-01 00:00:00 UTC", t2000.to_s)
  490. assert_equal(Encoding::US_ASCII, t2000.to_s.encoding)
  491. assert_kind_of(String, Time.at(946684800).getlocal.to_s)
  492. assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
  493. end
  494. def test_inspect
  495. t2000 = get_t2000
  496. assert_equal("2000-01-01 00:00:00 UTC", t2000.inspect)
  497. assert_equal(Encoding::US_ASCII, t2000.inspect.encoding)
  498. assert_kind_of(String, Time.at(946684800).getlocal.inspect)
  499. assert_equal(Time.at(946684800).getlocal.inspect, Time.at(946684800).inspect)
  500. t2000 = get_t2000 + 1/10r
  501. assert_equal("2000-01-01 00:00:00.1 UTC", t2000.inspect)
  502. t2000 = get_t2000 + 1/1000000000r
  503. assert_equal("2000-01-01 00:00:00.000000001 UTC", t2000.inspect)
  504. t2000 = get_t2000 + 1/10000000000r
  505. assert_equal("2000-01-01 00:00:00 1/10000000000 UTC", t2000.inspect)
  506. t2000 = get_t2000 + 0.1
  507. assert_equal("2000-01-01 00:00:00 3602879701896397/36028797018963968 UTC", t2000.inspect)
  508. t2000 = get_t2000
  509. t2000 = t2000.localtime(9*3600)
  510. assert_equal("2000-01-01 09:00:00 +0900", t2000.inspect)
  511. t2000 = get_t2000.localtime(9*3600) + 1/10r
  512. assert_equal("2000-01-01 09:00:00.1 +0900", t2000.inspect)
  513. end
  514. def assert_zone_encoding(time)
  515. zone = time.zone
  516. assert_predicate(zone, :valid_encoding?)
  517. if zone.ascii_only?
  518. assert_equal(Encoding::US_ASCII, zone.encoding)
  519. else
  520. enc = Encoding.default_internal || Encoding.find('locale')
  521. assert_equal(enc, zone.encoding)
  522. end
  523. end
  524. def test_zone
  525. assert_zone_encoding Time.now
  526. t = Time.now.utc
  527. assert_equal("UTC", t.zone)
  528. assert_nil(t.getlocal(0).zone)
  529. assert_nil(t.getlocal("+02:00").zone)
  530. end
  531. def test_plus_minus_succ
  532. t2000 = get_t2000
  533. # assert_raise(RangeError) { t2000 + 10000000000 }
  534. # assert_raise(RangeError) t2000 - 3094168449 }
  535. # assert_raise(RangeError) { t2000 + 1200798848 }
  536. assert_raise(TypeError) { t2000 + Time.now }
  537. assert_equal(t2000 + 1, t2000.succ)
  538. end
  539. def test_plus_type
  540. t0 = Time.utc(2000,1,1)
  541. n0 = t0.to_i
  542. n1 = n0+1
  543. t1 = Time.at(n1)
  544. assert_equal(t1, t0 + 1)
  545. assert_equal(t1, t0 + 1.0)
  546. assert_equal(t1, t0 + Rational(1,1))
  547. assert_equal(t1, t0 + SimpleDelegator.new(1))
  548. assert_equal(t1, t0 + SimpleDelegator.new(1.0))
  549. assert_equal(t1, t0 + SimpleDelegator.new(Rational(1,1)))
  550. assert_raise(TypeError) { t0 + nil }
  551. assert_raise(TypeError) { t0 + "1" }
  552. assert_raise(TypeError) { t0 + SimpleDelegator.new("1") }
  553. assert_equal(0.5, (t0 + 1.5).subsec)
  554. assert_equal(Rational(1,3), (t0 + Rational(4,3)).subsec)
  555. assert_equal(0.5, (t0 + SimpleDelegator.new(1.5)).subsec)
  556. assert_equal(Rational(1,3), (t0 + SimpleDelegator.new(Rational(4,3))).subsec)
  557. end
  558. def test_minus
  559. t = Time.at(-4611686018).utc - 100
  560. assert_equal(1823, t.year)
  561. end
  562. def test_readers
  563. t2000 = get_t2000
  564. assert_equal(0, t2000.sec)
  565. assert_equal(0, t2000.min)
  566. assert_equal(0, t2000.hour)
  567. assert_equal(1, t2000.mday)
  568. assert_equal(1, t2000.mon)
  569. assert_equal(2000, t2000.year)
  570. assert_equal(6, t2000.wday)
  571. assert_equal(1, t2000.yday)
  572. assert_equal(false, t2000.isdst)
  573. assert_equal("UTC", t2000.zone)
  574. assert_zone_encoding(t2000)
  575. assert_equal(0, t2000.gmt_offset)
  576. assert_not_predicate(t2000, :sunday?)
  577. assert_not_predicate(t2000, :monday?)
  578. assert_not_predicate(t2000, :tuesday?)
  579. assert_not_predicate(t2000, :wednesday?)
  580. assert_not_predicate(t2000, :thursday?)
  581. assert_not_predicate(t2000, :friday?)
  582. assert_predicate(t2000, :saturday?)
  583. assert_equal([0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], t2000.to_a)
  584. t = Time.at(946684800).getlocal
  585. assert_equal(t.sec, Time.at(946684800).sec)
  586. assert_equal(t.min, Time.at(946684800).min)
  587. assert_equal(t.hour, Time.at(946684800).hour)
  588. assert_equal(t.mday, Time.at(946684800).mday)
  589. assert_equal(t.mon, Time.at(946684800).mon)
  590. assert_equal(t.year, Time.at(946684800).year)
  591. assert_equal(t.wday, Time.at(946684800).wday)
  592. assert_equal(t.yday, Time.at(946684800).yday)
  593. assert_equal(t.isdst, Time.at(946684800).isdst)
  594. assert_equal(t.zone, Time.at(946684800).zone)
  595. assert_zone_encoding(Time.at(946684800))
  596. assert_equal(t.gmt_offset, Time.at(946684800).gmt_offset)
  597. assert_equal(t.sunday?, Time.at(946684800).sunday?)
  598. assert_equal(t.monday?, Time.at(946684800).monday?)
  599. assert_equal(t.tuesday?, Time.at(946684800).tuesday?)
  600. assert_equal(t.wednesday?, Time.at(946684800).wednesday?)
  601. assert_equal(t.thursday?, Time.at(946684800).thursday?)
  602. assert_equal(t.friday?, Time.at(946684800).friday?)
  603. assert_equal(t.saturday?, Time.at(946684800).saturday?)
  604. assert_equal(t.to_a, Time.at(946684800).to_a)
  605. end
  606. def test_strftime
  607. t2000 = get_t2000
  608. t = Time.at(946684800).getlocal
  609. assert_equal("Sat", t2000.strftime("%a"))
  610. assert_equal("Saturday", t2000.strftime("%A"))
  611. assert_equal("Jan", t2000.strftime("%b"))
  612. assert_equal("January", t2000.strftime("%B"))
  613. assert_kind_of(String, t2000.strftime("%c"))
  614. assert_equal("01", t2000.strftime("%d"))
  615. assert_equal("00", t2000.strftime("%H"))
  616. assert_equal("12", t2000.strftime("%I"))
  617. assert_equal("001", t2000.strftime("%j"))
  618. assert_equal("01", t2000.strftime("%m"))
  619. assert_equal("00", t2000.strftime("%M"))
  620. assert_equal("AM", t2000.strftime("%p"))
  621. assert_equal("00", t2000.strftime("%S"))
  622. assert_equal("00", t2000.strftime("%U"))
  623. assert_equal("00", t2000.strftime("%W"))
  624. assert_equal("6", t2000.strftime("%w"))
  625. assert_equal("01/01/00", t2000.strftime("%x"))
  626. assert_equal("00:00:00", t2000.strftime("%X"))
  627. assert_equal("00", t2000.strftime("%y"))
  628. assert_equal("2000", t2000.strftime("%Y"))
  629. assert_equal("UTC", t2000.strftime("%Z"))
  630. assert_equal("%", t2000.strftime("%%"))
  631. assert_equal("0", t2000.strftime("%-S"))
  632. assert_equal("12:00:00 AM", t2000.strftime("%r"))
  633. assert_equal("Sat 2000-01-01T00:00:00", t2000.strftime("%3a %FT%T"))
  634. assert_equal("", t2000.strftime(""))
  635. assert_equal("foo\0bar\x0000\x0000\x0000", t2000.strftime("foo\0bar\0%H\0%M\0%S"))
  636. assert_equal("foo" * 1000, t2000.strftime("foo" * 1000))
  637. t = Time.mktime(2000, 1, 1)
  638. assert_equal("Sat", t.strftime("%a"))
  639. end
  640. def test_strftime_subsec
  641. t = Time.at(946684800, 123456.789)
  642. assert_equal("123", t.strftime("%3N"))
  643. assert_equal("123456", t.strftime("%6N"))
  644. assert_equal("123456789", t.strftime("%9N"))
  645. assert_equal("1234567890", t.strftime("%10N"))
  646. assert_equal("123456789", t.strftime("%0N"))
  647. end
  648. def test_strftime_sec
  649. t = get_t2000.getlocal
  650. assert_equal("000", t.strftime("%3S"))
  651. end
  652. def test_strftime_seconds_from_epoch
  653. t = Time.at(946684800, 123456.789)
  654. assert_equal("946684800", t.strftime("%s"))
  655. assert_equal("946684800", t.utc.strftime("%s"))
  656. t = Time.at(10000000000000000000000)
  657. assert_equal("<<10000000000000000000000>>", t.strftime("<<%s>>"))
  658. assert_equal("<<010000000000000000000000>>", t.strftime("<<%24s>>"))
  659. assert_equal("<<010000000000000000000000>>", t.strftime("<<%024s>>"))
  660. assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24s>>"))
  661. end
  662. def test_strftime_zone
  663. t = Time.mktime(2001, 10, 1)
  664. assert_equal("2001-10-01", t.strftime("%F"))
  665. assert_equal(Encoding::UTF_8, t.strftime("\u3042%Z").encoding)
  666. assert_equal(true, t.strftime("\u3042%Z").valid_encoding?)
  667. end
  668. def test_strftime_flags
  669. t = Time.mktime(2001, 10, 1, 2, 0, 0)
  670. assert_equal("01", t.strftime("%d"))
  671. assert_equal("01", t.strftime("%0d"))
  672. assert_equal(" 1", t.strftime("%_d"))
  673. assert_equal(" 1", t.strftime("%e"))
  674. assert_equal("01", t.strftime("%0e"))
  675. assert_equal(" 1", t.strftime("%_e"))
  676. assert_equal("AM", t.strftime("%p"))
  677. assert_equal("am", t.strftime("%#p"))
  678. assert_equal("am", t.strftime("%P"))
  679. assert_equal("AM", t.strftime("%#P"))
  680. assert_equal("02", t.strftime("%H"))
  681. assert_equal("02", t.strftime("%0H"))
  682. assert_equal(" 2", t.strftime("%_H"))
  683. assert_equal("02", t.strftime("%I"))
  684. assert_equal("02", t.strftime("%0I"))
  685. assert_equal(" 2", t.strftime("%_I"))
  686. assert_equal(" 2", t.strftime("%k"))
  687. assert_equal("02", t.strftime("%0k"))
  688. assert_equal(" 2", t.strftime("%_k"))
  689. assert_equal(" 2", t.strftime("%l"))
  690. assert_equal("02", t.strftime("%0l"))
  691. assert_equal(" 2", t.strftime("%_l"))
  692. t = Time.mktime(2001, 10, 1, 14, 0, 0)
  693. assert_equal("PM", t.strftime("%p"))
  694. assert_equal("pm", t.strftime("%#p"))
  695. assert_equal("pm", t.strftime("%P"))
  696. assert_equal("PM", t.strftime("%#P"))
  697. assert_equal("14", t.strftime("%H"))
  698. assert_equal("14", t.strftime("%0H"))
  699. assert_equal("14", t.strftime("%_H"))
  700. assert_equal("02", t.strftime("%I"))
  701. assert_equal("02", t.strftime("%0I"))
  702. assert_equal(" 2", t.strftime("%_I"))
  703. assert_equal("14", t.strftime("%k"))
  704. assert_equal("14", t.strftime("%0k"))
  705. assert_equal("14", t.strftime("%_k"))
  706. assert_equal(" 2", t.strftime("%l"))
  707. assert_equal("02", t.strftime("%0l"))
  708. assert_equal(" 2", t.strftime("%_l"))
  709. assert_equal("MON", t.strftime("%^a"))
  710. assert_equal("OCT", t.strftime("%^b"))
  711. t = get_t2000
  712. assert_equal("UTC", t.strftime("%^Z"))
  713. assert_equal("utc", t.strftime("%#Z"))
  714. assert_equal("SAT JAN 1 00:00:00 2000", t.strftime("%^c"))
  715. end
  716. def test_strftime_invalid_flags
  717. t = Time.mktime(2001, 10, 1, 2, 0, 0)
  718. assert_equal("%4^p", t.strftime("%4^p"), 'prec after flag')
  719. end
  720. def test_strftime_year
  721. t = Time.utc(1,1,4)
  722. assert_equal("0001", t.strftime("%Y"))
  723. assert_equal("0001", t.strftime("%G"))
  724. t = Time.utc(0,1,4)
  725. assert_equal("0000", t.strftime("%Y"))
  726. assert_equal("0000", t.strftime("%G"))
  727. t = Time.utc(-1,1,4)
  728. assert_equal("-0001", t.strftime("%Y"))
  729. assert_equal("-0001", t.strftime("%G"))
  730. t = Time.utc(10000000000000000000000,1,1)
  731. assert_equal("<<10000000000000000000000>>", t.strftime("<<%Y>>"))
  732. assert_equal("<<010000000000000000000000>>", t.strftime("<<%24Y>>"))
  733. assert_equal("<<010000000000000000000000>>", t.strftime("<<%024Y>>"))
  734. assert_equal("<< 10000000000000000000000>>", t.strftime("<<%_24Y>>"))
  735. end
  736. def test_strftime_weeknum
  737. # [ruby-dev:37155]
  738. t = Time.mktime(1970, 1, 18)
  739. assert_equal("0", t.strftime("%w"))
  740. assert_equal("7", t.strftime("%u"))
  741. end
  742. def test_strftime_ctrlchar
  743. # [ruby-dev:37160]
  744. t2000 = get_t2000
  745. assert_equal("\t", t2000.strftime("%t"))
  746. assert_equal("\t", t2000.strftime("%0t"))
  747. assert_equal("\t", t2000.strftime("%1t"))
  748. assert_equal(" \t", t2000.strftime("%3t"))
  749. assert_equal("00\t", t2000.strftime("%03t"))
  750. assert_equal("\n", t2000.strftime("%n"))
  751. assert_equal("\n", t2000.strftime("%0n"))
  752. assert_equal("\n", t2000.strftime("%1n"))
  753. assert_equal(" \n", t2000.strftime("%3n"))
  754. assert_equal("00\n", t2000.strftime("%03n"))
  755. end
  756. def test_strftime_weekflags
  757. # [ruby-dev:37162]
  758. t2000 = get_t2000
  759. assert_equal("SAT", t2000.strftime("%#a"))
  760. assert_equal("SATURDAY", t2000.strftime("%#A"))
  761. assert_equal("JAN", t2000.strftime("%#b"))
  762. assert_equal("JANUARY", t2000.strftime("%#B"))
  763. assert_equal("JAN", t2000.strftime("%#h"))
  764. assert_equal("FRIDAY", Time.local(2008,1,4).strftime("%#A"))
  765. end
  766. def test_strftime_rational
  767. t = Time.utc(2000,3,14, 6,53,"58.979323846".to_r) # Pi Day
  768. assert_equal("03/14/2000 6:53:58.97932384600000000000000000000",
  769. t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
  770. assert_equal("03/14/2000 6:53:58.9793238460",
  771. t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
  772. assert_equal("03/14/2000 6:53:58.979323846",
  773. t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
  774. assert_equal("03/14/2000 6:53:58.97932384",
  775. t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
  776. t = Time.utc(1592,3,14, 6,53,"58.97932384626433832795028841971".to_r) # Pi Day
  777. assert_equal("03/14/1592 6:53:58.97932384626433832795028841971",
  778. t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
  779. assert_equal("03/14/1592 6:53:58.9793238462",
  780. t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
  781. assert_equal("03/14/1592 6:53:58.979323846",
  782. t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
  783. assert_equal("03/14/1592 6:53:58.97932384",
  784. t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
  785. end
  786. def test_strftime_far_future
  787. # [ruby-core:33985]
  788. assert_equal("3000000000", Time.at(3000000000).strftime('%s'))
  789. end
  790. def test_strftime_too_wide
  791. assert_equal(8192, Time.now.strftime('%8192z').size)
  792. end
  793. def test_strftime_wide_precision
  794. t2000 = get_t2000
  795. s = t2000.strftime("%28c")
  796. assert_equal(28, s.size)
  797. assert_equal(t2000.strftime("%c"), s.strip)
  798. end
  799. def test_strfimte_zoneoffset
  800. t2000 = get_t2000
  801. t = t2000.getlocal("+09:00:00")
  802. assert_equal("+0900", t.strftime("%z"))
  803. assert_equal("+09:00", t.strftime("%:z"))
  804. assert_equal("+09:00:00", t.strftime("%::z"))
  805. assert_equal("+09", t.strftime("%:::z"))
  806. t = t2000.getlocal("+09:00:01")
  807. assert_equal("+0900", t.strftime("%z"))
  808. assert_equal("+09:00", t.strftime("%:z"))
  809. assert_equal("+09:00:01", t.strftime("%::z"))
  810. assert_equal("+09:00:01", t.strftime("%:::z"))
  811. end
  812. def test_strftime_padding
  813. bug4458 = '[ruby-dev:43287]'
  814. t2000 = get_t2000
  815. t = t2000.getlocal("+09:00")
  816. assert_equal("+0900", t.strftime("%z"))
  817. assert_equal("+09:00", t.strftime("%:z"))
  818. assert_equal(" +900", t.strftime("%_10z"), bug4458)
  819. assert_equal("+000000900", t.strftime("%10z"), bug4458)
  820. assert_equal(" +9:00", t.strftime("%_10:z"), bug4458)
  821. assert_equal("+000009:00", t.strftime("%10:z"), bug4458)
  822. assert_equal(" +9:00:00", t.strftime("%_10::z"), bug4458)
  823. assert_equal("+009:00:00", t.strftime("%10::z"), bug4458)
  824. assert_equal("+000000009", t.strftime("%10:::z"))
  825. t = t2000.getlocal("-05:00")
  826. assert_equal("-0500", t.strftime("%z"))
  827. assert_equal("-05:00", t.strftime("%:z"))
  828. assert_equal(" -500", t.strftime("%_10z"), bug4458)
  829. assert_equal("-000000500", t.strftime("%10z"), bug4458)
  830. assert_equal(" -5:00", t.strftime("%_10:z"), bug4458)
  831. assert_equal("-000005:00", t.strftime("%10:z"), bug4458)
  832. assert_equal(" -5:00:00", t.strftime("%_10::z"), bug4458)
  833. assert_equal("-005:00:00", t.strftime("%10::z"), bug4458)
  834. assert_equal("-000000005", t.strftime("%10:::z"))
  835. bug6323 = '[ruby-core:44447]'
  836. t = t2000.getlocal("+00:36")
  837. assert_equal(" +036", t.strftime("%_10z"), bug6323)
  838. assert_equal("+000000036", t.strftime("%10z"), bug6323)
  839. assert_equal(" +0:36", t.strftime("%_10:z"), bug6323)
  840. assert_equal("+000000:36", t.strftime("%10:z"), bug6323)
  841. assert_equal(" +0:36:00", t.strftime("%_10::z"), bug6323)
  842. assert_equal("+000:36:00", t.strftime("%10::z"), bug6323)
  843. assert_equal("+000000:36", t.strftime("%10:::z"))
  844. t = t2000.getlocal("-00:55")
  845. assert_equal(" -055", t.strftime("%_10z"), bug6323)
  846. assert_equal("-000000055", t.strftime("%10z"), bug6323)
  847. assert_equal(" -0:55", t.strftime("%_10:z"), bug6323)
  848. assert_equal("-000000:55", t.strftime("%10:z"), bug6323)
  849. assert_equal(" -0:55:00", t.strftime("%_10::z"), bug6323)
  850. assert_equal("-000:55:00", t.strftime("%10::z"), bug6323)
  851. assert_equal("-000000:55", t.strftime("%10:::z"))
  852. end
  853. def test_strftime_invalid_modifier
  854. t2000 = get_t2000
  855. t = t2000.getlocal("+09:00")
  856. assert_equal("%:y", t.strftime("%:y"), 'invalid conversion after : modifier')
  857. assert_equal("%:0z", t.strftime("%:0z"), 'flag after : modifier')
  858. assert_equal("%:10z", t.strftime("%:10z"), 'prec after : modifier')
  859. assert_equal("%Ob", t.strftime("%Ob"), 'invalid conversion after locale modifier')
  860. assert_equal("%Eb", t.strftime("%Eb"), 'invalid conversion after locale modifier')
  861. assert_equal("%O0y", t.strftime("%O0y"), 'flag after locale modifier')
  862. assert_equal("%E0y", t.strftime("%E0y"), 'flag after locale modifier')
  863. assert_equal("%O10y", t.strftime("%O10y"), 'prec after locale modifier')
  864. assert_equal("%E10y", t.strftime("%E10y"), 'prec after locale modifier')
  865. end
  866. def test_delegate
  867. d1 = SimpleDelegator.new(t1 = Time.utc(2000))
  868. d2 = SimpleDelegator.new(t2 = Time.utc(2001))
  869. assert_equal(-1, t1 <=> t2)
  870. assert_equal(1, t2 <=> t1)
  871. assert_equal(-1, d1 <=> d2)
  872. assert_equal(1, d2 <=> d1)
  873. end
  874. def test_to_r
  875. assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r)
  876. assert_kind_of(Rational, Time.utc(1970).to_r)
  877. end
  878. def test_round
  879. t = Time.utc(1999,12,31, 23,59,59)
  880. t2 = (t+0.4).round
  881. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  882. assert_equal(0, t2.subsec)
  883. t2 = (t+0.49).round
  884. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  885. assert_equal(0, t2.subsec)
  886. t2 = (t+0.5).round
  887. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  888. assert_equal(0, t2.subsec)
  889. t2 = (t+1.4).round
  890. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  891. assert_equal(0, t2.subsec)
  892. t2 = (t+1.49).round
  893. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  894. assert_equal(0, t2.subsec)
  895. t2 = (t+1.5).round
  896. assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  897. assert_equal(0, t2.subsec)
  898. t2 = (t+0.123456789).round(4)
  899. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  900. assert_equal(Rational(1235,10000), t2.subsec)
  901. off = 0.0
  902. 100.times {|i|
  903. t2 = (t+off).round(1)
  904. assert_equal(Rational(i % 10, 10), t2.subsec)
  905. off += 0.1
  906. }
  907. end
  908. def test_floor
  909. t = Time.utc(1999,12,31, 23,59,59)
  910. t2 = (t+0.4).floor
  911. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  912. assert_equal(0, t2.subsec)
  913. t2 = (t+0.49).floor
  914. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  915. assert_equal(0, t2.subsec)
  916. t2 = (t+0.5).floor
  917. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  918. assert_equal(0, t2.subsec)
  919. t2 = (t+1.4).floor
  920. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  921. assert_equal(0, t2.subsec)
  922. t2 = (t+1.49).floor
  923. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  924. assert_equal(0, t2.subsec)
  925. t2 = (t+1.5).floor
  926. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  927. assert_equal(0, t2.subsec)
  928. t2 = (t+0.123456789).floor(4)
  929. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  930. assert_equal(Rational(1234,10000), t2.subsec)
  931. end
  932. def test_ceil
  933. t = Time.utc(1999,12,31, 23,59,59)
  934. t2 = (t+0.4).ceil
  935. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  936. assert_equal(0, t2.subsec)
  937. t2 = (t+0.49).ceil
  938. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  939. assert_equal(0, t2.subsec)
  940. t2 = (t+0.5).ceil
  941. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  942. assert_equal(0, t2.subsec)
  943. t2 = (t+1.4).ceil
  944. assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  945. assert_equal(0, t2.subsec)
  946. t2 = (t+1.49).ceil
  947. assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  948. assert_equal(0, t2.subsec)
  949. t2 = (t+1.5).ceil
  950. assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  951. assert_equal(0, t2.subsec)
  952. t2 = (t+0.123456789).ceil(4)
  953. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  954. assert_equal(Rational(1235,10000), t2.subsec)
  955. end
  956. def test_getlocal_dont_share_eigenclass
  957. bug5012 = "[ruby-dev:44071]"
  958. t0 = Time.now
  959. class << t0; end
  960. t1 = t0.getlocal
  961. def t0.m
  962. 0
  963. end
  964. assert_raise(NoMethodError, bug5012) { t1.m }
  965. end
  966. def test_sec_str
  967. bug6193 = '[ruby-core:43569]'
  968. t = nil
  969. assert_nothing_raised(bug6193) {t = Time.new(2012, 1, 2, 3, 4, "5")}
  970. assert_equal(Time.new(2012, 1, 2, 3, 4, 5), t, bug6193)
  971. end
  972. def test_past
  973. [
  974. [-(1 << 100), 1, 1, 0, 0, 0],
  975. [-4000, 1, 1, 0, 0, 0],
  976. [-3000, 1, 1, 0, 0, 0],
  977. ].each {|year, mon, day, hour, min, sec|
  978. t = Time.utc(year, mon, day, hour, min, sec)
  979. assert_equal(year, t.year)
  980. assert_equal(mon, t.mon)
  981. assert_equal(day, t.day)
  982. assert_equal(hour, t.hour)
  983. assert_equal(min, t.min)
  984. assert_equal(sec, t.sec)
  985. }
  986. end
  987. def test_1901
  988. assert_equal(-0x80000001, Time.utc(1901, 12, 13, 20, 45, 51).tv_sec)
  989. [
  990. [1901, 12, 13, 20, 45, 50],
  991. [1901, 12, 13, 20, 45, 51],
  992. [1901, 12, 13, 20, 45, 52], # -0x80000000
  993. [1901, 12, 13, 20, 45, 53],
  994. ].each {|year, mon, day, hour, min, sec|
  995. t = Time.utc(year, mon, day, hour, min, sec)
  996. assert_equal(year, t.year)
  997. assert_equal(mon, t.mon)
  998. assert_equal(day, t.day)
  999. assert_equal(hour, t.hour)
  1000. assert_equal(min, t.min)
  1001. assert_equal(sec, t.sec)
  1002. }
  1003. end
  1004. def test_1970
  1005. assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec)
  1006. [
  1007. [1969, 12, 31, 23, 59, 59],
  1008. [1970, 1, 1, 0, 0, 0],
  1009. [1970, 1, 1, 0, 0, 1],
  1010. ].each {|year, mon, day, hour, min, sec|
  1011. t = Time.utc(year, mon, day, hour, min, sec)
  1012. assert_equal(year, t.year)
  1013. assert_equal(mon, t.mon)
  1014. assert_equal(day, t.day)
  1015. assert_equal(hour, t.hour)
  1016. assert_equal(min, t.min)
  1017. assert_equal(sec, t.sec)
  1018. }
  1019. end
  1020. def test_2038
  1021. # Giveup to try 2nd test because some state is changed.
  1022. skip if Minitest::Unit.current_repeat_count > 0
  1023. if no_leap_seconds?
  1024. assert_equal(0x80000000, Time.utc(2038, 1, 19, 3, 14, 8).tv_sec)
  1025. end
  1026. [
  1027. [2038, 1, 19, 3, 14, 7],
  1028. [2038, 1, 19, 3, 14, 8],
  1029. [2038, 1, 19, 3, 14, 9],
  1030. [2039, 1, 1, 0, 0, 0],
  1031. ].each {|year, mon, day, hour, min, sec|
  1032. t = Time.utc(year, mon, day, hour, min, sec)
  1033. assert_equal(year, t.year)
  1034. assert_equal(mon, t.mon)
  1035. assert_equal(day, t.day)
  1036. assert_equal(hour, t.hour)
  1037. assert_equal(min, t.min)
  1038. assert_equal(sec, t.sec)
  1039. }
  1040. assert_equal(Time.local(2038,3,1), Time.local(2038,2,29))
  1041. assert_equal(Time.local(2038,3,2), Time.local(2038,2,30))
  1042. assert_equal(Time.local(2038,3,3), Time.local(2038,2,31))
  1043. assert_equal(Time.local(2040,2,29), Time.local(2040,2,29))
  1044. assert_equal(Time.local(2040,3,1), Time.local(2040,2,30))
  1045. assert_equal(Time.local(2040,3,2), Time.local(2040,2,31))
  1046. n = 2 ** 64
  1047. n += 400 - n % 400 # n is over 2^64 and multiple of 400
  1048. assert_equal(Time.local(n,2,29),Time.local(n,2,29))
  1049. assert_equal(Time.local(n,3,1), Time.local(n,2,30))
  1050. assert_equal(Time.local(n,3,2), Time.local(n,2,31))
  1051. n += 100
  1052. assert_equal(Time.local(n,3,1), Time.local(n,2,29))
  1053. assert_equal(Time.local(n,3,2), Time.local(n,2,30))
  1054. assert_equal(Time.local(n,3,3), Time.local(n,2,31))
  1055. n += 4
  1056. assert_equal(Time.local(n,2,29),Time.local(n,2,29))
  1057. assert_equal(Time.local(n,3,1), Time.local(n,2,30))
  1058. assert_equal(Time.local(n,3,2), Time.local(n,2,31))
  1059. n += 1
  1060. assert_equal(Time.local(n,3,1), Time.local(n,2,29))
  1061. assert_equal(Time.local(n,3,2), Time.local(n,2,30))
  1062. assert_equal(Time.local(n,3,3), Time.local(n,2,31))
  1063. end
  1064. def test_future
  1065. [
  1066. [3000, 1, 1, 0, 0, 0],
  1067. [4000, 1, 1, 0, 0, 0],
  1068. [1 << 100, 1, 1, 0, 0, 0],
  1069. ].each {|year, mon, day, hour, min, sec|
  1070. t = Time.utc(year, mon, day, hour, min, sec)
  1071. assert_equal(year, t.year)
  1072. assert_equal(mon, t.mon)
  1073. assert_equal(day, t.day)
  1074. assert_equal(hour, t.hour)
  1075. assert_equal(min, t.min)
  1076. assert_equal(sec, t.sec)
  1077. }
  1078. end
  1079. def test_getlocal_nil
  1080. now = Time.now
  1081. now2 = nil
  1082. now3 = nil
  1083. assert_nothing_raised {
  1084. now2 = now.getlocal
  1085. now3 = now.getlocal(nil)
  1086. }
  1087. assert_equal now2, now3
  1088. assert_equal now2.zone, now3.zone
  1089. end
  1090. def test_strftime_yearday_on_last_day_of_year
  1091. t = Time.utc(2015, 12, 31, 0, 0, 0)
  1092. assert_equal("365", t.strftime("%j"))
  1093. t = Time.utc(2016, 12, 31, 0, 0, 0)
  1094. assert_equal("366", t.strftime("%j"))
  1095. t = Time.utc(2015, 12, 30, 20, 0, 0).getlocal("+05:00")
  1096. assert_equal("365", t.strftime("%j"))
  1097. t = Time.utc(2016, 12, 30, 20, 0, 0).getlocal("+05:00")
  1098. assert_equal("366", t.strftime("%j"))
  1099. t = Time.utc(2016, 1, 1, 1, 0, 0).getlocal("-05:00")
  1100. assert_equal("365", t.strftime("%j"))
  1101. t = Time.utc(2017, 1, 1, 1, 0, 0).getlocal("-05:00")
  1102. assert_equal("366", t.strftime("%j"))
  1103. end
  1104. def test_strftime_no_hidden_garbage
  1105. skip unless Thread.list.size == 1
  1106. fmt = %w(Y m d).map { |x| "%#{x}" }.join('-') # defeats optimization
  1107. t = Time.at(0).getutc
  1108. ObjectSpace.count_objects(res = {}) # creates strings on first call
  1109. GC.disable
  1110. before = ObjectSpace.count_objects(res)[:T_STRING]
  1111. val = t.strftime(fmt)
  1112. after = ObjectSpace.count_objects(res)[:T_STRING]
  1113. assert_equal before + 1, after, 'only new string is the created one'
  1114. assert_equal '1970-01-01', val
  1115. ensure
  1116. GC.enable
  1117. end
  1118. def test_num_exact_error
  1119. bad = EnvUtil.labeled_class("BadValue").new
  1120. x = EnvUtil.labeled_class("Inexact") do
  1121. def to_s; "Inexact"; end
  1122. define_method(:to_int) {bad}
  1123. define_method(:to_r) {bad}
  1124. end.new
  1125. assert_raise_with_message(TypeError, /Inexact/) {Time.at(x)}
  1126. end
  1127. def test_memsize
  1128. # Time objects are common in some code, try to keep them small
  1129. skip "Time object size test" if /^(?:i.?86|x86_64)-linux/ !~ RUBY_PLATFORM
  1130. skip "GC is in debug" if GC::INTERNAL_CONSTANTS[:DEBUG]
  1131. require 'objspace'
  1132. t = Time.at(0)
  1133. size = GC::INTERNAL_CONSTANTS[:RVALUE_SIZE]
  1134. case size
  1135. when 20 then expect = 50
  1136. when 24 then expect = 54
  1137. when 40 then expect = 86
  1138. when 48 then expect = 94
  1139. else
  1140. flunk "Unsupported RVALUE_SIZE=#{size}, update test_memsize"
  1141. end
  1142. assert_equal expect, ObjectSpace.memsize_of(t)
  1143. rescue LoadError => e
  1144. skip "failed to load objspace: #{e.message}"
  1145. end
  1146. end