From 725ebc9e102422f5573a875e7bd1a861802513fe Mon Sep 17 00:00:00 2001 From: John Hawthorn Date: Fri, 7 Jun 2024 21:29:29 -0700 Subject: [PATCH] Remove code for DateTime-backed TimeWithZone At one point (I believe until ruby-1.8.0) Time could only represent values between 1970 and the integer overflow in 2038. On modern Ruby there does not seem to be a limit. >> Time.at(2**128) => 10783118943836478994022445751222-08-06 01:04:16 -0700 TimeWithZone will also convert a DateTime to a Time when initialized with one, so the code we had to catch this overflow and to deal with DateTime is dead. This commit removes this code and adjusts the test to be more general (the old test passed but we might as well keep a better version of the test to check that we have a large both negative and positive range). Co-authored-by: Adam Hess --- activesupport/lib/active_support/time_with_zone.rb | 5 ++--- activesupport/test/core_ext/time_with_zone_test.rb | 7 ++++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/activesupport/lib/active_support/time_with_zone.rb b/activesupport/lib/active_support/time_with_zone.rb index 058ab790b4..245c621d94 100644 --- a/activesupport/lib/active_support/time_with_zone.rb +++ b/activesupport/lib/active_support/time_with_zone.rb @@ -300,7 +300,7 @@ def +(other) if duration_of_variable_length?(other) method_missing(:+, other) else - result = utc.acts_like?(:date) ? utc.since(other) : utc + other rescue utc.since(other) + result = utc + other result.in_time_zone(time_zone) end end @@ -336,7 +336,7 @@ def -(other) elsif duration_of_variable_length?(other) method_missing(:-, other) else - result = utc.acts_like?(:date) ? utc.ago(other) : utc - other rescue utc.ago(other) + result = utc - other result.in_time_zone(time_zone) end end @@ -537,7 +537,6 @@ def respond_to?(sym, include_priv = false) # Ensure proxy class responds to all methods that underlying time instance # responds to. def respond_to_missing?(sym, include_priv) - return false if sym.to_sym == :acts_like_date? time.respond_to?(sym, include_priv) end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index e1f05aac72..fdb1c4e405 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -394,9 +394,10 @@ def test_plus_with_integer_when_self_wraps_datetime assert_equal DateTime.civil(1999, 12, 31, 19, 0, 5), (twz + 5).time end - def test_plus_when_crossing_time_class_limit - twz = ActiveSupport::TimeWithZone.new(Time.utc(2038, 1, 19), @time_zone) - assert_equal [0, 0, 19, 19, 1, 2038], (twz + 86_400).to_a[0, 6] + def test_no_limit_on_times + twz = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), @time_zone) + assert_equal [0, 0, 19, 31, 12, 11999], (twz + 10_000.years).to_a[0, 6] + assert_equal [0, 0, 19, 31, 12, -8001], (twz - 10_000.years).to_a[0, 6] end def test_plus_with_duration