class TimeWithZoneMethodsForTimeAndDateTimeTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1007
def setup
  @t, @dt, @zone = Time.utc(2000), DateTime.civil(2000), Time.zone
end
teardown() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1011
def teardown
  Time.zone = @zone
end
test_current_returns_time_now_when_zone_not_set() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1170
def test_current_returns_time_now_when_zone_not_set
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2000)) do
      assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone)
      assert_equal Time.local(2000), Time.current
    end
  end
end
test_current_returns_time_zone_now_when_zone_set() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1179
def test_current_returns_time_zone_now_when_zone_set
  Time.zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
  with_env_tz "US/Eastern" do
    Time.stub(:now, Time.local(2000)) do
      assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone)
      assert_equal "Eastern Time (US & Canada)", Time.current.time_zone.name
      assert_equal Time.utc(2000), Time.current.time
    end
  end
end
test_find_zone_with_bang_raises_if_time_zone_can_not_be_found() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1158
def test_find_zone_with_bang_raises_if_time_zone_can_not_be_found
  assert_raise(ArgumentError) { Time.find_zone!("No such timezone exists") }
  assert_raise(ArgumentError) { Time.find_zone!(-15.hours) }
  assert_raise(ArgumentError) { Time.find_zone!(Object.new) }
end
test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1152
def test_find_zone_without_bang_returns_nil_if_time_zone_can_not_be_found
  assert_nil Time.find_zone("No such timezone exists")
  assert_nil Time.find_zone(-15.hours)
  assert_nil Time.find_zone(Object.new)
end
test_in_time_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1015
def test_in_time_zone
  Time.use_zone "Alaska" do
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", @t.in_time_zone.inspect
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", @dt.in_time_zone.inspect
  end
  Time.use_zone "Hawaii" do
    assert_equal "Fri, 31 Dec 1999 14:00:00 HST -10:00", @t.in_time_zone.inspect
    assert_equal "Fri, 31 Dec 1999 14:00:00 HST -10:00", @dt.in_time_zone.inspect
  end
  Time.use_zone nil do
    assert_equal @t, @t.in_time_zone
    assert_equal @dt, @dt.in_time_zone
  end
end
test_in_time_zone_with_argument() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1037
def test_in_time_zone_with_argument
  Time.use_zone "Eastern Time (US & Canada)" do # Time.zone will not affect #in_time_zone(zone)
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", @t.in_time_zone("Alaska").inspect
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", @dt.in_time_zone("Alaska").inspect
    assert_equal "Fri, 31 Dec 1999 14:00:00 HST -10:00", @t.in_time_zone("Hawaii").inspect
    assert_equal "Fri, 31 Dec 1999 14:00:00 HST -10:00", @dt.in_time_zone("Hawaii").inspect
    assert_equal "Sat, 01 Jan 2000 00:00:00 UTC +00:00", @t.in_time_zone("UTC").inspect
    assert_equal "Sat, 01 Jan 2000 00:00:00 UTC +00:00", @dt.in_time_zone("UTC").inspect
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", @t.in_time_zone(-9.hours).inspect
  end
end
test_in_time_zone_with_invalid_argument() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1049
def test_in_time_zone_with_invalid_argument
  assert_raise(ArgumentError) {  @t.in_time_zone("No such timezone exists") }
  assert_raise(ArgumentError) { @dt.in_time_zone("No such timezone exists") }
  assert_raise(ArgumentError) {  @t.in_time_zone(-15.hours) }
  assert_raise(ArgumentError) { @dt.in_time_zone(-15.hours) }
  assert_raise(ArgumentError) {  @t.in_time_zone(Object.new) }
  assert_raise(ArgumentError) { @dt.in_time_zone(Object.new) }
end
test_in_time_zone_with_time_local_instance() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1058
def test_in_time_zone_with_time_local_instance
  with_env_tz "US/Eastern" do
    time = Time.local(1999, 12, 31, 19) # == Time.utc(2000)
    assert_equal "Fri, 31 Dec 1999 15:00:00 AKST -09:00", time.in_time_zone("Alaska").inspect
  end
end
test_localtime() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1065
def test_localtime
  Time.zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
  assert_equal @dt.in_time_zone.localtime, @dt.in_time_zone.utc.to_time.getlocal
end
test_nil_time_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1030
def test_nil_time_zone
  Time.use_zone nil do
    assert !@t.in_time_zone.respond_to?(:period), "no period method"
    assert !@dt.in_time_zone.respond_to?(:period), "no period method"
  end
end
test_time_in_time_zone_doesnt_affect_receiver() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1190
def test_time_in_time_zone_doesnt_affect_receiver
  with_env_tz "Europe/London" do
    time = Time.local(2000, 7, 1)
    time_with_zone = time.in_time_zone("Eastern Time (US & Canada)")
    assert_equal Time.utc(2000, 6, 30, 23, 0, 0), time_with_zone
    assert_not time.utc?, "time expected to be local, but is UTC"
  end
end
test_time_zone_getter_and_setter() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1094
def test_time_zone_getter_and_setter
  Time.zone = ActiveSupport::TimeZone["Alaska"]
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
  Time.zone = "Alaska"
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
  Time.zone = -9.hours
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
  Time.zone = nil
  assert_nil Time.zone
end
test_time_zone_getter_and_setter_with_zone_default_set() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1105
def test_time_zone_getter_and_setter_with_zone_default_set
  old_zone_default = Time.zone_default
  Time.zone_default = ActiveSupport::TimeZone["Alaska"]
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
  Time.zone = ActiveSupport::TimeZone["Hawaii"]
  assert_equal ActiveSupport::TimeZone["Hawaii"], Time.zone
  Time.zone = nil
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
ensure
  Time.zone_default = old_zone_default
end
test_time_zone_setter_is_thread_safe() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1117
def test_time_zone_setter_is_thread_safe
  Time.use_zone "Paris" do
    t1 = Thread.new { Time.zone = "Alaska" }.join
    t2 = Thread.new { Time.zone = "Hawaii" }.join
    assert t1.stop?, "Thread 1 did not finish running"
    assert t2.stop?, "Thread 2 did not finish running"
    assert_equal ActiveSupport::TimeZone["Paris"], Time.zone
    assert_equal ActiveSupport::TimeZone["Alaska"], t1[:time_zone]
    assert_equal ActiveSupport::TimeZone["Hawaii"], t2[:time_zone]
  end
end
test_time_zone_setter_with_find_zone_without_bang() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1164
def test_time_zone_setter_with_find_zone_without_bang
  assert_nil Time.zone = Time.find_zone("No such timezone exists")
  assert_nil Time.zone = Time.find_zone(-15.hours)
  assert_nil Time.zone = Time.find_zone(Object.new)
end
test_time_zone_setter_with_invalid_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1146
def test_time_zone_setter_with_invalid_zone
  assert_raise(ArgumentError) { Time.zone = "No such timezone exists" }
  assert_raise(ArgumentError) { Time.zone = -15.hours }
  assert_raise(ArgumentError) { Time.zone = Object.new }
end
test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_quails_time_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1138
def test_time_zone_setter_with_tzinfo_timezone_identifier_does_lookup_and_wraps_in_quails_time_zone
  Time.zone = "America/New_York"
  assert_kind_of ActiveSupport::TimeZone, Time.zone
  assert_equal "America/New_York", Time.zone.tzinfo.name
  assert_equal "America/New_York", Time.zone.name
  assert_equal(-18_000, Time.zone.utc_offset)
end
test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_quails_time_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1129
def test_time_zone_setter_with_tzinfo_timezone_object_wraps_in_quails_time_zone
  tzinfo = TZInfo::Timezone.get("America/New_York")
  Time.zone = tzinfo
  assert_kind_of ActiveSupport::TimeZone, Time.zone
  assert_equal tzinfo, Time.zone.tzinfo
  assert_equal "America/New_York", Time.zone.name
  assert_equal(-18_000, Time.zone.utc_offset)
end
test_use_zone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1070
def test_use_zone
  Time.zone = "Alaska"
  Time.use_zone "Hawaii" do
    assert_equal ActiveSupport::TimeZone["Hawaii"], Time.zone
  end
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
end
test_use_zone_raises_on_invalid_timezone() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1086
def test_use_zone_raises_on_invalid_timezone
  Time.zone = "Alaska"
  assert_raise ArgumentError do
    Time.use_zone("No such timezone exists") {}
  end
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
end
test_use_zone_with_exception_raised() click to toggle source
# File activesupport/test/core_ext/time_with_zone_test.rb, line 1078
def test_use_zone_with_exception_raised
  Time.zone = "Alaska"
  assert_raise RuntimeError do
    Time.use_zone("Hawaii") { raise RuntimeError }
  end
  assert_equal ActiveSupport::TimeZone["Alaska"], Time.zone
end