Skip to content

Commit d12a42d

Browse files
authored
Merge pull request rails#53546 from matthewd/dst_deprecation_fix
Fix deprecation warning caused by DST
2 parents f8d74e2 + df65856 commit d12a42d

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

activesupport/lib/active_support/core_ext/time/compatibility.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,18 @@ def to_time
1515
end
1616

1717
def preserve_timezone # :nodoc:
18-
active_support_local_zone == zone || super
18+
system_local_time? || super
1919
end
2020

2121
private
22+
def system_local_time?
23+
if ::Time.equal?(self.class)
24+
zone = self.zone
25+
String === zone &&
26+
(zone != "UTC" || active_support_local_zone == "UTC")
27+
end
28+
end
29+
2230
@@active_support_local_tz = nil
2331

2432
def active_support_local_zone

activesupport/test/core_ext/date_and_time_compatibility_test.rb

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def setup
1212
@date_time = DateTime.new(2016, 4, 23, 14, 11, 12, 0)
1313
@utc_offset = 3600
1414
@system_offset = -14400
15+
@system_dst_offset = -18000
1516
@zone = ActiveSupport::TimeZone["London"]
1617
end
1718

@@ -43,7 +44,7 @@ def test_time_to_time_does_not_preserve_time_zone
4344
end
4445
end
4546

46-
def test_time_to_time_without_preserve_configured
47+
def test_time_to_time_on_utc_value_without_preserve_configured
4748
with_preserve_timezone(nil) do
4849
with_env_tz "US/Eastern" do
4950
source = Time.new(2016, 4, 23, 15, 11, 12)
@@ -60,6 +61,24 @@ def test_time_to_time_without_preserve_configured
6061
end
6162
end
6263

64+
with_preserve_timezone(nil) do
65+
with_env_tz "US/Eastern" do
66+
source = Time.new(2016, 11, 23, 15, 11, 12)
67+
# No warning because it's already local
68+
base_time = source.to_time
69+
70+
utc_time = base_time.getutc
71+
converted_time = assert_deprecated(ActiveSupport.deprecator) { utc_time.to_time }
72+
73+
assert_equal source, base_time
74+
assert_equal source, converted_time
75+
assert_equal @system_dst_offset, base_time.utc_offset
76+
assert_equal @system_dst_offset, converted_time.utc_offset
77+
end
78+
end
79+
end
80+
81+
def test_time_to_time_on_offset_value_without_preserve_configured
6382
with_preserve_timezone(nil) do
6483
with_env_tz "US/Eastern" do
6584
foreign_time = Time.new(2016, 4, 23, 15, 11, 12, in: "-0700")
@@ -70,6 +89,43 @@ def test_time_to_time_without_preserve_configured
7089
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
7190
end
7291
end
92+
93+
with_preserve_timezone(nil) do
94+
with_env_tz "US/Eastern" do
95+
foreign_time = Time.new(2016, 11, 23, 15, 11, 12, in: "-0700")
96+
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }
97+
98+
assert_equal foreign_time, converted_time
99+
assert_equal @system_dst_offset, converted_time.utc_offset
100+
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
101+
end
102+
end
103+
end
104+
105+
def test_time_to_time_on_tzinfo_value_without_preserve_configured
106+
foreign_zone = ActiveSupport::TimeZone["America/Phoenix"]
107+
108+
with_preserve_timezone(nil) do
109+
with_env_tz "US/Eastern" do
110+
foreign_time = foreign_zone.tzinfo.utc_to_local(Time.new(2016, 4, 23, 15, 11, 12, in: "-0700"))
111+
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }
112+
113+
assert_equal foreign_time, converted_time
114+
assert_equal @system_offset, converted_time.utc_offset
115+
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
116+
end
117+
end
118+
119+
with_preserve_timezone(nil) do
120+
with_env_tz "US/Eastern" do
121+
foreign_time = foreign_zone.tzinfo.utc_to_local(Time.new(2016, 11, 23, 15, 11, 12, in: "-0700"))
122+
converted_time = assert_deprecated(ActiveSupport.deprecator) { foreign_time.to_time }
123+
124+
assert_equal foreign_time, converted_time
125+
assert_equal @system_dst_offset, converted_time.utc_offset
126+
assert_not_equal foreign_time.utc_offset, converted_time.utc_offset
127+
end
128+
end
73129
end
74130

75131
def test_time_to_time_frozen_preserves_timezone

0 commit comments

Comments
 (0)