From df57f53d09575f8bc6542f87a3384240b66f9b4b Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Mon, 5 Mar 2007 08:34:42 +0000 Subject: [PATCH] Time#since overflows to DateTime. Introduce Time#to_datetime. Closes #7715. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6335 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activesupport/CHANGELOG | 2 +- .../active_support/core_ext/date_time/conversions.rb | 5 +++++ .../lib/active_support/core_ext/time/calculations.rb | 2 ++ .../lib/active_support/core_ext/time/conversions.rb | 5 +++++ activesupport/test/core_ext/time_ext_test.rb | 12 ++++++++++++ 5 files changed, 25 insertions(+), 1 deletion(-) diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index dab56d1501..40e24d4ad4 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,6 +1,6 @@ *SVN* -* Out-of-range Time calculations transparently overflow to DateTime. #7706 [Geoff Buesing] +* Out-of-range Time calculations transparently overflow to DateTime. Introduce Time#to_datetime. #7706, #7715 [Geoff Buesing] * DateTime calculations analogous to the Date and Time extensions. #7693 [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/date_time/conversions.rb b/activesupport/lib/active_support/core_ext/date_time/conversions.rb index 48a13e1fde..926a168cec 100644 --- a/activesupport/lib/active_support/core_ext/date_time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/date_time/conversions.rb @@ -22,6 +22,11 @@ module ActiveSupport #:nodoc: def to_date ::Date.new(year, month, day) end + + # To be able to keep Times and DateTimes interchangeable on conversions + def to_datetime + self + end end end end diff --git a/activesupport/lib/active_support/core_ext/time/calculations.rb b/activesupport/lib/active_support/core_ext/time/calculations.rb index 1c2daf0606..1fdfbcdf45 100644 --- a/activesupport/lib/active_support/core_ext/time/calculations.rb +++ b/activesupport/lib/active_support/core_ext/time/calculations.rb @@ -91,6 +91,8 @@ module ActiveSupport #:nodoc: f = seconds.since(self) final_dst = f.dst? ? 1 : 0 (seconds.abs >= 86400 && initial_dst != final_dst) ? f + (initial_dst - final_dst).hours : f + rescue + self.to_datetime.since(seconds) end alias :in :since diff --git a/activesupport/lib/active_support/core_ext/time/conversions.rb b/activesupport/lib/active_support/core_ext/time/conversions.rb index f2ff0e06a8..903dd02031 100644 --- a/activesupport/lib/active_support/core_ext/time/conversions.rb +++ b/activesupport/lib/active_support/core_ext/time/conversions.rb @@ -28,6 +28,11 @@ module ActiveSupport #:nodoc: def to_time self end + + # converts to a Ruby DateTime instance; preserves utc offset + def to_datetime + ::DateTime.civil(year, month, day, hour, min, sec, Rational(utc_offset, 86400), 0) + end end end end diff --git a/activesupport/test/core_ext/time_ext_test.rb b/activesupport/test/core_ext/time_ext_test.rb index ebeaf264a4..8616129348 100644 --- a/activesupport/test/core_ext/time_ext_test.rb +++ b/activesupport/test/core_ext/time_ext_test.rb @@ -167,6 +167,8 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_equal Time.local(2005,2,22,11,10,10), Time.local(2005,2,22,10,10,10).since(3600) assert_equal Time.local(2005,2,24,10,10,10), Time.local(2005,2,22,10,10,10).since(86400*2) assert_equal Time.local(2005,2,24,11,10,35), Time.local(2005,2,22,10,10,10).since(86400*2 + 3600 + 25) + # when out of range of Time, returns a DateTime + assert_equal DateTime.civil(2038,1,20,11,59,59), Time.utc(2038,1,18,11,59,59).since(86400*2) end def test_daylight_savings_time_crossings_forward_start @@ -351,6 +353,16 @@ class TimeExtCalculationsTest < Test::Unit::TestCase assert_nothing_raised { Time.now.xmlschema } end + def test_to_datetime + assert_equal Time.utc(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, 0, 0) + with_timezone 'US/Eastern' do + assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, DateTime.now.offset, 0) + end + with_timezone 'NZ' do + assert_equal Time.local(2005, 2, 21, 17, 44, 30).to_datetime, DateTime.civil(2005, 2, 21, 17, 44, 30, DateTime.now.offset, 0) + end + end + protected def with_timezone new_tz='US/Eastern' old_tz, ENV['TZ'] = ENV['TZ'], new_tz