1require 'test/unit' 2require 'date' 3 4class TestDateConv < Test::Unit::TestCase 5 6 def test_to_class 7 [Time.now, Date.today, DateTime.now].each do |o| 8 assert_instance_of(Time, o.to_time) 9 assert_instance_of(Date, o.to_date) 10 assert_instance_of(DateTime, o.to_datetime) 11 end 12 end 13 14 def test_to_time__from_time 15 t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789) 16 t2 = t.to_time 17 assert_equal([2004, 9, 19, 1, 2, 3, 456789], 18 [t2.year, t2.mon, t2.mday, t2.hour, t2.min, t2.sec, t2.usec]) 19 20 t = Time.utc(2004, 9, 19, 1, 2, 3, 456789) 21 t2 = t.to_time.utc 22 assert_equal([2004, 9, 19, 1, 2, 3, 456789], 23 [t2.year, t2.mon, t2.mday, t2.hour, t2.min, t2.sec, t2.usec]) 24 end 25 26 def test_to_time__from_date 27 d = Date.new(2004, 9, 19) 28 t = d.to_time 29 assert_equal([2004, 9, 19, 0, 0, 0, 0], 30 [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) 31 end 32 33 def test_to_time__from_datetime 34 d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000 35 t = d.to_time 36 if t.utc_offset == 9*60*60 37 assert_equal([2004, 9, 19, 1, 2, 3, 456789], 38 [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) 39 end 40 41 d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000 42 t = d.to_time.utc 43 assert_equal([2004, 9, 19, 1, 2, 3, 456789], 44 [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.usec]) 45 46 if Time.allocate.respond_to?(:nsec) 47 d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123.to_r/86400000000000 48 t = d.to_time.utc 49 assert_equal([2004, 9, 19, 1, 2, 3, 456789123], 50 [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.nsec]) 51 end 52 53 if Time.allocate.respond_to?(:subsec) 54 d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789123456789123.to_r/86400000000000000000000 55 t = d.to_time.utc 56 assert_equal([2004, 9, 19, 1, 2, 3, Rational(456789123456789123,1000000000000000000)], 57 [t.year, t.mon, t.mday, t.hour, t.min, t.sec, t.subsec]) 58 end 59 end 60 61 def test_to_date__from_time 62 t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789) 63 d = t.to_date 64 assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction]) 65 66 t = Time.utc(2004, 9, 19, 1, 2, 3, 456789) 67 d = t.to_date 68 assert_equal([2004, 9, 19, 0], [d.year, d.mon, d.mday, d.day_fraction]) 69 end 70 71 def test_to_date__from_date 72 d = Date.new(2004, 9, 19) + 1.to_r/2 73 d2 = d.to_date 74 assert_equal([2004, 9, 19, 1.to_r/2], 75 [d2.year, d2.mon, d2.mday, d2.day_fraction]) 76 end 77 78 def test_to_date__from_datetime 79 d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000 80 d2 = d.to_date 81 assert_equal([2004, 9, 19, 0], [d2.year, d2.mon, d2.mday, d2.day_fraction]) 82 83 d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000 84 d2 = d.to_date 85 assert_equal([2004, 9, 19, 0], [d2.year, d2.mon, d2.mday, d2.day_fraction]) 86 end 87 88 def test_to_datetime__from_time 89 t = Time.mktime(2004, 9, 19, 1, 2, 3, 456789) 90 d = t.to_datetime 91 assert_equal([2004, 9, 19, 1, 2, 3, 92 456789.to_r/1000000, 93 t.utc_offset.to_r/86400], 94 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, 95 d.sec_fraction, d.offset]) 96 97 t = Time.utc(2004, 9, 19, 1, 2, 3, 456789) 98 d = t.to_datetime 99 assert_equal([2004, 9, 19, 1, 2, 3, 100 456789.to_r/1000000, 101 0], 102 [d.year, d.mon, d.mday, d.hour, d.min, d.sec, 103 d.sec_fraction, d.offset]) 104 105 t = Time.now 106 d = t.to_datetime 107 require 'time' 108 assert_equal(t.iso8601(10), d.iso8601(10)) 109 end 110 111 def test_to_datetime__from_date 112 d = Date.new(2004, 9, 19) + 1.to_r/2 113 d2 = d.to_datetime 114 assert_equal([2004, 9, 19, 0, 0, 0, 0, 0], 115 [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, 116 d2.sec_fraction, d2.offset]) 117 end 118 119 def test_to_datetime__from_datetime 120 d = DateTime.new(2004, 9, 19, 1, 2, 3, 9.to_r/24) + 456789.to_r/86400000000 121 d2 = d.to_datetime 122 assert_equal([2004, 9, 19, 1, 2, 3, 123 456789.to_r/1000000, 124 9.to_r/24], 125 [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, 126 d2.sec_fraction, d2.offset]) 127 128 d = DateTime.new(2004, 9, 19, 1, 2, 3, 0) + 456789.to_r/86400000000 129 d2 = d.to_datetime 130 assert_equal([2004, 9, 19, 1, 2, 3, 131 456789.to_r/1000000, 132 0], 133 [d2.year, d2.mon, d2.mday, d2.hour, d2.min, d2.sec, 134 d2.sec_fraction, d2.offset]) 135 end 136 137end 138