PageRenderTime 29ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jruby-1.7.3/test/externals/ruby1.9/ruby/test_time.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 756 lines | 722 code | 30 blank | 4 comment | 5 complexity | a6c88c48933ce999ec68ffb085601c57 MD5 | raw file
  1. require 'test/unit'
  2. require 'rational'
  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 test_new
  15. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, 3600*11))
  16. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,9, 13,0,0, -3600*11))
  17. assert_equal(Time.utc(2000,2,10), Time.new(2000,2,10, 11,0,0, "+11:00"))
  18. assert_equal(Rational(1,2), Time.new(2000,2,10, 11,0,5.5, "+11:00").subsec)
  19. bug4090 = '[ruby-dev:42631]'
  20. tm = [2001,2,28,23,59,30]
  21. t = Time.new(*tm, "-12:00")
  22. 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)
  23. end
  24. def test_time_add()
  25. assert_equal(Time.utc(2000, 3, 21, 3, 30) + 3 * 3600,
  26. Time.utc(2000, 3, 21, 6, 30))
  27. assert_equal(Time.utc(2000, 3, 21, 3, 30) + (-3 * 3600),
  28. Time.utc(2000, 3, 21, 0, 30))
  29. assert_equal(0, (Time.at(1.1) + 0.9).usec)
  30. assert((Time.utc(2000, 4, 1) + 24).utc?)
  31. assert(!(Time.local(2000, 4, 1) + 24).utc?)
  32. t = Time.new(2000, 4, 1, 0, 0, 0, "+01:00") + 24
  33. assert(!t.utc?)
  34. assert_equal(3600, t.utc_offset)
  35. t = Time.new(2000, 4, 1, 0, 0, 0, "+02:00") + 24
  36. assert(!t.utc?)
  37. assert_equal(7200, t.utc_offset)
  38. end
  39. def test_time_subt()
  40. assert_equal(Time.utc(2000, 3, 21, 3, 30) - 3 * 3600,
  41. Time.utc(2000, 3, 21, 0, 30))
  42. assert_equal(Time.utc(2000, 3, 21, 3, 30) - (-3 * 3600),
  43. Time.utc(2000, 3, 21, 6, 30))
  44. assert_equal(900000, (Time.at(1.1) - 0.2).usec)
  45. end
  46. def test_time_time()
  47. assert_equal(Time.utc(2000, 3, 21, 3, 30) \
  48. -Time.utc(2000, 3, 21, 0, 30), 3*3600)
  49. assert_equal(Time.utc(2000, 3, 21, 0, 30) \
  50. -Time.utc(2000, 3, 21, 3, 30), -3*3600)
  51. end
  52. def negative_time_t?
  53. begin
  54. Time.at(-1)
  55. true
  56. rescue ArgumentError
  57. false
  58. end
  59. end
  60. def test_timegm
  61. if negative_time_t?
  62. assert_equal(-0x80000000, Time.utc(1901, 12, 13, 20, 45, 52).tv_sec)
  63. assert_equal(-2, Time.utc(1969, 12, 31, 23, 59, 58).tv_sec)
  64. assert_equal(-1, Time.utc(1969, 12, 31, 23, 59, 59).tv_sec)
  65. end
  66. assert_equal(0, Time.utc(1970, 1, 1, 0, 0, 0).tv_sec) # the Epoch
  67. assert_equal(1, Time.utc(1970, 1, 1, 0, 0, 1).tv_sec)
  68. assert_equal(31535999, Time.utc(1970, 12, 31, 23, 59, 59).tv_sec)
  69. assert_equal(31536000, Time.utc(1971, 1, 1, 0, 0, 0).tv_sec)
  70. assert_equal(78796799, Time.utc(1972, 6, 30, 23, 59, 59).tv_sec)
  71. # 1972-06-30T23:59:60Z is the first leap second.
  72. if Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59) == 1
  73. # no leap second.
  74. assert_equal(78796800, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  75. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  76. assert_equal(946684800, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  77. assert_equal(0x7fffffff, Time.utc(2038, 1, 19, 3, 14, 7).tv_sec)
  78. else
  79. # leap seconds supported.
  80. assert_equal(2, Time.utc(1972, 7, 1, 0, 0, 0) - Time.utc(1972, 6, 30, 23, 59, 59))
  81. assert_equal(78796800, Time.utc(1972, 6, 30, 23, 59, 60).tv_sec)
  82. assert_equal(78796801, Time.utc(1972, 7, 1, 0, 0, 0).tv_sec)
  83. assert_equal(78796802, Time.utc(1972, 7, 1, 0, 0, 1).tv_sec)
  84. assert_equal(946684822, Time.utc(2000, 1, 1, 0, 0, 0).tv_sec)
  85. end
  86. end
  87. def test_strtime
  88. t = nil
  89. assert_nothing_raised { t = Time.utc("2000", "1", "2" , "3", "4", "5") }
  90. assert_equal(Time.utc(2000,1,2,3,4,5), t)
  91. end
  92. def test_huge_difference
  93. if negative_time_t?
  94. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) - 0xffffffff, "[ruby-dev:22619]")
  95. assert_equal(Time.at(-0x80000000), Time.at(0x7fffffff) + (-0xffffffff))
  96. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) + 0xffffffff, "[ruby-dev:22619]")
  97. assert_equal(Time.at(0x7fffffff), Time.at(-0x80000000) - (-0xffffffff))
  98. end
  99. end
  100. def test_big_minus
  101. begin
  102. bigtime0 = Time.at(2**60)
  103. bigtime1 = Time.at(2**60+1)
  104. rescue RangeError
  105. return
  106. end
  107. assert_equal(1.0, bigtime1 - bigtime0)
  108. end
  109. def test_at
  110. assert_equal(100000, Time.at("0.1".to_r).usec)
  111. assert_equal(10000, Time.at("0.01".to_r).usec)
  112. assert_equal(1000, Time.at("0.001".to_r).usec)
  113. assert_equal(100, Time.at("0.0001".to_r).usec)
  114. assert_equal(10, Time.at("0.00001".to_r).usec)
  115. assert_equal(1, Time.at("0.000001".to_r).usec)
  116. assert_equal(100000000, Time.at("0.1".to_r).nsec)
  117. assert_equal(10000000, Time.at("0.01".to_r).nsec)
  118. assert_equal(1000000, Time.at("0.001".to_r).nsec)
  119. assert_equal(100000, Time.at("0.0001".to_r).nsec)
  120. assert_equal(10000, Time.at("0.00001".to_r).nsec)
  121. assert_equal(1000, Time.at("0.000001".to_r).nsec)
  122. assert_equal(100, Time.at("0.0000001".to_r).nsec)
  123. assert_equal(10, Time.at("0.00000001".to_r).nsec)
  124. assert_equal(1, Time.at("0.000000001".to_r).nsec)
  125. assert_equal(100000, Time.at(0.1).usec)
  126. assert_equal(10000, Time.at(0.01).usec)
  127. assert_equal(1000, Time.at(0.001).usec)
  128. assert_equal(100, Time.at(0.0001).usec)
  129. assert_equal(10, Time.at(0.00001).usec)
  130. assert_equal(3, Time.at(0.000003).usec)
  131. assert_equal(100000000, Time.at(0.1).nsec)
  132. assert_equal(10000000, Time.at(0.01).nsec)
  133. assert_equal(1000000, Time.at(0.001).nsec)
  134. assert_equal(100000, Time.at(0.0001).nsec)
  135. assert_equal(10000, Time.at(0.00001).nsec)
  136. assert_equal(3000, Time.at(0.000003).nsec)
  137. assert_equal(199, Time.at(0.0000002).nsec)
  138. assert_equal(10, Time.at(0.00000001).nsec)
  139. assert_equal(1, Time.at(0.000000001).nsec)
  140. assert_equal(0, Time.at(1e-10).nsec)
  141. assert_equal(0, Time.at(4e-10).nsec)
  142. assert_equal(0, Time.at(6e-10).nsec)
  143. assert_equal(1, Time.at(14e-10).nsec)
  144. assert_equal(1, Time.at(16e-10).nsec)
  145. if negative_time_t?
  146. assert_equal(999999999, Time.at(-1e-10).nsec)
  147. assert_equal(999999999, Time.at(-4e-10).nsec)
  148. assert_equal(999999999, Time.at(-6e-10).nsec)
  149. assert_equal(999999998, Time.at(-14e-10).nsec)
  150. assert_equal(999999998, Time.at(-16e-10).nsec)
  151. end
  152. t = Time.at(-4611686019).utc
  153. assert_equal(1823, t.year)
  154. t = Time.at(4611686018, 999999).utc
  155. assert_equal(2116, t.year)
  156. assert_equal("0.999999".to_r, t.subsec)
  157. t = Time.at(2**40 + "1/3".to_r, 9999999999999).utc
  158. assert_equal(36812, t.year)
  159. t = Time.at(-0x3fff_ffff_ffff_ffff)
  160. assert_equal(-146138510344, t.year)
  161. t = Time.at(-0x4000_0000_0000_0000)
  162. assert_equal(-146138510344, t.year)
  163. t = Time.at(-0x4000_0000_0000_0001)
  164. assert_equal(-146138510344, t.year)
  165. t = Time.at(-0x5000_0000_0000_0001)
  166. assert_equal(-182673138422, t.year)
  167. t = Time.at(-0x6000_0000_0000_0000)
  168. assert_equal(-219207766501, t.year)
  169. t = Time.at(0).utc
  170. assert_equal([1970,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  171. t = Time.at(-86400).utc
  172. assert_equal([1969,12,31, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  173. t = Time.at(-86400 * (400 * 365 + 97)).utc
  174. assert_equal([1970-400,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  175. t = Time.at(-86400 * (400 * 365 + 97)*1000).utc
  176. assert_equal([1970-400*1000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  177. t = Time.at(-86400 * (400 * 365 + 97)*2421).utc
  178. assert_equal([1970-400*2421,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  179. t = Time.at(-86400 * (400 * 365 + 97)*1000000).utc
  180. assert_equal([1970-400*1000000,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  181. t = Time.at(-30613683110400).utc
  182. assert_equal([-968139,1,1, 0,0,0], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  183. t = Time.at(-30613683110401).utc
  184. assert_equal([-968140,12,31, 23,59,59], [t.year, t.mon, t.day, t.hour, t.min, t.sec])
  185. end
  186. def test_at2
  187. assert_equal(100, Time.at(0, 0.1).nsec)
  188. assert_equal(10, Time.at(0, 0.01).nsec)
  189. assert_equal(1, Time.at(0, 0.001).nsec)
  190. end
  191. def test_at_rational
  192. assert_equal(1, Time.at(Rational(1,1) / 1000000000).nsec)
  193. assert_equal(1, Time.at(1167609600 + Rational(1,1) / 1000000000).nsec)
  194. end
  195. def test_utc_subsecond
  196. assert_equal(500000, Time.utc(2007,1,1,0,0,1.5).usec)
  197. assert_equal(100000, Time.utc(2007,1,1,0,0,Rational(11,10)).usec)
  198. end
  199. def test_eq_nsec
  200. assert_equal(Time.at(0, 0.123), Time.at(0, 0.123))
  201. assert_not_equal(Time.at(0, 0.123), Time.at(0, 0.124))
  202. end
  203. def assert_marshal_roundtrip(t)
  204. iv_names = t.instance_variables
  205. iv_vals1 = iv_names.map {|n| t.instance_variable_get n }
  206. m = Marshal.dump(t)
  207. t2 = Marshal.load(m)
  208. iv_vals2 = iv_names.map {|n| t2.instance_variable_get n }
  209. assert_equal(t, t2)
  210. assert_equal(iv_vals1, iv_vals2)
  211. t2
  212. end
  213. def test_marshal_nsec
  214. assert_marshal_roundtrip(Time.at(0, 0.123))
  215. assert_marshal_roundtrip(Time.at(0, 0.120))
  216. end
  217. def test_marshal_nsec_191
  218. # generated by ruby 1.9.1p376
  219. m = "\x04\bIu:\tTime\r \x80\x11\x80@\xE2\x01\x00\x06:\rsubmicro\"\ax\x90"
  220. t = Marshal.load(m)
  221. assert_equal(Time.at(Rational(123456789, 1000000000)), t, "[ruby-dev:40133]")
  222. end
  223. def test_marshal_rational
  224. assert_marshal_roundtrip(Time.at(0, Rational(1,3)))
  225. assert_not_match(/Rational/, Marshal.dump(Time.at(0, Rational(1,3))))
  226. end
  227. def test_marshal_ivar
  228. t = Time.at(123456789, 987654.321)
  229. t.instance_eval { @var = 135 }
  230. assert_marshal_roundtrip(t)
  231. assert_marshal_roundtrip(Marshal.load(Marshal.dump(t)))
  232. end
  233. def test_marshal_timezone
  234. bug = '[ruby-dev:40063]'
  235. t1 = Time.gm(2000)
  236. m = Marshal.dump(t1.getlocal("-02:00"))
  237. t2 = Marshal.load(m)
  238. assert_equal(t1, t2)
  239. assert_equal(-7200, t2.utc_offset, bug)
  240. m = Marshal.dump(t1.getlocal("+08:15"))
  241. t2 = Marshal.load(m)
  242. assert_equal(t1, t2)
  243. assert_equal(29700, t2.utc_offset, bug)
  244. end
  245. def test_marshal_to_s
  246. t1 = Time.new(2011,11,8, 0,42,25, 9*3600)
  247. t2 = Time.at(Marshal.load(Marshal.dump(t1)))
  248. assert_equal("2011-11-08 00:42:25 +0900", t2.to_s,
  249. "[ruby-dev:44827] [Bug #5586]")
  250. end
  251. # Sat Jan 01 00:00:00 UTC 2000
  252. T2000 = Time.at(946684800).gmtime
  253. def test_security_error
  254. assert_raise(SecurityError) do
  255. Thread.new do
  256. t = Time.gm(2000)
  257. $SAFE = 4
  258. t.localtime
  259. end.join
  260. end
  261. end
  262. def test_at3
  263. assert_equal(T2000, Time.at(T2000))
  264. # assert_raise(RangeError) do
  265. # Time.at(2**31-1, 1_000_000)
  266. # Time.at(2**63-1, 1_000_000)
  267. # end
  268. # assert_raise(RangeError) do
  269. # Time.at(-2**31, -1_000_000)
  270. # Time.at(-2**63, -1_000_000)
  271. # end
  272. end
  273. def test_utc_or_local
  274. assert_equal(T2000, Time.gm(2000))
  275. assert_equal(T2000, Time.gm(0, 0, 0, 1, 1, 2000, :foo, :bar, false, :baz))
  276. assert_equal(T2000, Time.gm(2000, "jan"))
  277. assert_equal(T2000, Time.gm(2000, "1"))
  278. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, 0))
  279. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, 0, "0"))
  280. assert_equal(T2000, Time.gm(2000, 1, 1, 0, 0, "0", :foo, :foo))
  281. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1, :foo, :foo) }
  282. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -1.0, :foo, :foo) }
  283. assert_raise(RangeError) do
  284. Time.gm(2000, 1, 1, 0, 0, 10_000_000_000_000_000_001.0, :foo, :foo)
  285. end
  286. assert_raise(ArgumentError) { Time.gm(2000, 1, 1, 0, 0, -(2**31), :foo, :foo) }
  287. o = Object.new
  288. def o.to_r; nil; end
  289. assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  290. def o.to_r; ""; end
  291. assert_raise(TypeError) { Time.gm(2000, 1, 1, 0, 0, o, :foo, :foo) }
  292. def o.to_r; Rational(11); end
  293. assert_equal(11, Time.gm(2000, 1, 1, 0, 0, o).sec)
  294. o = Object.new
  295. def o.to_int; 10; end
  296. assert_equal(10, Time.gm(2000, 1, 1, 0, 0, o).sec)
  297. assert_raise(ArgumentError) { Time.gm(2000, 13) }
  298. t = Time.local(2000)
  299. assert_equal(t.gmt_offset, T2000 - t)
  300. assert_equal(-4427700000, Time.utc(-4427700000,12,1).year)
  301. assert_equal(-2**30+10, Time.utc(-2**30+10,1,1).year)
  302. end
  303. def test_time_interval
  304. m = Mutex.new.lock
  305. assert_nothing_raised {
  306. Timeout.timeout(10) {
  307. m.sleep(0)
  308. }
  309. }
  310. assert_raise(ArgumentError) { m.sleep(-1) }
  311. end
  312. def test_to_f
  313. assert_equal(946684800.0, T2000.to_f)
  314. end
  315. def test_cmp
  316. assert_equal(-1, T2000 <=> Time.gm(2001))
  317. assert_equal(1, T2000 <=> Time.gm(1999))
  318. assert_nil(T2000 <=> 0)
  319. end
  320. def test_eql
  321. assert(T2000.eql?(T2000))
  322. assert(!T2000.eql?(Time.gm(2001)))
  323. end
  324. def test_utc_p
  325. assert(Time.gm(2000).gmt?)
  326. assert(!Time.local(2000).gmt?)
  327. assert(!Time.at(0).gmt?)
  328. end
  329. def test_hash
  330. assert_kind_of(Integer, T2000.hash)
  331. end
  332. def test_init_copy
  333. assert_equal(T2000, T2000.dup)
  334. assert_raise(TypeError) do
  335. T2000.instance_eval { initialize_copy(nil) }
  336. end
  337. end
  338. def test_localtime_gmtime
  339. assert_nothing_raised do
  340. t = Time.gm(2000)
  341. assert(t.gmt?)
  342. t.localtime
  343. assert(!t.gmt?)
  344. t.localtime
  345. assert(!t.gmt?)
  346. t.gmtime
  347. assert(t.gmt?)
  348. t.gmtime
  349. assert(t.gmt?)
  350. end
  351. t1 = Time.gm(2000)
  352. t2 = t1.getlocal
  353. assert_equal(t1, t2)
  354. t3 = t1.getlocal("-02:00")
  355. assert_equal(t1, t3)
  356. assert_equal(-7200, t3.utc_offset)
  357. t1.localtime
  358. assert_equal(t1, t2)
  359. assert_equal(t1.gmt?, t2.gmt?)
  360. assert_equal(t1, t3)
  361. t1 = Time.local(2000)
  362. t2 = t1.getgm
  363. assert_equal(t1, t2)
  364. t3 = t1.getlocal("-02:00")
  365. assert_equal(t1, t3)
  366. assert_equal(-7200, t3.utc_offset)
  367. t1.gmtime
  368. assert_equal(t1, t2)
  369. assert_equal(t1.gmt?, t2.gmt?)
  370. assert_equal(t1, t3)
  371. end
  372. def test_asctime
  373. assert_equal("Sat Jan 1 00:00:00 2000", T2000.asctime)
  374. assert_kind_of(String, Time.at(0).asctime)
  375. end
  376. def test_to_s
  377. assert_equal("2000-01-01 00:00:00 UTC", T2000.to_s)
  378. assert_kind_of(String, Time.at(946684800).getlocal.to_s)
  379. assert_equal(Time.at(946684800).getlocal.to_s, Time.at(946684800).to_s)
  380. end
  381. def test_plus_minus_succ
  382. # assert_raise(RangeError) { T2000 + 10000000000 }
  383. # assert_raise(RangeError) T2000 - 3094168449 }
  384. # assert_raise(RangeError) { T2000 + 1200798848 }
  385. assert_raise(TypeError) { T2000 + Time.now }
  386. assert_equal(T2000 + 1, T2000.succ)
  387. end
  388. def test_plus_type
  389. t0 = Time.utc(2000,1,1)
  390. n0 = t0.to_i
  391. n1 = n0+1
  392. t1 = Time.at(n1)
  393. assert_equal(t1, t0 + 1)
  394. assert_equal(t1, t0 + 1.0)
  395. assert_equal(t1, t0 + Rational(1,1))
  396. assert_equal(t1, t0 + SimpleDelegator.new(1))
  397. assert_equal(t1, t0 + SimpleDelegator.new(1.0))
  398. assert_equal(t1, t0 + SimpleDelegator.new(Rational(1,1)))
  399. assert_raise(TypeError) { t0 + nil }
  400. assert_raise(TypeError) { t0 + "1" }
  401. assert_raise(TypeError) { t0 + SimpleDelegator.new("1") }
  402. assert_equal(0.5, (t0 + 1.5).subsec)
  403. assert_equal(Rational(1,3), (t0 + Rational(4,3)).subsec)
  404. assert_equal(0.5, (t0 + SimpleDelegator.new(1.5)).subsec)
  405. assert_equal(Rational(1,3), (t0 + SimpleDelegator.new(Rational(4,3))).subsec)
  406. end
  407. def test_minus
  408. t = Time.at(-4611686018).utc - 100
  409. assert_equal(1823, t.year)
  410. end
  411. def test_readers
  412. assert_equal(0, T2000.sec)
  413. assert_equal(0, T2000.min)
  414. assert_equal(0, T2000.hour)
  415. assert_equal(1, T2000.mday)
  416. assert_equal(1, T2000.mon)
  417. assert_equal(2000, T2000.year)
  418. assert_equal(6, T2000.wday)
  419. assert_equal(1, T2000.yday)
  420. assert_equal(false, T2000.isdst)
  421. assert_equal("UTC", T2000.zone)
  422. assert_equal(Encoding.find("locale"), T2000.zone.encoding)
  423. assert_equal(0, T2000.gmt_offset)
  424. assert(!T2000.sunday?)
  425. assert(!T2000.monday?)
  426. assert(!T2000.tuesday?)
  427. assert(!T2000.wednesday?)
  428. assert(!T2000.thursday?)
  429. assert(!T2000.friday?)
  430. assert(T2000.saturday?)
  431. assert_equal([0, 0, 0, 1, 1, 2000, 6, 1, false, "UTC"], T2000.to_a)
  432. t = Time.at(946684800).getlocal
  433. assert_equal(t.sec, Time.at(946684800).sec)
  434. assert_equal(t.min, Time.at(946684800).min)
  435. assert_equal(t.hour, Time.at(946684800).hour)
  436. assert_equal(t.mday, Time.at(946684800).mday)
  437. assert_equal(t.mon, Time.at(946684800).mon)
  438. assert_equal(t.year, Time.at(946684800).year)
  439. assert_equal(t.wday, Time.at(946684800).wday)
  440. assert_equal(t.yday, Time.at(946684800).yday)
  441. assert_equal(t.isdst, Time.at(946684800).isdst)
  442. assert_equal(t.zone, Time.at(946684800).zone)
  443. assert_equal(Encoding.find("locale"), Time.at(946684800).zone.encoding)
  444. assert_equal(t.gmt_offset, Time.at(946684800).gmt_offset)
  445. assert_equal(t.sunday?, Time.at(946684800).sunday?)
  446. assert_equal(t.monday?, Time.at(946684800).monday?)
  447. assert_equal(t.tuesday?, Time.at(946684800).tuesday?)
  448. assert_equal(t.wednesday?, Time.at(946684800).wednesday?)
  449. assert_equal(t.thursday?, Time.at(946684800).thursday?)
  450. assert_equal(t.friday?, Time.at(946684800).friday?)
  451. assert_equal(t.saturday?, Time.at(946684800).saturday?)
  452. assert_equal(t.to_a, Time.at(946684800).to_a)
  453. end
  454. def test_strftime
  455. t = Time.at(946684800).getlocal
  456. assert_equal("Sat", T2000.strftime("%a"))
  457. assert_equal("Saturday", T2000.strftime("%A"))
  458. assert_equal("Jan", T2000.strftime("%b"))
  459. assert_equal("January", T2000.strftime("%B"))
  460. assert_kind_of(String, T2000.strftime("%c"))
  461. assert_equal("01", T2000.strftime("%d"))
  462. assert_equal("00", T2000.strftime("%H"))
  463. assert_equal("12", T2000.strftime("%I"))
  464. assert_equal("001", T2000.strftime("%j"))
  465. assert_equal("01", T2000.strftime("%m"))
  466. assert_equal("00", T2000.strftime("%M"))
  467. assert_equal("AM", T2000.strftime("%p"))
  468. assert_equal("00", T2000.strftime("%S"))
  469. assert_equal("00", T2000.strftime("%U"))
  470. assert_equal("00", T2000.strftime("%W"))
  471. assert_equal("6", T2000.strftime("%w"))
  472. assert_equal("01/01/00", T2000.strftime("%x"))
  473. assert_equal("00:00:00", T2000.strftime("%X"))
  474. assert_equal("00", T2000.strftime("%y"))
  475. assert_equal("2000", T2000.strftime("%Y"))
  476. assert_equal("UTC", T2000.strftime("%Z"))
  477. assert_equal("%", T2000.strftime("%%"))
  478. assert_equal("0", T2000.strftime("%-S"))
  479. assert_equal("", T2000.strftime(""))
  480. assert_equal("foo\0bar\x0000\x0000\x0000", T2000.strftime("foo\0bar\0%H\0%M\0%S"))
  481. assert_equal("foo" * 1000, T2000.strftime("foo" * 1000))
  482. t = Time.mktime(2000, 1, 1)
  483. assert_equal("Sat", t.strftime("%a"))
  484. t = Time.at(946684800, 123456.789)
  485. assert_equal("123", t.strftime("%3N"))
  486. assert_equal("123456", t.strftime("%6N"))
  487. assert_equal("123456789", t.strftime("%9N"))
  488. assert_equal("1234567890", t.strftime("%10N"))
  489. assert_equal("123456789", t.strftime("%0N"))
  490. assert_equal("000", t.strftime("%3S"))
  491. assert_equal("946684800", t.strftime("%s"))
  492. assert_equal("946684800", t.utc.strftime("%s"))
  493. t = Time.mktime(2001, 10, 1)
  494. assert_equal("2001-10-01", t.strftime("%F"))
  495. t = Time.mktime(2001, 10, 1, 2, 0, 0)
  496. assert_equal("01", t.strftime("%d"))
  497. assert_equal("01", t.strftime("%0d"))
  498. assert_equal(" 1", t.strftime("%_d"))
  499. assert_equal(" 1", t.strftime("%e"))
  500. assert_equal("01", t.strftime("%0e"))
  501. assert_equal(" 1", t.strftime("%_e"))
  502. assert_equal("AM", t.strftime("%p"))
  503. assert_equal("am", t.strftime("%#p"))
  504. assert_equal("am", t.strftime("%P"))
  505. assert_equal("AM", t.strftime("%#P"))
  506. assert_equal("02", t.strftime("%H"))
  507. assert_equal("02", t.strftime("%0H"))
  508. assert_equal(" 2", t.strftime("%_H"))
  509. assert_equal("02", t.strftime("%I"))
  510. assert_equal("02", t.strftime("%0I"))
  511. assert_equal(" 2", t.strftime("%_I"))
  512. assert_equal(" 2", t.strftime("%k"))
  513. assert_equal("02", t.strftime("%0k"))
  514. assert_equal(" 2", t.strftime("%_k"))
  515. assert_equal(" 2", t.strftime("%l"))
  516. assert_equal("02", t.strftime("%0l"))
  517. assert_equal(" 2", t.strftime("%_l"))
  518. t = Time.mktime(2001, 10, 1, 14, 0, 0)
  519. assert_equal("PM", t.strftime("%p"))
  520. assert_equal("pm", t.strftime("%#p"))
  521. assert_equal("pm", t.strftime("%P"))
  522. assert_equal("PM", t.strftime("%#P"))
  523. assert_equal("14", t.strftime("%H"))
  524. assert_equal("14", t.strftime("%0H"))
  525. assert_equal("14", t.strftime("%_H"))
  526. assert_equal("02", t.strftime("%I"))
  527. assert_equal("02", t.strftime("%0I"))
  528. assert_equal(" 2", t.strftime("%_I"))
  529. assert_equal("14", t.strftime("%k"))
  530. assert_equal("14", t.strftime("%0k"))
  531. assert_equal("14", t.strftime("%_k"))
  532. assert_equal(" 2", t.strftime("%l"))
  533. assert_equal("02", t.strftime("%0l"))
  534. assert_equal(" 2", t.strftime("%_l"))
  535. t = Time.utc(1,1,4)
  536. assert_equal("0001", t.strftime("%Y"))
  537. assert_equal("0001", t.strftime("%G"))
  538. t = Time.utc(0,1,4)
  539. assert_equal("0000", t.strftime("%Y"))
  540. assert_equal("0000", t.strftime("%G"))
  541. t = Time.utc(-1,1,4)
  542. assert_equal("-0001", t.strftime("%Y"))
  543. assert_equal("-0001", t.strftime("%G"))
  544. # [ruby-dev:37155]
  545. t = Time.mktime(1970, 1, 18)
  546. assert_equal("0", t.strftime("%w"))
  547. assert_equal("7", t.strftime("%u"))
  548. # [ruby-dev:37160]
  549. assert_equal("\t", T2000.strftime("%t"))
  550. assert_equal("\t", T2000.strftime("%0t"))
  551. assert_equal("\t", T2000.strftime("%1t"))
  552. assert_equal(" \t", T2000.strftime("%3t"))
  553. assert_equal("00\t", T2000.strftime("%03t"))
  554. assert_equal("\n", T2000.strftime("%n"))
  555. assert_equal("\n", T2000.strftime("%0n"))
  556. assert_equal("\n", T2000.strftime("%1n"))
  557. assert_equal(" \n", T2000.strftime("%3n"))
  558. assert_equal("00\n", T2000.strftime("%03n"))
  559. # [ruby-dev:37162]
  560. assert_equal("SAT", T2000.strftime("%#a"))
  561. assert_equal("SATURDAY", T2000.strftime("%#A"))
  562. assert_equal("JAN", T2000.strftime("%#b"))
  563. assert_equal("JANUARY", T2000.strftime("%#B"))
  564. assert_equal("JAN", T2000.strftime("%#h"))
  565. assert_equal("FRIDAY", Time.local(2008,1,4).strftime("%#A"))
  566. t = Time.utc(2000,3,14, 6,53,"58.979323846".to_r) # Pi Day
  567. assert_equal("03/14/2000 6:53:58.97932384600000000000000000000",
  568. t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
  569. assert_equal("03/14/2000 6:53:58.9793238460",
  570. t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
  571. assert_equal("03/14/2000 6:53:58.979323846",
  572. t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
  573. assert_equal("03/14/2000 6:53:58.97932384",
  574. t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
  575. t = Time.utc(1592,3,14, 6,53,"58.97932384626433832795028841971".to_r) # Pi Day
  576. assert_equal("03/14/1592 6:53:58.97932384626433832795028841971",
  577. t.strftime("%m/%d/%Y %l:%M:%S.%29N"))
  578. assert_equal("03/14/1592 6:53:58.9793238462",
  579. t.strftime("%m/%d/%Y %l:%M:%S.%10N"))
  580. assert_equal("03/14/1592 6:53:58.979323846",
  581. t.strftime("%m/%d/%Y %l:%M:%S.%9N"))
  582. assert_equal("03/14/1592 6:53:58.97932384",
  583. t.strftime("%m/%d/%Y %l:%M:%S.%8N"))
  584. # [ruby-core:33985]
  585. assert_equal("3000000000", Time.at(3000000000).strftime('%s'))
  586. bug4457 = '[ruby-dev:43285]'
  587. assert_raise(Errno::ERANGE, bug4457) {Time.now.strftime('%8192z')}
  588. bug4458 = '[ruby-dev:43287]'
  589. t = T2000.getlocal("+09:00")
  590. assert_equal(" +900", t.strftime("%_10z"), bug4458)
  591. assert_equal("+000000900", t.strftime("%10z"), bug4458)
  592. assert_equal(" +9:00", t.strftime("%_:10z"), bug4458)
  593. assert_equal("+000009:00", t.strftime("%:10z"), bug4458)
  594. assert_equal(" +9:00:00", t.strftime("%_::10z"), bug4458)
  595. assert_equal("+009:00:00", t.strftime("%::10z"), bug4458)
  596. t = T2000.getlocal("-05:00")
  597. assert_equal(" -500", t.strftime("%_10z"), bug4458)
  598. assert_equal("-000000500", t.strftime("%10z"), bug4458)
  599. assert_equal(" -5:00", t.strftime("%_:10z"), bug4458)
  600. assert_equal("-000005:00", t.strftime("%:10z"), bug4458)
  601. assert_equal(" -5:00:00", t.strftime("%_::10z"), bug4458)
  602. assert_equal("-005:00:00", t.strftime("%::10z"), bug4458)
  603. bug6323 = '[ruby-core:44447]'
  604. t = T2000.getlocal("+00:36")
  605. assert_equal(" +036", t.strftime("%_10z"), bug6323)
  606. assert_equal("+000000036", t.strftime("%10z"), bug6323)
  607. assert_equal(" +0:36", t.strftime("%_:10z"), bug6323)
  608. assert_equal("+000000:36", t.strftime("%:10z"), bug6323)
  609. assert_equal(" +0:36:00", t.strftime("%_::10z"), bug6323)
  610. assert_equal("+000:36:00", t.strftime("%::10z"), bug6323)
  611. t = T2000.getlocal("-00:55")
  612. assert_equal(" -055", t.strftime("%_10z"), bug6323)
  613. assert_equal("-000000055", t.strftime("%10z"), bug6323)
  614. assert_equal(" -0:55", t.strftime("%_:10z"), bug6323)
  615. assert_equal("-000000:55", t.strftime("%:10z"), bug6323)
  616. assert_equal(" -0:55:00", t.strftime("%_::10z"), bug6323)
  617. assert_equal("-000:55:00", t.strftime("%::10z"), bug6323)
  618. end
  619. def test_delegate
  620. d1 = SimpleDelegator.new(t1 = Time.utc(2000))
  621. d2 = SimpleDelegator.new(t2 = Time.utc(2001))
  622. assert_equal(-1, t1 <=> t2)
  623. assert_equal(1, t2 <=> t1)
  624. assert_equal(-1, d1 <=> d2)
  625. assert_equal(1, d2 <=> d1)
  626. end
  627. def test_to_r
  628. assert_kind_of(Rational, Time.new(2000,1,1,0,0,Rational(4,3)).to_r)
  629. assert_kind_of(Rational, Time.utc(1970).to_r)
  630. end
  631. def test_round
  632. t = Time.utc(1999,12,31, 23,59,59)
  633. t2 = (t+0.4).round
  634. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  635. assert_equal(0, t2.subsec)
  636. t2 = (t+0.49).round
  637. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  638. assert_equal(0, t2.subsec)
  639. t2 = (t+0.5).round
  640. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  641. assert_equal(0, t2.subsec)
  642. t2 = (t+1.4).round
  643. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  644. assert_equal(0, t2.subsec)
  645. t2 = (t+1.49).round
  646. assert_equal([0,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  647. assert_equal(0, t2.subsec)
  648. t2 = (t+1.5).round
  649. assert_equal([1,0,0, 1,1,2000, 6,1,false,"UTC"], t2.to_a)
  650. assert_equal(0, t2.subsec)
  651. t2 = (t+0.123456789).round(4)
  652. assert_equal([59,59,23, 31,12,1999, 5,365,false,"UTC"], t2.to_a)
  653. assert_equal(Rational(1235,10000), t2.subsec)
  654. off = 0.0
  655. 100.times {|i|
  656. t2 = (t+off).round(1)
  657. assert_equal(Rational(i % 10, 10), t2.subsec)
  658. off += 0.1
  659. }
  660. end
  661. def test_getlocal_dont_share_eigenclass
  662. bug5012 = "[ruby-dev:44071]"
  663. t0 = Time.now
  664. class <<t0; end
  665. t1 = t0.getlocal
  666. def t0.m
  667. 0
  668. end
  669. assert_raise(NoMethodError, bug5012) { t1.m }
  670. end
  671. end