PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/jruby-1.7.3/test/rubicon/test_time.rb

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Ruby | 699 lines | 599 code | 57 blank | 43 comment | 5 complexity | 6112c36faac96e1806141d03683afff9 MD5 | raw file
  1. require 'test/unit'
  2. #
  3. # NOTICE: These tests assume that your local time zone is *not* GMT.
  4. #
  5. class T # ZenTest SKIP
  6. attr :orig
  7. attr :amt
  8. attr :result
  9. def initialize(a1, anAmt, a2)
  10. @orig = a1
  11. @amt = anAmt
  12. @result = a2
  13. end
  14. def to_s
  15. @orig.join("-")
  16. end
  17. end
  18. class TestTime < Test::Unit::TestCase
  19. ONEDAYSEC = 60 * 60 * 24
  20. #
  21. # Test month name to month number
  22. #
  23. @@months = {
  24. 'Jan' => 1,
  25. 'Feb' => 2,
  26. 'Mar' => 3,
  27. 'Apr' => 4,
  28. 'May' => 5,
  29. 'Jun' => 6,
  30. 'Jul' => 7,
  31. 'Aug' => 8,
  32. 'Sep' => 9,
  33. 'Oct' => 10,
  34. 'Nov' => 11,
  35. 'Dec' => 12
  36. }
  37. #
  38. # A random selection of interesting dates
  39. #
  40. @@dates = [
  41. # Source + amt == dest
  42. T.new([1999, 12, 31, 23,59,59], 1, [2000, 1, 1, 0,0,0]),
  43. T.new([2036, 12, 31, 23,59,59], 1, [2037, 1, 1, 0,0,0]),
  44. T.new([2000, 2, 28, 23,59,59], 1, [2000, 2, 29, 0,0,0]),
  45. T.new([1970, 2, 1, 0, 0, 0], ONEDAYSEC, [1970, 2, 2, 0,0,0]),
  46. T.new([2000, 7, 1, 0, 0, 0], 32 * ONEDAYSEC, [2000, 8, 2, 0,0,0]),
  47. T.new([2000, 1, 1, 0, 0, 0], 366 * ONEDAYSEC, [2001, 1, 1, 0,0,0]),
  48. T.new([2001, 1, 1, 0, 0, 0], 365 * ONEDAYSEC, [2002, 1, 1, 0,0,0]),
  49. T.new([2000, 1, 1, 0, 0, 0], 0, [2000, 1, 1, 0,0,0]),
  50. T.new([2000, 2, 1, 0, 0, 0], 0, [2000, 2, 1, 0,0,0]),
  51. T.new([2000, 3, 1, 0, 0, 0], 0, [2000, 3, 1, 0,0,0]),
  52. T.new([2000, 4, 1, 0, 0, 0], 0, [2000, 4, 1, 0,0,0]),
  53. T.new([2000, 5, 1, 0, 0, 0], 0, [2000, 5, 1, 0,0,0]),
  54. T.new([2000, 6, 1, 0, 0, 0], 0, [2000, 6, 1, 0,0,0]),
  55. T.new([2000, 7, 1, 0, 0, 0], 0, [2000, 7, 1, 0,0,0]),
  56. T.new([2000, 8, 1, 0, 0, 0], 0, [2000, 8, 1, 0,0,0]),
  57. T.new([2000, 9, 1, 0, 0, 0], 0, [2000, 9, 1, 0,0,0]),
  58. T.new([2000, 10, 1, 0, 0, 0], 0, [2000, 10, 1, 0,0,0]),
  59. T.new([2000, 11, 1, 0, 0, 0], 0, [2000, 11, 1, 0,0,0]),
  60. T.new([2000, 12, 1, 0, 0, 0], 0, [2000, 12, 1, 0,0,0]),
  61. T.new([2001, 1, 1, 0, 0, 0], 0, [2001, 1, 1, 0,0,0]),
  62. T.new([2001, 2, 1, 0, 0, 0], 0, [2001, 2, 1, 0,0,0]),
  63. T.new([2001, 3, 1, 0, 0, 0], 0, [2001, 3, 1, 0,0,0]),
  64. T.new([2001, 4, 1, 0, 0, 0], 0, [2001, 4, 1, 0,0,0]),
  65. T.new([2001, 5, 1, 0, 0, 0], 0, [2001, 5, 1, 0,0,0]),
  66. T.new([2001, 6, 1, 0, 0, 0], 0, [2001, 6, 1, 0,0,0]),
  67. T.new([2001, 7, 1, 0, 0, 0], 0, [2001, 7, 1, 0,0,0]),
  68. T.new([2001, 8, 1, 0, 0, 0], 0, [2001, 8, 1, 0,0,0]),
  69. T.new([2001, 9, 1, 0, 0, 0], 0, [2001, 9, 1, 0,0,0]),
  70. T.new([2001, 10, 1, 0, 0, 0], 0, [2001, 10, 1, 0,0,0]),
  71. T.new([2001, 11, 1, 0, 0, 0], 0, [2001, 11, 1, 0,0,0]),
  72. T.new([2001, 12, 1, 0, 0, 0], 0, [2001, 12, 1, 0,0,0]),
  73. ]
  74. def setup
  75. @orig_zone = ENV['TZ']
  76. ENV['TZ'] = 'PST8PDT'
  77. @utc = Time.utc(2001, 2, 3, 4, 5, 6)
  78. @loc = Time.local(2001, 2, 3, 4, 5, 6)
  79. @zone = @loc.zone
  80. end
  81. def teardown
  82. ENV['TZ'] = @orig_zone
  83. end
  84. ##
  85. # Check a particular date component -- m is the method (day, month, etc)
  86. # and i is the index in the date specifications above.
  87. def util_check_component(m, i)
  88. @@dates.each do |x|
  89. assert_equal(x.orig[i], Time.local(*x.orig).send(m))
  90. assert_equal(x.result[i], Time.local(*x.result).send(m))
  91. assert_equal(x.orig[i], Time.gm(*x.orig).send(m))
  92. assert_equal(x.result[i], Time.gm(*x.result).send(m))
  93. end
  94. end
  95. def util_class_now(method)
  96. min = 0.1
  97. max = min * 3.0 # some ruby impls will be SLOOOW
  98. t1 = Time.send(method)
  99. sleep min
  100. t2 = Time.send(method)
  101. delta = t2.to_f - t1.to_f
  102. assert(delta >= min, "time difference must be at least #{min}")
  103. assert(max >= delta, "time difference should not be more than #{max}")
  104. end
  105. def util_os_specific_epoch
  106. "Thu Jan 1 00:00:00 1970"
  107. end
  108. ##
  109. # If this test is failing, you've got big problems. Start with Time::at,
  110. # Time::utc and Time::local before looking at bugs in any of your other
  111. # code.
  112. def test_00sanity # ZenTest SKIP
  113. assert_equal(Time.at(981173106), Time.utc(2001, 2, 3, 4, 5, 6),
  114. "If this test fails, don't bother debugging anything else.")
  115. assert_equal(Time.at(981201906), Time.local(2001, 2, 3, 4, 5, 6),
  116. "If this test fails, don't bother debugging anything else.")
  117. end
  118. # Class methods:
  119. def test_class__load
  120. # TODO: raise NotImplementedError, 'Need to write test_class__load'
  121. end
  122. def test_class_at
  123. sec = @loc.to_i
  124. assert_equal(0, Time.at(0).to_i)
  125. assert_equal(@loc, Time.at(@loc))
  126. assert_in_delta(Time.at(sec,1_000_000).to_f, Time.at(sec).to_f, 1.0)
  127. # no arguments ==> error
  128. assert_raise(ArgumentError) do
  129. Time.at
  130. end
  131. # one integer argument ==> seconds
  132. t = Time.at(1_234_567)
  133. assert_equal(1_234_567, t.tv_sec)
  134. assert_equal( 0, t.tv_usec)
  135. # two integer arguments ==> seconds & microseconds
  136. t = Time.at(1_234_567, 888_999)
  137. assert_equal(1_234_567, t.tv_sec)
  138. assert_equal( 888_999, t.tv_usec)
  139. # float argument ==> second & rounded microseconds
  140. t = Time.at(1_234_567.5)
  141. assert_equal(1_234_567, t.tv_sec)
  142. assert_equal( 500_000, t.tv_usec)
  143. # float + integer arguments ==> rounded seconds & microseconds
  144. t = Time.at(1_234_567.5, 300_000)
  145. assert_equal(1_234_567, t.tv_sec)
  146. assert_equal( 300_000, t.tv_usec)
  147. # Time argument
  148. t1 = Time.at(1_234_567, 888_999)
  149. t2 = Time.at(t1)
  150. assert_equal(1_234_567, t2.tv_sec)
  151. assert_equal( 888_999, t2.tv_usec)
  152. end
  153. def test_class_at_utc
  154. utc1 = @utc
  155. utc2 = Time.at(@utc)
  156. assert(utc1.utc?)
  157. assert(utc2.utc?)
  158. assert_equal(utc1.to_i, utc2.to_i)
  159. end
  160. def test_class_gm
  161. assert_raise(ArgumentError) { Time.gm }
  162. assert_not_equal(Time.gm(2000), Time.local(2000))
  163. assert_equal(Time.gm(2000), Time.gm(2000,1,1,0,0,0))
  164. assert_equal(Time.gm(2000,nil,nil,nil,nil,nil), Time.gm(2000,1,1,0,0,0))
  165. assert_raise(ArgumentError) { Time.gm(2000,0) }
  166. assert_raise(ArgumentError) { Time.gm(2000,13) }
  167. assert_raise(ArgumentError) { Time.gm(2000,1,1,24) }
  168. Time.gm(2000,1,1,23)
  169. @@months.each do |month, num|
  170. assert_equal(Time.gm(2000,month), Time.gm(2000,num,1,0,0,0))
  171. assert_equal(Time.gm(1970,month), Time.gm(1970,num,1,0,0,0))
  172. assert_equal(Time.gm(2037,month), Time.gm(2037,num,1,0,0,0))
  173. end
  174. t = Time.gm(2000,1,1)
  175. a = t.to_a
  176. assert_equal(Time.gm(*a),t)
  177. end
  178. def test_class_local
  179. assert_raise(ArgumentError) { Time.local }
  180. assert_not_equal(Time.gm(2000), Time.local(2000))
  181. assert_equal(Time.local(2000), Time.local(2000,1,1,0,0,0))
  182. assert_equal(Time.local(2000,nil,nil,nil,nil,nil), Time.local(2000,1,1,0,0,0))
  183. assert_raise(ArgumentError) { Time.local(2000,0) }
  184. assert_raise(ArgumentError) { Time.local(2000,13) }
  185. assert_raise(ArgumentError) { Time.local(2000,1,1,24) }
  186. Time.local(2000,1,1,23)
  187. @@months.each do |month, num|
  188. assert_equal(Time.local(2000,month), Time.local(2000,num,1,0,0,0))
  189. assert_equal(Time.local(1971,month), Time.local(1971,num,1,0,0,0))
  190. assert_equal(Time.local(2037,month), Time.local(2037,num,1,0,0,0))
  191. end
  192. t = Time.local(2000,1,1)
  193. a = t.to_a
  194. assert_equal(Time.local(*a),t)
  195. end
  196. def test_class_mktime
  197. #
  198. # Test insufficient arguments
  199. #
  200. assert_raise(ArgumentError) { Time.mktime }
  201. assert_not_equal(Time.gm(2000), Time.mktime(2000))
  202. assert_equal(Time.mktime(2000), Time.mktime(2000,1,1,0,0,0))
  203. assert_equal(Time.mktime(2000,nil,nil,nil,nil,nil), Time.mktime(2000,1,1,0,0,0))
  204. assert_raise(ArgumentError) { Time.mktime(2000,0) }
  205. assert_raise(ArgumentError) { Time.mktime(2000,13) }
  206. assert_raise(ArgumentError) { Time.mktime(2000,1,1,24) }
  207. Time.mktime(2000,1,1,23)
  208. #
  209. # Make sure spelled-out month names work
  210. #
  211. @@months.each do |month, num|
  212. assert_equal(Time.mktime(2000,month), Time.mktime(2000,num,1,0,0,0))
  213. assert_equal(Time.mktime(1971,month), Time.mktime(1971,num,1,0,0,0))
  214. assert_equal(Time.mktime(2037,month), Time.mktime(2037,num,1,0,0,0))
  215. end
  216. t = Time.mktime(2000,1,1)
  217. a = t.to_a
  218. assert_equal(Time.mktime(*a),t)
  219. end
  220. def test_class_now
  221. util_class_now(:now) # Time.now
  222. end
  223. def test_class_times
  224. assert_instance_of(Struct::Tms, Process.times)
  225. end
  226. def test_class_utc
  227. test_class_gm # TODO: refactor to ensure they really are synonyms
  228. end
  229. # Instance Methods:
  230. def test__dump
  231. # TODO: raise NotImplementedError, 'Need to write test__dump'
  232. end
  233. def test_asctime
  234. expected = util_os_specific_epoch
  235. assert_equal(expected, Time.at(0).gmtime.asctime)
  236. end
  237. def test_clone
  238. for taint in [ false, true ]
  239. for frozen in [ false, true ]
  240. a = @loc.dup
  241. a.taint if taint
  242. a.freeze if frozen
  243. b = a.clone
  244. assert_equal(a, b)
  245. assert_not_equal(a.__id__, b.__id__)
  246. assert_equal(a.frozen?, b.frozen?)
  247. assert_equal(a.tainted?, b.tainted?)
  248. end
  249. end
  250. end
  251. def test_ctime
  252. expected = util_os_specific_epoch
  253. assert_equal(expected, Time.at(0).gmtime.ctime)
  254. end
  255. def test_day
  256. util_check_component(:day, 2)
  257. end
  258. def test_dst_eh
  259. test_isdst # TODO: refactor to test that they really are the same
  260. end
  261. def test_eql_eh
  262. t1 = @loc
  263. t2 = Time.at(t1)
  264. t3 = t1 + 2e-6
  265. t4 = t1 + 1
  266. assert(t1.eql?(t1))
  267. assert(t1.eql?(t2))
  268. assert(!t1.eql?(t3))
  269. assert(!t1.eql?(t4))
  270. assert(t1.eql?(t1.getutc))
  271. end
  272. def test_getgm
  273. # TODO: this only tests local -> gm
  274. t1 = @loc
  275. loc = Time.at(t1)
  276. assert(!t1.gmt?)
  277. t2 = t1.getgm
  278. assert(!t1.gmt?)
  279. assert(t2.gmt?)
  280. assert_equal(t1, loc)
  281. assert_equal(t1.asctime, loc.asctime)
  282. assert_not_equal(t2.asctime, loc.asctime)
  283. assert_not_equal(t1.asctime, t2.asctime)
  284. assert_equal(t1, t2)
  285. end
  286. def test_getlocal
  287. # TODO: this only tests gm -> local
  288. t1 = @utc
  289. utc = Time.at(t1)
  290. assert(t1.gmt?)
  291. t2 = t1.getlocal
  292. assert(t1.gmt?)
  293. assert(!t2.gmt?)
  294. assert_equal(t1, utc)
  295. assert_equal(t1.asctime, utc.asctime)
  296. assert_not_equal(t2.asctime, utc.asctime)
  297. assert_not_equal(t1.asctime, t2.asctime)
  298. assert_equal(t1, t2)
  299. end
  300. def test_getutc
  301. test_getgm # REFACTOR to test both calls
  302. end
  303. def test_gmt_eh
  304. assert(!@loc.gmt?)
  305. assert(@utc.gmt?)
  306. assert(!Time.local(2000).gmt?)
  307. assert(Time.gm(2000).gmt?)
  308. end
  309. def test_gmt_offset
  310. test_utc_offset # REFACTOR to test both methods
  311. end
  312. def test_gmtime
  313. # TODO: this only tests local -> gm
  314. t = @loc
  315. loc = Time.at(t)
  316. assert(!t.gmt?)
  317. t.gmtime
  318. assert(t.gmt?)
  319. assert_not_equal(t.asctime, loc.asctime)
  320. end
  321. def test_gmtoff
  322. test_utc_offset # REFACTOR to test both methods
  323. end
  324. def test_hash
  325. t1 = @utc
  326. t2 = Time.at(t1)
  327. t3 = @utc + 1
  328. assert_equal(t1.hash, t2.hash)
  329. assert_not_equal(t1.hash, t3.hash)
  330. end
  331. def test_hour
  332. util_check_component(:hour, 3)
  333. end
  334. def test_initialize
  335. util_class_now(:new) # Time.new
  336. end
  337. def test_inspect
  338. assert_equal("Sat Feb 03 04:05:06 UTC 2001", @utc.inspect)
  339. assert_equal("Sat Feb 03 04:05:06 -0800 2001", @loc.inspect)
  340. end
  341. def test_isdst
  342. # This code is problematic: how do I find out the exact
  343. # date and time of the dst switch for all the possible
  344. # timezones in which this code runs? For now, I'll just check
  345. # midvalues, and add boundary checks for the US. I know this won't
  346. # work in some parts of the US, even, so I'm looking for
  347. # better ideas
  348. # Are we in the US?
  349. if ["EST", "EDT",
  350. "CST", "CDT",
  351. "MST", "MDT",
  352. "PST", "PDT"].include? @zone
  353. dtest = [
  354. [false, 2000, 1, 1],
  355. [true, 2000, 7, 1],
  356. [true, 2000, 4, 2, 4],
  357. [false, 2000, 10, 29, 4],
  358. [false, 2000, 4,2,1,59], # Spring forward
  359. [true, 2000, 4,2,3,0],
  360. [true, 2000, 10,29,0,59], # Fall back
  361. [false, 2000, 10,29,2,0]
  362. ]
  363. dtest.each do |x|
  364. result = x.shift
  365. assert_equal(result, Time.local(*x).isdst,
  366. "\nExpected Time.local(#{x.join(',')}).isdst == #{result}")
  367. end
  368. else
  369. skipping("Don't know how to do timezones");
  370. end
  371. end
  372. def test_localtime
  373. # TODO: this only tests gm -> local
  374. t = @utc
  375. utc = Time.at(t)
  376. assert(t.gmt?)
  377. t.localtime
  378. assert(!t.gmt?)
  379. assert_not_equal(t.asctime, utc.asctime)
  380. end
  381. def test_mday
  382. util_check_component(:mday, 2)
  383. end
  384. def test_min
  385. util_check_component(:min, 4)
  386. end
  387. def test_minus # '-'
  388. @@dates.each do |x|
  389. # Check subtracting an amount in seconds
  390. assert_equal(Time.local(*x.result) - x.amt, Time.local(*x.orig))
  391. assert_equal(Time.gm(*x.result) - x.amt, Time.gm(*x.orig))
  392. # Check subtracting two times
  393. assert_equal(Time.local(*x.result) - Time.local(*x.orig), x.amt)
  394. assert_equal(Time.gm(*x.result) - Time.gm(*x.orig), x.amt)
  395. end
  396. # integer argument
  397. t1 = Time.at(1_234_567, 500_000)
  398. t2 = t1 - 567
  399. assert_equal( 1_234_000, t2.tv_sec)
  400. assert_equal( 500_000, t2.tv_usec)
  401. # float argument with fractional part
  402. t1 = Time.at(1_234_567, 500_000)
  403. t2 = t1 - 566.75
  404. assert_equal( 1_234_000, t2.tv_sec)
  405. assert_equal( 750_000, t2.tv_usec)
  406. # Time argument
  407. t1 = Time.at(1_234_000, 750_000)
  408. t2 = Time.at(1_234_567, 500_000)
  409. diff = t2 - t1
  410. assert_equal( 566.75, diff)
  411. end
  412. def test_mon
  413. util_check_component(:mon, 1)
  414. end
  415. def test_month
  416. util_check_component(:month, 1)
  417. end
  418. def test_plus # '+'
  419. @@dates.each do |x|
  420. assert_equal(Time.local(*x.orig) + x.amt, Time.local(*x.result))
  421. assert_equal(Time.gm(*x.orig) + x.amt, Time.gm(*x.result))
  422. end
  423. # integer argument
  424. t1 = Time.at(1_234_567, 500_000)
  425. t2 = t1 + 433
  426. assert_equal( 1_235_000, t2.tv_sec)
  427. assert_equal( 500_000, t2.tv_usec)
  428. # float argument with fractional part
  429. t1 = Time.at(1_234_567, 500_000)
  430. t2 = t1 + 433.25
  431. assert_equal( 1_235_000, t2.tv_sec)
  432. assert_equal( 750_000, t2.tv_usec)
  433. end
  434. def test_sec
  435. util_check_component(:sec, 5)
  436. end
  437. def test_spaceship # '<=>'
  438. @@dates.each do |x|
  439. if (x.amt != 0)
  440. assert_equal(1, Time.local(*x.result) <=> Time.local(*x.orig),
  441. "#{x.result} should be > #{x.orig}")
  442. assert_equal(-1, Time.local(*x.orig) <=> Time.local(*x.result))
  443. assert_equal(0, Time.local(*x.orig) <=> Time.local(*x.orig))
  444. assert_equal(0, Time.local(*x.result) <=> Time.local(*x.result))
  445. assert_equal(1,Time.gm(*x.result) <=> Time.gm(*x.orig))
  446. assert_equal(-1,Time.gm(*x.orig) <=> Time.gm(*x.result))
  447. assert_equal(0,Time.gm(*x.orig) <=> Time.gm(*x.orig))
  448. assert_equal(0,Time.gm(*x.result) <=> Time.gm(*x.result))
  449. end
  450. end
  451. # microsecond diffs
  452. assert_equal( 1, Time.at(10_000, 500_000) <=> Time.at(10_000, 499_999))
  453. assert_equal( 0, Time.at(10_000, 500_000) <=> Time.at(10_000, 500_000))
  454. assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_000, 500_001))
  455. # second diff & microsecond diffs
  456. assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 499_999))
  457. assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 500_000))
  458. assert_equal(-1, Time.at(10_000, 500_000) <=> Time.at(10_001, 500_001))
  459. # non-Time object gives nil
  460. assert_nil(Time.at(10_000) <=> Object.new)
  461. end
  462. def test_strftime
  463. # Sat Jan 1 14:58:42 2000
  464. t = Time.local(2000,1,1,14,58,42)
  465. stest = {
  466. '%a' => 'Sat',
  467. '%A' => 'Saturday',
  468. '%b' => 'Jan',
  469. '%B' => 'January',
  470. #'%c', The preferred local date and time representation,
  471. '%d' => '01',
  472. '%H' => '14',
  473. '%I' => '02',
  474. '%j' => '001',
  475. '%m' => '01',
  476. '%M' => '58',
  477. '%p' => 'PM',
  478. '%S' => '42',
  479. '%U' => '00',
  480. '%W' => '00',
  481. '%w' => '6',
  482. #'%x', Preferred representation for the date alone, no time\\
  483. #'%X', Preferred representation for the time alone, no date\\
  484. '%y' => '00',
  485. '%Y' => '2000',
  486. #'%Z', Time zone name\\
  487. '%%' => '%',
  488. }
  489. stest.each {|flag,val|
  490. assert_equal("Got "+val,t.strftime("Got " + flag))
  491. }
  492. end
  493. def test_succ
  494. t1 = @loc
  495. t2 = t1 + 1
  496. t3 = t1.succ
  497. assert_equal(t2, t3)
  498. end
  499. def test_to_a
  500. t = @loc
  501. a = t.to_a
  502. assert_equal(t.sec, a[0])
  503. assert_equal(t.min, a[1])
  504. assert_equal(t.hour, a[2])
  505. assert_equal(t.day, a[3])
  506. assert_equal(t.month,a[4])
  507. assert_equal(t.year, a[5])
  508. assert_equal(t.wday, a[6])
  509. assert_equal(t.yday, a[7])
  510. assert_equal(t.isdst,a[8])
  511. assert_equal(t.zone, a[9])
  512. end
  513. def test_to_f
  514. t = Time.at(10000,1066)
  515. assert_in_delta(10000.001066, t.to_f, 1e-7)
  516. end
  517. def test_to_i
  518. t = Time.at(0)
  519. assert_equal(0, t.to_i)
  520. t = Time.at(10000)
  521. assert_equal(10000, t.to_i)
  522. end
  523. def test_to_s
  524. assert_equal("Sat Feb 03 04:05:06 UTC 2001", @utc.to_s)
  525. assert_equal("Sat Feb 03 04:05:06 -0800 2001", @loc.to_s)
  526. end
  527. def test_tv_sec
  528. t = Time.at(0)
  529. assert_equal(0,t.tv_sec)
  530. t = Time.at(10000)
  531. assert_equal(10000,t.tv_sec)
  532. end
  533. def util_usec(s, u, method)
  534. t = Time.at(s,u)
  535. assert_equal(u,t.send(method))
  536. end
  537. def test_tv_usec
  538. util_usec(10000, 1066, :tv_usec)
  539. util_usec(10000, 0, :tv_usec)
  540. end
  541. def test_usec
  542. util_usec(10000, 1066, :usec)
  543. util_usec(10000, 0, :usec)
  544. end
  545. def test_utc
  546. test_gmtime # REFACTOR to test both methods
  547. end
  548. def test_utc_eh
  549. test_gmt_eh # REFACTOR to test both methods
  550. end
  551. def test_utc_offset
  552. # TODO: figure out the year, month, & day edgecase setups
  553. off = @utc - @loc
  554. assert_equal(0, @utc.utc_offset)
  555. assert_equal(off, @loc.utc_offset)
  556. end
  557. def test_wday
  558. t = Time.local(2001, 4, 1)
  559. 7.times { |i|
  560. assert_equal(i,t.wday)
  561. t += ONEDAYSEC
  562. }
  563. end
  564. def test_yday
  565. # non-leap 1/1, 2/28, 3/1, 12/31
  566. # leap 1/1, 2/28, 2/29, 3/1, 12/31
  567. # leap century (2000)
  568. # want to do a non-leap century, but they are out of range.
  569. # any others?
  570. # non-leap year:
  571. assert_equal( 1, Time.local(1999, 1, 1).yday)
  572. assert_equal( 59, Time.local(1999, 2, 28).yday)
  573. assert_equal( 60, Time.local(1999, 3, 1).yday)
  574. assert_equal(365, Time.local(1999, 12, 31).yday)
  575. # leap century:
  576. assert_equal( 1, Time.local(2000, 1, 1).yday)
  577. assert_equal( 59, Time.local(2000, 2, 28).yday)
  578. assert_equal( 60, Time.local(2000, 2, 29).yday)
  579. assert_equal( 61, Time.local(2000, 3, 1).yday)
  580. assert_equal(366, Time.local(2000, 12, 31).yday)
  581. # leap year:
  582. assert_equal( 1, Time.local(2004, 1, 1).yday)
  583. assert_equal( 59, Time.local(2004, 2, 28).yday)
  584. assert_equal( 60, Time.local(2004, 2, 29).yday)
  585. assert_equal( 61, Time.local(2004, 3, 1).yday)
  586. assert_equal(366, Time.local(2004, 12, 31).yday)
  587. end
  588. def test_year
  589. util_check_component(:year, 0)
  590. end
  591. def test_zone
  592. gmt = "UTC"
  593. t = @utc
  594. assert_equal(gmt, t.zone)
  595. t = @loc
  596. assert_not_equal(gmt, t.zone)
  597. end
  598. end