Skip to content

Commit 595c3cc

Browse files
committed
Re-roll deprecation of to_time_preserves_timezone
We realised the previous deprecation hadn't been warning for all users.
1 parent 43fcdfa commit 595c3cc

File tree

4 files changed

+58
-2
lines changed

4 files changed

+58
-2
lines changed

activesupport/lib/active_support.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def self.to_time_preserves_timezone
117117
def self.to_time_preserves_timezone=(value)
118118
unless value
119119
ActiveSupport.deprecator.warn(
120-
"Support for the pre-Ruby 2.4 behavior of to_time has been deprecated and will be removed in Rails 7.2."
120+
"Support for the pre-Ruby 2.4 behavior of to_time has been deprecated and will be removed in Rails 8.0."
121121
)
122122
end
123123

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "active_support/core_ext/module/attribute_accessors"
4+
require "active_support/core_ext/module/redefine_method"
45

56
module DateAndTime
67
module Compatibility
@@ -11,7 +12,33 @@ module Compatibility
1112
# of the receiver. For backwards compatibility we're overriding
1213
# this behavior, but new apps will have an initializer that sets
1314
# this to true, because the new behavior is preferred.
14-
mattr_accessor :preserve_timezone, instance_writer: false, default: false
15+
mattr_accessor :preserve_timezone, instance_accessor: false, default: nil
16+
17+
singleton_class.silence_redefinition_of_method :preserve_timezone
18+
19+
#--
20+
# This re-implements the behaviour of the mattr_reader, instead
21+
# of prepending on to it, to avoid overcomplicating a module that
22+
# is in turn included in several places. This will all go away in
23+
# Rails 8.0 anyway.
24+
def self.preserve_timezone # :nodoc:
25+
if @@preserve_timezone.nil?
26+
# Only warn once, the first time the value is used (which should
27+
# be the first time #to_time is called).
28+
ActiveSupport.deprecator.warn(
29+
"to_time will always preserve the timezone offset of the receiver in Rails 8.0. " \
30+
"To opt in to the new behavior, set `ActiveSupport.to_time_preserves_timezone = true`."
31+
)
32+
33+
@@preserve_timezone = false
34+
end
35+
36+
@@preserve_timezone
37+
end
38+
39+
def preserve_timezone # :nodoc:
40+
Compatibility.preserve_timezone
41+
end
1542

1643
# Change the output of <tt>ActiveSupport::TimeZone.utc_to_local</tt>.
1744
#

activesupport/test/core_ext/date_and_time_compatibility_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,20 @@ def test_to_time_preserves_timezone_is_deprecated
287287
assert_deprecated(ActiveSupport.deprecator) do
288288
ActiveSupport.to_time_preserves_timezone = false
289289
end
290+
291+
assert_deprecated(ActiveSupport.deprecator) do
292+
ActiveSupport.to_time_preserves_timezone = nil
293+
end
294+
295+
# When set to nil, the first call will report a deprecation,
296+
# then switch the configured value to (and return) false.
297+
assert_deprecated(ActiveSupport.deprecator) do
298+
assert_equal false, ActiveSupport.to_time_preserves_timezone
299+
end
300+
301+
assert_not_deprecated(ActiveSupport.deprecator) do
302+
ActiveSupport.to_time_preserves_timezone
303+
end
290304
ensure
291305
ActiveSupport.deprecator.silence do
292306
ActiveSupport.to_time_preserves_timezone = current_preserve_tz

activesupport/test/core_ext/time_with_zone_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,21 @@ def test_to_time_without_preserve_timezone
543543
end
544544
end
545545

546+
def test_to_time_without_preserve_timezone_configured
547+
with_preserve_timezone(nil) do
548+
with_env_tz "US/Eastern" do
549+
time = assert_deprecated(ActiveSupport.deprecator) { @twz.to_time }
550+
551+
assert_equal Time, time.class
552+
assert_equal time.object_id, @twz.to_time.object_id
553+
assert_equal Time.local(1999, 12, 31, 19), time
554+
assert_equal Time.local(1999, 12, 31, 19).utc_offset, time.utc_offset
555+
556+
assert_equal false, ActiveSupport.to_time_preserves_timezone
557+
end
558+
end
559+
end
560+
546561
def test_to_date
547562
# 1 sec before midnight Jan 1 EST
548563
assert_equal Date.new(1999, 12, 31), ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1, 4, 59, 59), ActiveSupport::TimeZone["Eastern Time (US & Canada)"]).to_date

0 commit comments

Comments
 (0)