Skip to content

Commit

Permalink
Merge pull request rails#53546 from matthewd/dst_deprecation_fix
Browse files Browse the repository at this point in the history
Fix deprecation warning caused by DST
  • Loading branch information
matthewd authored Nov 6, 2024
2 parents f8d74e2 + df65856 commit d12a42d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
10 changes: 9 additions & 1 deletion activesupport/lib/active_support/core_ext/time/compatibility.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ def to_time
end

def preserve_timezone # :nodoc:
active_support_local_zone == zone || super
system_local_time? || super
end

private
def system_local_time?
if ::Time.equal?(self.class)
zone = self.zone
String === zone &&
(zone != "UTC" || active_support_local_zone == "UTC")
end
end

@@active_support_local_tz = nil

def active_support_local_zone
Expand Down
58 changes: 57 additions & 1 deletion activesupport/test/core_ext/date_and_time_compatibility_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def setup
@date_time = DateTime.new(2016, 4, 23, 14, 11, 12, 0)
@utc_offset = 3600
@system_offset = -14400
@system_dst_offset = -18000
@zone = ActiveSupport::TimeZone["London"]
end

Expand Down Expand Up @@ -43,7 +44,7 @@ def test_time_to_time_does_not_preserve_time_zone
end
end

def test_time_to_time_without_preserve_configured
def test_time_to_time_on_utc_value_without_preserve_configured
with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
source = Time.new(2016, 4, 23, 15, 11, 12)
Expand All @@ -60,6 +61,24 @@ def test_time_to_time_without_preserve_configured
end
end

with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
source = Time.new(2016, 11, 23, 15, 11, 12)
# No warning because it's already local
base_time = source.to_time

utc_time = base_time.getutc
converted_time = assert_deprecated(ActiveSupport.deprecator) { utc_time.to_time }

assert_equal source, base_time
assert_equal source, converted_time
assert_equal @system_dst_offset, base_time.utc_offset
assert_equal @system_dst_offset, converted_time.utc_offset
end
end
end

def test_time_to_time_on_offset_value_without_preserve_configured
with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
foreign_time = Time.new(2016, 4, 23, 15, 11, 12, in: "-0700")
Expand All @@ -70,6 +89,43 @@ def test_time_to_time_without_preserve_configured
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
end
end

with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
foreign_time = Time.new(2016, 11, 23, 15, 11, 12, in: "-0700")
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }

assert_equal foreign_time, converted_time
assert_equal @system_dst_offset, converted_time.utc_offset
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
end
end
end

def test_time_to_time_on_tzinfo_value_without_preserve_configured
foreign_zone = ActiveSupport::TimeZone["America/Phoenix"]

with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
foreign_time = foreign_zone.tzinfo.utc_to_local(Time.new(2016, 4, 23, 15, 11, 12, in: "-0700"))
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }

assert_equal foreign_time, converted_time
assert_equal @system_offset, converted_time.utc_offset
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
end
end

with_preserve_timezone(nil) do
with_env_tz "US/Eastern" do
foreign_time = foreign_zone.tzinfo.utc_to_local(Time.new(2016, 11, 23, 15, 11, 12, in: "-0700"))
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }

assert_equal foreign_time, converted_time
assert_equal @system_dst_offset, converted_time.utc_offset
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
end
end
end

def test_time_to_time_frozen_preserves_timezone
Expand Down

0 comments on commit d12a42d

Please sign in to comment.