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