class DateAndTimeCompatibilityTest

Public Instance Methods

setup() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 10
def setup
  @utc_time = Time.utc(2016, 4, 23, 14, 11, 12)
  @date_time = DateTime.new(2016, 4, 23, 14, 11, 12, 0)
  @utc_offset = 3600
  @system_offset = -14400
  @zone = ActiveSupport::TimeZone["London"]
end
test_datetime_to_time_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 89
def test_datetime_to_time_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = DateTime.new(2016, 4, 23, 15, 11, 12, Rational(1, 24))
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
    end
  end
end
test_datetime_to_time_frozen_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 116
def test_datetime_to_time_frozen_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = DateTime.new(2016, 4, 23, 15, 11, 12, Rational(1, 24)).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_datetime_to_time_frozen_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 102
def test_datetime_to_time_frozen_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = DateTime.new(2016, 4, 23, 15, 11, 12, Rational(1, 24)).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_datetime_to_time_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 76
def test_datetime_to_time_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = DateTime.new(2016, 4, 23, 15, 11, 12, Rational(1, 24))
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
    end
  end
end
test_string_to_time_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 235
def test_string_to_time_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = "2016-04-23T15:11:12+01:00"
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
    end
  end
end
test_string_to_time_frozen_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 262
def test_string_to_time_frozen_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = "2016-04-23T15:11:12+01:00".freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_string_to_time_frozen_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 248
def test_string_to_time_frozen_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = "2016-04-23T15:11:12+01:00".freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_string_to_time_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 222
def test_string_to_time_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = "2016-04-23T15:11:12+01:00"
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
    end
  end
end
test_time_to_time_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 32
def test_time_to_time_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = Time.new(2016, 4, 23, 15, 11, 12, 3600)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_equal source.object_id, time.object_id
    end
  end
end
test_time_to_time_frozen_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 61
def test_time_to_time_frozen_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = Time.new(2016, 4, 23, 15, 11, 12, 3600).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_equal source.object_id, time.object_id
      assert_not_predicate time, :frozen?
    end
  end
end
test_time_to_time_frozen_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 46
def test_time_to_time_frozen_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = Time.new(2016, 4, 23, 15, 11, 12, 3600).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_equal source.object_id, time.object_id
      assert_predicate time, :frozen?
    end
  end
end
test_time_to_time_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 18
def test_time_to_time_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = Time.new(2016, 4, 23, 15, 11, 12, 3600)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_equal source.object_id, time.object_id
    end
  end
end
test_twz_to_time_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 152
def test_twz_to_time_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = ActiveSupport::TimeWithZone.new(@utc_time, @zone)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @system_offset, time.utc_offset

      source = ActiveSupport::TimeWithZone.new(@date_time, @zone)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @date_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @system_offset, time.utc_offset
    end
  end
end
test_twz_to_time_frozen_does_not_preserve_time_zone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 198
def test_twz_to_time_frozen_does_not_preserve_time_zone
  with_preserve_timezone(false) do
    with_env_tz "US/Eastern" do
      source = ActiveSupport::TimeWithZone.new(@utc_time, @zone).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_predicate time, :frozen?

      source = ActiveSupport::TimeWithZone.new(@date_time, @zone).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @date_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @system_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_twz_to_time_frozen_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 174
def test_twz_to_time_frozen_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = ActiveSupport::TimeWithZone.new(@utc_time, @zone).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_not_predicate time, :frozen?

      source = ActiveSupport::TimeWithZone.new(@date_time, @zone).freeze
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @date_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @utc_offset, time.utc_offset
      assert_not_predicate time, :frozen?
    end
  end
end
test_twz_to_time_preserves_timezone() click to toggle source
# File activesupport/test/core_ext/date_and_time_compatibility_test.rb, line 130
def test_twz_to_time_preserves_timezone
  with_preserve_timezone(true) do
    with_env_tz "US/Eastern" do
      source = ActiveSupport::TimeWithZone.new(@utc_time, @zone)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @utc_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @utc_offset, time.utc_offset

      source = ActiveSupport::TimeWithZone.new(@date_time, @zone)
      time = source.to_time

      assert_instance_of Time, time
      assert_equal @date_time, time.getutc
      assert_instance_of Time, time.getutc
      assert_equal @utc_offset, time.utc_offset
    end
  end
end