| 2 | | |
|---|
| 3 | | uses_tzinfo 'TimeWithZoneTest' do |
|---|
| 4 | | |
|---|
| 5 | | class TimeWithZoneTest < Test::Unit::TestCase |
|---|
| 6 | | |
|---|
| 7 | | def setup |
|---|
| 8 | | @utc = Time.utc(2000, 1, 1, 0) |
|---|
| 9 | | @time_zone = TimeZone['Eastern Time (US & Canada)'] |
|---|
| 10 | | @twz = ActiveSupport::TimeWithZone.new(@utc, @time_zone) |
|---|
| 11 | | end |
|---|
| 12 | | |
|---|
| 13 | | def test_utc |
|---|
| 14 | | assert_equal @utc, @twz.utc |
|---|
| 15 | | end |
|---|
| 16 | | |
|---|
| 17 | | def test_time |
|---|
| 18 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 19 | | assert_equal Time.utc(1999, 12, 31, 19), @twz.time |
|---|
| | 2 | |
|---|
| | 3 | class TimeWithZoneTest < Test::Unit::TestCase |
|---|
| | 4 | |
|---|
| | 5 | def setup |
|---|
| | 6 | @utc = Time.utc(2000, 1, 1, 0) |
|---|
| | 7 | @time_zone = TimeZone['Eastern Time (US & Canada)'] |
|---|
| | 8 | @twz = ActiveSupport::TimeWithZone.new(@utc, @time_zone) |
|---|
| | 9 | end |
|---|
| | 10 | |
|---|
| | 11 | def test_utc |
|---|
| | 12 | assert_equal @utc, @twz.utc |
|---|
| | 13 | end |
|---|
| | 14 | |
|---|
| | 15 | def test_time |
|---|
| | 16 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 17 | assert_equal Time.utc(1999, 12, 31, 19), @twz.time |
|---|
| | 18 | end |
|---|
| | 19 | end |
|---|
| | 20 | |
|---|
| | 21 | def test_time_zone |
|---|
| | 22 | assert_equal @time_zone, @twz.time_zone |
|---|
| | 23 | end |
|---|
| | 24 | |
|---|
| | 25 | def test_in_time_zone |
|---|
| | 26 | Time.use_zone 'Alaska' do |
|---|
| | 27 | assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone |
|---|
| | 28 | end |
|---|
| | 29 | end |
|---|
| | 30 | |
|---|
| | 31 | def test_in_time_zone_with_argument |
|---|
| | 32 | assert_equal ActiveSupport::TimeWithZone.new(@utc, TimeZone['Alaska']), @twz.in_time_zone('Alaska') |
|---|
| | 33 | end |
|---|
| | 34 | |
|---|
| | 35 | def test_in_time_zone_with_new_zone_equal_to_old_zone_does_not_create_new_object |
|---|
| | 36 | assert_equal @twz.object_id, @twz.in_time_zone(TimeZone['Eastern Time (US & Canada)']).object_id |
|---|
| | 37 | end |
|---|
| | 38 | |
|---|
| | 39 | def test_utc? |
|---|
| | 40 | assert_equal false, @twz.utc? |
|---|
| | 41 | assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone['UTC']).utc? |
|---|
| | 42 | end |
|---|
| | 43 | |
|---|
| | 44 | def test_formatted_offset |
|---|
| | 45 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 46 | assert_equal '-05:00', @twz.formatted_offset |
|---|
| | 47 | assert_equal '-04:00', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).formatted_offset #dst |
|---|
| | 48 | end |
|---|
| | 49 | end |
|---|
| | 50 | |
|---|
| | 51 | def test_dst? |
|---|
| | 52 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 53 | assert_equal false, @twz.dst? |
|---|
| | 54 | assert_equal true, ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).dst? |
|---|
| | 55 | end |
|---|
| | 56 | end |
|---|
| | 57 | |
|---|
| | 58 | def test_zone |
|---|
| | 59 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 60 | assert_equal 'EST', @twz.zone |
|---|
| | 61 | assert_equal 'EDT', ActiveSupport::TimeWithZone.new(Time.utc(2000, 6), @time_zone).zone #dst |
|---|
| | 62 | end |
|---|
| | 63 | end |
|---|
| | 64 | |
|---|
| | 65 | def test_to_json |
|---|
| | 66 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 67 | assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json |
|---|
| | 68 | end |
|---|
| | 69 | end |
|---|
| | 70 | |
|---|
| | 71 | def test_strftime |
|---|
| | 72 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 73 | assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') |
|---|
| | 74 | end |
|---|
| | 75 | end |
|---|
| | 76 | |
|---|
| | 77 | def test_inspect |
|---|
| | 78 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 79 | assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect |
|---|
| | 80 | end |
|---|
| | 81 | end |
|---|
| | 82 | |
|---|
| | 83 | def test_to_s |
|---|
| | 84 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 85 | assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s |
|---|
| | 86 | end |
|---|
| | 87 | end |
|---|
| | 88 | |
|---|
| | 89 | def test_to_s_db |
|---|
| | 90 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 91 | assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) |
|---|
| | 92 | end |
|---|
| | 93 | end |
|---|
| | 94 | |
|---|
| | 95 | def test_xmlschema |
|---|
| | 96 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 97 | assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema |
|---|
| | 98 | end |
|---|
| | 99 | end |
|---|
| | 100 | |
|---|
| | 101 | def test_to_yaml |
|---|
| | 102 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 103 | assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml |
|---|
| | 104 | end |
|---|
| | 105 | end |
|---|
| | 106 | |
|---|
| | 107 | def test_ruby_to_yaml |
|---|
| | 108 | silence_warnings do |
|---|
| | 109 | assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml |
|---|
| | 110 | end |
|---|
| | 111 | end |
|---|
| | 112 | |
|---|
| | 113 | def test_httpdate |
|---|
| | 114 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 115 | assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate |
|---|
| | 116 | end |
|---|
| | 117 | end |
|---|
| | 118 | |
|---|
| | 119 | def test_rfc2822 |
|---|
| | 120 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 121 | assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 |
|---|
| | 122 | end |
|---|
| | 123 | end |
|---|
| | 124 | |
|---|
| | 125 | def test_compare_with_time |
|---|
| | 126 | assert_equal 1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59) |
|---|
| | 127 | assert_equal 0, @twz <=> Time.utc(2000, 1, 1, 0, 0, 0) |
|---|
| | 128 | assert_equal(-1, @twz <=> Time.utc(2000, 1, 1, 0, 0, 1)) |
|---|
| | 129 | end |
|---|
| | 130 | |
|---|
| | 131 | def test_compare_with_datetime |
|---|
| | 132 | assert_equal 1, @twz <=> DateTime.civil(1999, 12, 31, 23, 59, 59) |
|---|
| | 133 | assert_equal 0, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 0) |
|---|
| | 134 | assert_equal(-1, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 1)) |
|---|
| | 135 | end |
|---|
| | 136 | |
|---|
| | 137 | def test_compare_with_time_with_zone |
|---|
| | 138 | assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] ) |
|---|
| | 139 | assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) |
|---|
| | 140 | assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) |
|---|
| | 141 | end |
|---|
| | 142 | |
|---|
| | 143 | def test_between? |
|---|
| | 144 | assert @twz.between?(Time.utc(1999,12,31,23,59,59), Time.utc(2000,1,1,0,0,1)) |
|---|
| | 145 | assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2)) |
|---|
| | 146 | end |
|---|
| | 147 | |
|---|
| | 148 | def test_eql? |
|---|
| | 149 | assert @twz.eql?(Time.utc(2000)) |
|---|
| | 150 | assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) |
|---|
| | 151 | end |
|---|
| | 152 | |
|---|
| | 153 | def test_plus_with_integer |
|---|
| | 154 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 155 | assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time |
|---|
| | 156 | end |
|---|
| | 157 | end |
|---|
| | 158 | |
|---|
| | 159 | def test_plus_with_integer_when_self_wraps_datetime |
|---|
| | 160 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 161 | datetime = DateTime.civil(2000, 1, 1, 0) |
|---|
| | 162 | twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) |
|---|
| | 163 | assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time |
|---|
| | 164 | end |
|---|
| | 165 | end |
|---|
| | 166 | |
|---|
| | 167 | def test_plus_with_duration |
|---|
| | 168 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 169 | assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time |
|---|
| | 170 | end |
|---|
| | 171 | end |
|---|
| | 172 | |
|---|
| | 173 | def test_minus_with_integer |
|---|
| | 174 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 175 | assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time |
|---|
| | 176 | end |
|---|
| | 177 | end |
|---|
| | 178 | |
|---|
| | 179 | def test_minus_with_integer_when_self_wraps_datetime |
|---|
| | 180 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 181 | datetime = DateTime.civil(2000, 1, 1, 0) |
|---|
| | 182 | twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) |
|---|
| | 183 | assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time |
|---|
| | 184 | end |
|---|
| | 185 | end |
|---|
| | 186 | |
|---|
| | 187 | def test_minus_with_duration |
|---|
| | 188 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 189 | assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time |
|---|
| | 190 | end |
|---|
| | 191 | end |
|---|
| | 192 | |
|---|
| | 193 | def test_minus_with_time |
|---|
| | 194 | assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) - Time.utc(2000, 1, 1) |
|---|
| | 195 | assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1) |
|---|
| | 196 | end |
|---|
| | 197 | |
|---|
| | 198 | def test_minus_with_time_with_zone |
|---|
| | 199 | twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) |
|---|
| | 200 | twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) |
|---|
| | 201 | assert_equal 86_400.0, twz2 - twz1 |
|---|
| | 202 | end |
|---|
| | 203 | |
|---|
| | 204 | def test_plus_and_minus_enforce_spring_dst_rules |
|---|
| | 205 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 206 | utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start |
|---|
| | 207 | twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) |
|---|
| | 208 | assert_equal Time.utc(2006,4,2,1,59,59), twz.time |
|---|
| | 209 | assert_equal false, twz.dst? |
|---|
| | 210 | assert_equal 'EST', twz.zone |
|---|
| | 211 | twz = twz + 1 |
|---|
| | 212 | assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT |
|---|
| | 213 | assert_equal true, twz.dst? |
|---|
| | 214 | assert_equal 'EDT', twz.zone |
|---|
| | 215 | twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST |
|---|
| | 216 | assert_equal Time.utc(2006,4,2,1,59,59), twz.time |
|---|
| | 217 | assert_equal false, twz.dst? |
|---|
| | 218 | assert_equal 'EST', twz.zone |
|---|
| | 219 | end |
|---|
| | 220 | end |
|---|
| | 221 | |
|---|
| | 222 | def test_plus_and_minus_enforce_fall_dst_rules |
|---|
| | 223 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 224 | utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end |
|---|
| | 225 | twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) |
|---|
| | 226 | assert_equal Time.utc(2006,10,29,1,59,59), twz.time |
|---|
| | 227 | assert_equal true, twz.dst? |
|---|
| | 228 | assert_equal 'EDT', twz.zone |
|---|
| | 229 | twz = twz + 1 |
|---|
| | 230 | assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST |
|---|
| | 231 | assert_equal false, twz.dst? |
|---|
| | 232 | assert_equal 'EST', twz.zone |
|---|
| | 233 | twz = twz - 1 |
|---|
| | 234 | assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT |
|---|
| | 235 | assert_equal true, twz.dst? |
|---|
| | 236 | assert_equal 'EDT', twz.zone |
|---|
| | 237 | end |
|---|
| | 238 | end |
|---|
| | 239 | |
|---|
| | 240 | def test_to_a |
|---|
| | 241 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 242 | assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a |
|---|
| | 243 | end |
|---|
| | 244 | end |
|---|
| | 245 | |
|---|
| | 246 | def test_to_f |
|---|
| | 247 | result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_f |
|---|
| | 248 | assert_equal 946684800.0, result |
|---|
| | 249 | assert result.is_a?(Float) |
|---|
| | 250 | end |
|---|
| | 251 | |
|---|
| | 252 | def test_to_i |
|---|
| | 253 | result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_i |
|---|
| | 254 | assert_equal 946684800, result |
|---|
| | 255 | assert result.is_a?(Integer) |
|---|
| | 256 | end |
|---|
| | 257 | |
|---|
| | 258 | def test_to_time |
|---|
| | 259 | assert_equal @twz, @twz.to_time |
|---|
| | 260 | end |
|---|
| | 261 | |
|---|
| | 262 | def test_to_date |
|---|
| | 263 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 264 | # 1 sec before midnight Jan 1 EST |
|---|
| | 265 | assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| | 266 | # midnight Jan 1 EST |
|---|
| | 267 | assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| | 268 | # 1 sec before midnight Jan 2 EST |
|---|
| | 269 | assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| | 270 | # midnight Jan 2 EST |
|---|
| | 271 | assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| | 272 | end |
|---|
| | 273 | end |
|---|
| | 274 | |
|---|
| | 275 | def test_to_datetime |
|---|
| | 276 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 277 | assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime |
|---|
| | 278 | end |
|---|
| | 279 | end |
|---|
| | 280 | |
|---|
| | 281 | def test_acts_like_time |
|---|
| | 282 | assert @twz.acts_like?(:time) |
|---|
| | 283 | assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time) |
|---|
| | 284 | end |
|---|
| | 285 | |
|---|
| | 286 | def test_acts_like_date |
|---|
| | 287 | assert_equal false, @twz.acts_like?(:date) |
|---|
| | 288 | assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date) |
|---|
| | 289 | end |
|---|
| | 290 | |
|---|
| | 291 | def test_is_a |
|---|
| | 292 | assert @twz.is_a?(Time) |
|---|
| | 293 | assert @twz.kind_of?(Time) |
|---|
| | 294 | assert @twz.is_a?(ActiveSupport::TimeWithZone) |
|---|
| | 295 | end |
|---|
| | 296 | |
|---|
| | 297 | def test_method_missing_with_time_return_value |
|---|
| | 298 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 299 | assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) |
|---|
| | 300 | assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time |
|---|
| | 301 | end |
|---|
| | 302 | end |
|---|
| | 303 | |
|---|
| | 304 | def test_marshal_dump_and_load |
|---|
| | 305 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 306 | marshal_str = Marshal.dump(@twz) |
|---|
| | 307 | mtime = Marshal.load(marshal_str) |
|---|
| | 308 | assert_equal Time.utc(2000, 1, 1, 0), mtime.utc |
|---|
| | 309 | assert_equal TimeZone['Eastern Time (US & Canada)'], mtime.time_zone |
|---|
| | 310 | assert_equal Time.utc(1999, 12, 31, 19), mtime.time |
|---|
| | 311 | end |
|---|
| | 312 | end |
|---|
| | 313 | |
|---|
| | 314 | def test_marshal_dump_and_load_with_tzinfo_identifier |
|---|
| | 315 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 316 | twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York')) |
|---|
| | 317 | marshal_str = Marshal.dump(twz) |
|---|
| | 318 | mtime = Marshal.load(marshal_str) |
|---|
| | 319 | assert_equal Time.utc(2000, 1, 1, 0), mtime.utc |
|---|
| | 320 | assert_equal 'America/New_York', mtime.time_zone.name |
|---|
| | 321 | assert_equal Time.utc(1999, 12, 31, 19), mtime.time |
|---|
| | 322 | end |
|---|
| | 323 | end |
|---|
| | 324 | |
|---|
| | 325 | def test_method_missing_with_non_time_return_value |
|---|
| | 326 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 327 | twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) |
|---|
| | 328 | assert_equal 1999, twz.year |
|---|
| | 329 | assert_equal 12, twz.month |
|---|
| | 330 | assert_equal 31, twz.day |
|---|
| | 331 | assert_equal 14, twz.hour |
|---|
| | 332 | assert_equal 18, twz.min |
|---|
| | 333 | assert_equal 17, twz.sec |
|---|
| | 334 | assert_equal 500, twz.usec |
|---|
| | 335 | end |
|---|
| | 336 | end |
|---|
| | 337 | |
|---|
| | 338 | def test_usec_returns_0_when_datetime_is_wrapped |
|---|
| | 339 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 340 | twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone) |
|---|
| | 341 | assert_equal 0, twz.usec |
|---|
| | 342 | end |
|---|
| | 343 | end |
|---|
| | 344 | |
|---|
| | 345 | def test_utc_to_local_conversion_saves_period_in_instance_variable |
|---|
| | 346 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 347 | assert_nil @twz.instance_variable_get('@period') |
|---|
| | 348 | @twz.time |
|---|
| | 349 | assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') |
|---|
| | 350 | end |
|---|
| | 351 | end |
|---|
| | 352 | |
|---|
| | 353 | def test_instance_created_with_local_time_returns_correct_utc_time |
|---|
| | 354 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 355 | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) |
|---|
| | 356 | assert_equal Time.utc(2000), twz.utc |
|---|
| | 357 | end |
|---|
| | 358 | end |
|---|
| | 359 | |
|---|
| | 360 | def test_instance_created_with_local_time_enforces_spring_dst_rules |
|---|
| | 361 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 362 | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST |
|---|
| | 363 | assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM |
|---|
| | 364 | assert_equal true, twz.dst? |
|---|
| | 365 | assert_equal 'EDT', twz.zone |
|---|
| | 366 | end |
|---|
| | 367 | end |
|---|
| | 368 | |
|---|
| | 369 | def test_instance_created_with_local_time_enforces_fall_dst_rules |
|---|
| | 370 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 371 | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST |
|---|
| | 372 | assert_equal Time.utc(2006,10,29,1), twz.time |
|---|
| | 373 | assert_equal true, twz.dst? |
|---|
| | 374 | assert_equal 'EDT', twz.zone |
|---|
| | 375 | end |
|---|
| | 376 | end |
|---|
| | 377 | |
|---|
| | 378 | def test_ruby_19_weekday_name_query_methods |
|---|
| | 379 | ruby_19_or_greater = RUBY_VERSION >= '1.9' |
|---|
| | 380 | %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name| |
|---|
| | 381 | assert_equal ruby_19_or_greater, @twz.respond_to?(name) |
|---|
| | 382 | end |
|---|
| | 383 | end |
|---|
| | 384 | |
|---|
| | 385 | def test_utc_to_local_conversion_with_far_future_datetime |
|---|
| | 386 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 387 | assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6] |
|---|
| | 388 | end |
|---|
| | 389 | end |
|---|
| | 390 | |
|---|
| | 391 | def test_local_to_utc_conversion_with_far_future_datetime |
|---|
| | 392 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 393 | assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f |
|---|
| | 394 | end |
|---|
| | 395 | end |
|---|
| | 396 | end |
|---|
| | 397 | |
|---|
| | 398 | class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase |
|---|
| | 399 | def setup |
|---|
| | 400 | @t, @dt = Time.utc(2000), DateTime.civil(2000) |
|---|
| | 401 | end |
|---|
| | 402 | |
|---|
| | 403 | def teardown |
|---|
| | 404 | Time.zone = nil |
|---|
| | 405 | end |
|---|
| | 406 | |
|---|
| | 407 | def test_in_time_zone |
|---|
| | 408 | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| | 409 | Time.use_zone 'Alaska' do |
|---|
| | 410 | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect |
|---|
| | 411 | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect |
|---|
| 66 | | |
|---|
| 67 | | def test_to_json |
|---|
| 68 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 69 | | assert_equal "\"1999/12/31 19:00:00 -0500\"", @twz.to_json |
|---|
| 70 | | end |
|---|
| 71 | | end |
|---|
| 72 | | |
|---|
| 73 | | def test_strftime |
|---|
| 74 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 75 | | assert_equal '1999-12-31 19:00:00 EST -0500', @twz.strftime('%Y-%m-%d %H:%M:%S %Z %z') |
|---|
| 76 | | end |
|---|
| 77 | | end |
|---|
| 78 | | |
|---|
| 79 | | def test_inspect |
|---|
| 80 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 81 | | assert_equal 'Fri, 31 Dec 1999 19:00:00 EST -05:00', @twz.inspect |
|---|
| 82 | | end |
|---|
| 83 | | end |
|---|
| 84 | | |
|---|
| 85 | | def test_to_s |
|---|
| 86 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 87 | | assert_equal '1999-12-31 19:00:00 -0500', @twz.to_s |
|---|
| 88 | | end |
|---|
| 89 | | end |
|---|
| 90 | | |
|---|
| 91 | | def test_to_s_db |
|---|
| 92 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 93 | | assert_equal '2000-01-01 00:00:00', @twz.to_s(:db) |
|---|
| 94 | | end |
|---|
| 95 | | end |
|---|
| 96 | | |
|---|
| 97 | | def test_xmlschema |
|---|
| 98 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 99 | | assert_equal "1999-12-31T19:00:00-05:00", @twz.xmlschema |
|---|
| 100 | | end |
|---|
| 101 | | end |
|---|
| 102 | | |
|---|
| 103 | | def test_to_yaml |
|---|
| 104 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 105 | | assert_equal "--- 1999-12-31 19:00:00 -05:00\n", @twz.to_yaml |
|---|
| 106 | | end |
|---|
| 107 | | end |
|---|
| 108 | | |
|---|
| 109 | | def test_ruby_to_yaml |
|---|
| 110 | | silence_warnings do |
|---|
| 111 | | assert_equal "--- \n:twz: 2000-01-01 00:00:00 Z\n", {:twz => @twz}.to_yaml |
|---|
| 112 | | end |
|---|
| 113 | | end |
|---|
| 114 | | |
|---|
| 115 | | def test_httpdate |
|---|
| 116 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 117 | | assert_equal 'Sat, 01 Jan 2000 00:00:00 GMT', @twz.httpdate |
|---|
| 118 | | end |
|---|
| 119 | | end |
|---|
| 120 | | |
|---|
| 121 | | def test_rfc2822 |
|---|
| 122 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 123 | | assert_equal "Fri, 31 Dec 1999 19:00:00 -0500", @twz.rfc2822 |
|---|
| 124 | | end |
|---|
| 125 | | end |
|---|
| 126 | | |
|---|
| 127 | | def test_compare_with_time |
|---|
| 128 | | assert_equal 1, @twz <=> Time.utc(1999, 12, 31, 23, 59, 59) |
|---|
| 129 | | assert_equal 0, @twz <=> Time.utc(2000, 1, 1, 0, 0, 0) |
|---|
| 130 | | assert_equal(-1, @twz <=> Time.utc(2000, 1, 1, 0, 0, 1)) |
|---|
| 131 | | end |
|---|
| 132 | | |
|---|
| 133 | | def test_compare_with_datetime |
|---|
| 134 | | assert_equal 1, @twz <=> DateTime.civil(1999, 12, 31, 23, 59, 59) |
|---|
| 135 | | assert_equal 0, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 0) |
|---|
| 136 | | assert_equal(-1, @twz <=> DateTime.civil(2000, 1, 1, 0, 0, 1)) |
|---|
| 137 | | end |
|---|
| 138 | | |
|---|
| 139 | | def test_compare_with_time_with_zone |
|---|
| 140 | | assert_equal 1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(1999, 12, 31, 23, 59, 59), TimeZone['UTC'] ) |
|---|
| 141 | | assert_equal 0, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 0), TimeZone['UTC'] ) |
|---|
| 142 | | assert_equal(-1, @twz <=> ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 0, 0, 1), TimeZone['UTC'] )) |
|---|
| 143 | | end |
|---|
| 144 | | |
|---|
| 145 | | def test_between? |
|---|
| 146 | | assert @twz.between?(Time.utc(1999,12,31,23,59,59), Time.utc(2000,1,1,0,0,1)) |
|---|
| 147 | | assert_equal false, @twz.between?(Time.utc(2000,1,1,0,0,1), Time.utc(2000,1,1,0,0,2)) |
|---|
| 148 | | end |
|---|
| 149 | | |
|---|
| 150 | | def test_eql? |
|---|
| 151 | | assert @twz.eql?(Time.utc(2000)) |
|---|
| 152 | | assert @twz.eql?( ActiveSupport::TimeWithZone.new(Time.utc(2000), TimeZone["Hawaii"]) ) |
|---|
| 153 | | end |
|---|
| 154 | | |
|---|
| 155 | | def test_plus_with_integer |
|---|
| 156 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 157 | | assert_equal Time.utc(1999, 12, 31, 19, 0 ,5), (@twz + 5).time |
|---|
| 158 | | end |
|---|
| 159 | | end |
|---|
| 160 | | |
|---|
| 161 | | def test_plus_with_integer_when_self_wraps_datetime |
|---|
| 162 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 163 | | datetime = DateTime.civil(2000, 1, 1, 0) |
|---|
| 164 | | twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) |
|---|
| 165 | | assert_equal DateTime.civil(1999, 12, 31, 19, 0 ,5), (twz + 5).time |
|---|
| 166 | | end |
|---|
| 167 | | end |
|---|
| 168 | | |
|---|
| 169 | | def test_plus_with_duration |
|---|
| 170 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 171 | | assert_equal Time.utc(2000, 1, 5, 19, 0 ,0), (@twz + 5.days).time |
|---|
| 172 | | end |
|---|
| 173 | | end |
|---|
| 174 | | |
|---|
| 175 | | def test_minus_with_integer |
|---|
| 176 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 177 | | assert_equal Time.utc(1999, 12, 31, 18, 59 ,55), (@twz - 5).time |
|---|
| 178 | | end |
|---|
| 179 | | end |
|---|
| 180 | | |
|---|
| 181 | | def test_minus_with_integer_when_self_wraps_datetime |
|---|
| 182 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 183 | | datetime = DateTime.civil(2000, 1, 1, 0) |
|---|
| 184 | | twz = ActiveSupport::TimeWithZone.new(datetime, @time_zone) |
|---|
| 185 | | assert_equal DateTime.civil(1999, 12, 31, 18, 59 ,55), (twz - 5).time |
|---|
| 186 | | end |
|---|
| 187 | | end |
|---|
| 188 | | |
|---|
| 189 | | def test_minus_with_duration |
|---|
| 190 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 191 | | assert_equal Time.utc(1999, 12, 26, 19, 0 ,0), (@twz - 5.days).time |
|---|
| 192 | | end |
|---|
| 193 | | end |
|---|
| 194 | | |
|---|
| 195 | | def test_minus_with_time |
|---|
| 196 | | assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) - Time.utc(2000, 1, 1) |
|---|
| 197 | | assert_equal 86_400.0, ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['Hawaii'] ) - Time.utc(2000, 1, 1) |
|---|
| 198 | | end |
|---|
| 199 | | |
|---|
| 200 | | def test_minus_with_time_with_zone |
|---|
| 201 | | twz1 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['UTC'] ) |
|---|
| 202 | | twz2 = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2), TimeZone['UTC'] ) |
|---|
| 203 | | assert_equal 86_400.0, twz2 - twz1 |
|---|
| 204 | | end |
|---|
| 205 | | |
|---|
| 206 | | def test_plus_and_minus_enforce_spring_dst_rules |
|---|
| 207 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 208 | | utc = Time.utc(2006,4,2,6,59,59) # == Apr 2 2006 01:59:59 EST; i.e., 1 second before daylight savings start |
|---|
| 209 | | twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) |
|---|
| 210 | | assert_equal Time.utc(2006,4,2,1,59,59), twz.time |
|---|
| 211 | | assert_equal false, twz.dst? |
|---|
| 212 | | assert_equal 'EST', twz.zone |
|---|
| 213 | | twz = twz + 1 |
|---|
| 214 | | assert_equal Time.utc(2006,4,2,3), twz.time # adding 1 sec springs forward to 3:00AM EDT |
|---|
| 215 | | assert_equal true, twz.dst? |
|---|
| 216 | | assert_equal 'EDT', twz.zone |
|---|
| 217 | | twz = twz - 1 # subtracting 1 second takes goes back to 1:59:59AM EST |
|---|
| 218 | | assert_equal Time.utc(2006,4,2,1,59,59), twz.time |
|---|
| 219 | | assert_equal false, twz.dst? |
|---|
| 220 | | assert_equal 'EST', twz.zone |
|---|
| 221 | | end |
|---|
| 222 | | end |
|---|
| 223 | | |
|---|
| 224 | | def test_plus_and_minus_enforce_fall_dst_rules |
|---|
| 225 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 226 | | utc = Time.utc(2006,10,29,5,59,59) # == Oct 29 2006 01:59:59 EST; i.e., 1 second before daylight savings end |
|---|
| 227 | | twz = ActiveSupport::TimeWithZone.new(utc, @time_zone) |
|---|
| 228 | | assert_equal Time.utc(2006,10,29,1,59,59), twz.time |
|---|
| 229 | | assert_equal true, twz.dst? |
|---|
| 230 | | assert_equal 'EDT', twz.zone |
|---|
| 231 | | twz = twz + 1 |
|---|
| 232 | | assert_equal Time.utc(2006,10,29,1), twz.time # adding 1 sec falls back from 1:59:59 EDT to 1:00AM EST |
|---|
| 233 | | assert_equal false, twz.dst? |
|---|
| 234 | | assert_equal 'EST', twz.zone |
|---|
| 235 | | twz = twz - 1 |
|---|
| 236 | | assert_equal Time.utc(2006,10,29,1,59,59), twz.time # subtracting 1 sec goes back to 1:59:59AM EDT |
|---|
| 237 | | assert_equal true, twz.dst? |
|---|
| 238 | | assert_equal 'EDT', twz.zone |
|---|
| 239 | | end |
|---|
| 240 | | end |
|---|
| 241 | | |
|---|
| 242 | | def test_to_a |
|---|
| 243 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 244 | | assert_equal [45, 30, 5, 1, 2, 2000, 2, 32, false, "HST"], ActiveSupport::TimeWithZone.new( Time.utc(2000, 2, 1, 15, 30, 45), TimeZone['Hawaii'] ).to_a |
|---|
| 245 | | end |
|---|
| 246 | | end |
|---|
| 247 | | |
|---|
| 248 | | def test_to_f |
|---|
| 249 | | result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_f |
|---|
| 250 | | assert_equal 946684800.0, result |
|---|
| 251 | | assert result.is_a?(Float) |
|---|
| 252 | | end |
|---|
| 253 | | |
|---|
| 254 | | def test_to_i |
|---|
| 255 | | result = ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1), TimeZone['Hawaii'] ).to_i |
|---|
| 256 | | assert_equal 946684800, result |
|---|
| 257 | | assert result.is_a?(Integer) |
|---|
| 258 | | end |
|---|
| 259 | | |
|---|
| 260 | | def test_to_time |
|---|
| 261 | | assert_equal @twz, @twz.to_time |
|---|
| 262 | | end |
|---|
| 263 | | |
|---|
| 264 | | def test_to_date |
|---|
| 265 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 266 | | # 1 sec before midnight Jan 1 EST |
|---|
| 267 | | assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| 268 | | # midnight Jan 1 EST |
|---|
| 269 | | assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 1, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| 270 | | # 1 sec before midnight Jan 2 EST |
|---|
| 271 | | assert_equal Date.new(2000, 1, 1), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 4, 59, 59), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| 272 | | # midnight Jan 2 EST |
|---|
| 273 | | assert_equal Date.new(2000, 1, 2), ActiveSupport::TimeWithZone.new( Time.utc(2000, 1, 2, 5, 0, 0), TimeZone['Eastern Time (US & Canada)'] ).to_date |
|---|
| 274 | | end |
|---|
| 275 | | end |
|---|
| 276 | | |
|---|
| 277 | | def test_to_datetime |
|---|
| 278 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 279 | | assert_equal DateTime.civil(1999, 12, 31, 19, 0, 0, Rational(-18_000, 86_400)), @twz.to_datetime |
|---|
| 280 | | end |
|---|
| 281 | | end |
|---|
| 282 | | |
|---|
| 283 | | def test_acts_like_time |
|---|
| 284 | | assert @twz.acts_like?(:time) |
|---|
| 285 | | assert ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:time) |
|---|
| 286 | | end |
|---|
| 287 | | |
|---|
| 288 | | def test_acts_like_date |
|---|
| 289 | | assert_equal false, @twz.acts_like?(:date) |
|---|
| 290 | | assert_equal false, ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone).acts_like?(:date) |
|---|
| 291 | | end |
|---|
| 292 | | |
|---|
| 293 | | def test_is_a |
|---|
| 294 | | assert @twz.is_a?(Time) |
|---|
| 295 | | assert @twz.kind_of?(Time) |
|---|
| 296 | | assert @twz.is_a?(ActiveSupport::TimeWithZone) |
|---|
| 297 | | end |
|---|
| 298 | | |
|---|
| 299 | | def test_method_missing_with_time_return_value |
|---|
| 300 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 301 | | assert_instance_of ActiveSupport::TimeWithZone, @twz.months_since(1) |
|---|
| 302 | | assert_equal Time.utc(2000, 1, 31, 19, 0 ,0), @twz.months_since(1).time |
|---|
| 303 | | end |
|---|
| 304 | | end |
|---|
| 305 | | |
|---|
| 306 | | def test_marshal_dump_and_load |
|---|
| 307 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 308 | | marshal_str = Marshal.dump(@twz) |
|---|
| 309 | | mtime = Marshal.load(marshal_str) |
|---|
| 310 | | assert_equal Time.utc(2000, 1, 1, 0), mtime.utc |
|---|
| 311 | | assert_equal TimeZone['Eastern Time (US & Canada)'], mtime.time_zone |
|---|
| 312 | | assert_equal Time.utc(1999, 12, 31, 19), mtime.time |
|---|
| 313 | | end |
|---|
| 314 | | end |
|---|
| 315 | | |
|---|
| 316 | | def test_marshal_dump_and_load_with_tzinfo_identifier |
|---|
| 317 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 318 | | twz = ActiveSupport::TimeWithZone.new(@utc, TZInfo::Timezone.get('America/New_York')) |
|---|
| 319 | | marshal_str = Marshal.dump(twz) |
|---|
| 320 | | mtime = Marshal.load(marshal_str) |
|---|
| 321 | | assert_equal Time.utc(2000, 1, 1, 0), mtime.utc |
|---|
| 322 | | assert_equal 'America/New_York', mtime.time_zone.name |
|---|
| 323 | | assert_equal Time.utc(1999, 12, 31, 19), mtime.time |
|---|
| 324 | | end |
|---|
| 325 | | end |
|---|
| 326 | | |
|---|
| 327 | | def test_method_missing_with_non_time_return_value |
|---|
| 328 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 329 | | twz = ActiveSupport::TimeWithZone.new(Time.utc(1999,12,31,19,18,17,500), @time_zone) |
|---|
| 330 | | assert_equal 1999, twz.year |
|---|
| 331 | | assert_equal 12, twz.month |
|---|
| 332 | | assert_equal 31, twz.day |
|---|
| 333 | | assert_equal 14, twz.hour |
|---|
| 334 | | assert_equal 18, twz.min |
|---|
| 335 | | assert_equal 17, twz.sec |
|---|
| 336 | | assert_equal 500, twz.usec |
|---|
| 337 | | end |
|---|
| 338 | | end |
|---|
| 339 | | |
|---|
| 340 | | def test_usec_returns_0_when_datetime_is_wrapped |
|---|
| 341 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 342 | | twz = ActiveSupport::TimeWithZone.new(DateTime.civil(2000), @time_zone) |
|---|
| 343 | | assert_equal 0, twz.usec |
|---|
| 344 | | end |
|---|
| 345 | | end |
|---|
| 346 | | |
|---|
| 347 | | def test_utc_to_local_conversion_saves_period_in_instance_variable |
|---|
| 348 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 349 | | assert_nil @twz.instance_variable_get('@period') |
|---|
| 350 | | @twz.time |
|---|
| 351 | | assert_kind_of TZInfo::TimezonePeriod, @twz.instance_variable_get('@period') |
|---|
| 352 | | end |
|---|
| 353 | | end |
|---|
| 354 | | |
|---|
| 355 | | def test_instance_created_with_local_time_returns_correct_utc_time |
|---|
| 356 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 357 | | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(1999, 12, 31, 19)) |
|---|
| 358 | | assert_equal Time.utc(2000), twz.utc |
|---|
| 359 | | end |
|---|
| 360 | | end |
|---|
| 361 | | |
|---|
| 362 | | def test_instance_created_with_local_time_enforces_spring_dst_rules |
|---|
| 363 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 364 | | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,4,2,2)) # first second of DST |
|---|
| 365 | | assert_equal Time.utc(2006,4,2,3), twz.time # springs forward to 3AM |
|---|
| 366 | | assert_equal true, twz.dst? |
|---|
| 367 | | assert_equal 'EDT', twz.zone |
|---|
| 368 | | end |
|---|
| 369 | | end |
|---|
| 370 | | |
|---|
| 371 | | def test_instance_created_with_local_time_enforces_fall_dst_rules |
|---|
| 372 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 373 | | twz = ActiveSupport::TimeWithZone.new(nil, @time_zone, Time.utc(2006,10,29,1)) # 1AM can be either DST or non-DST; we'll pick DST |
|---|
| 374 | | assert_equal Time.utc(2006,10,29,1), twz.time |
|---|
| 375 | | assert_equal true, twz.dst? |
|---|
| 376 | | assert_equal 'EDT', twz.zone |
|---|
| 377 | | end |
|---|
| 378 | | end |
|---|
| 379 | | |
|---|
| 380 | | def test_ruby_19_weekday_name_query_methods |
|---|
| 381 | | ruby_19_or_greater = RUBY_VERSION >= '1.9' |
|---|
| 382 | | %w(sunday? monday? tuesday? wednesday? thursday? friday? saturday?).each do |name| |
|---|
| 383 | | assert_equal ruby_19_or_greater, @twz.respond_to?(name) |
|---|
| 384 | | end |
|---|
| 385 | | end |
|---|
| 386 | | |
|---|
| 387 | | def test_utc_to_local_conversion_with_far_future_datetime |
|---|
| 388 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 389 | | assert_equal [0,0,19,31,12,2049], ActiveSupport::TimeWithZone.new(DateTime.civil(2050), @time_zone).to_a[0,6] |
|---|
| 390 | | end |
|---|
| 391 | | end |
|---|
| 392 | | |
|---|
| 393 | | def test_local_to_utc_conversion_with_far_future_datetime |
|---|
| 394 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 395 | | assert_equal DateTime.civil(2050).to_f, ActiveSupport::TimeWithZone.new(nil, @time_zone, DateTime.civil(2049,12,31,19)).to_f |
|---|
| 396 | | end |
|---|
| 397 | | end |
|---|
| 398 | | end |
|---|
| 399 | | |
|---|
| 400 | | class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase |
|---|
| 401 | | def setup |
|---|
| 402 | | @t, @dt = Time.utc(2000), DateTime.civil(2000) |
|---|
| 403 | | end |
|---|
| 404 | | |
|---|
| 405 | | def teardown |
|---|
| 406 | | Time.zone = nil |
|---|
| 407 | | end |
|---|
| 408 | | |
|---|
| 409 | | def test_in_time_zone |
|---|
| 410 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 411 | | Time.use_zone 'Alaska' do |
|---|
| 412 | | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone.inspect |
|---|
| 413 | | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone.inspect |
|---|
| 414 | | end |
|---|
| 415 | | Time.use_zone 'Hawaii' do |
|---|
| 416 | | assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone.inspect |
|---|
| 417 | | assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone.inspect |
|---|
| 418 | | end |
|---|
| 419 | | Time.use_zone nil do |
|---|
| 420 | | assert_equal @t, @t.in_time_zone |
|---|
| 421 | | assert_equal @dt, @dt.in_time_zone |
|---|
| 422 | | end |
|---|
| 423 | | end |
|---|
| 424 | | end |
|---|
| 425 | | |
|---|
| 426 | | def test_in_time_zone_with_argument |
|---|
| 427 | | silence_warnings do # silence warnings raised by tzinfo gem |
|---|
| 428 | | Time.use_zone 'Eastern Time (US & Canada)' do # Time.zone will not affect #in_time_zone(zone) |
|---|
| 429 | | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone('Alaska').inspect |
|---|
| 430 | | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @dt.in_time_zone('Alaska').inspect |
|---|
| 431 | | assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @t.in_time_zone('Hawaii').inspect |
|---|
| 432 | | assert_equal 'Fri, 31 Dec 1999 14:00:00 HST -10:00', @dt.in_time_zone('Hawaii').inspect |
|---|
| 433 | | assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @t.in_time_zone('UTC').inspect |
|---|
| 434 | | assert_equal 'Sat, 01 Jan 2000 00:00:00 UTC +00:00', @dt.in_time_zone('UTC').inspect |
|---|
| 435 | | assert_equal 'Fri, 31 Dec 1999 15:00:00 AKST -09:00', @t.in_time_zone(-9.hours).inspect |
|---|
| 436 | | end |
|---|
| 437 | | end |
|---|
| 438 | | end |
|---|
| 439 | | |
|---|
| 440 | | def test_in_time_zone_with_time_l |
|---|