diff options
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/CHANGELOG | 2 | ||||
-rw-r--r-- | activesupport/lib/active_support/core_ext/time/zones.rb | 5 | ||||
-rw-r--r-- | activesupport/lib/active_support/duration.rb | 6 | ||||
-rw-r--r-- | activesupport/test/core_ext/duration_test.rb | 42 | ||||
-rw-r--r-- | activesupport/test/core_ext/time_with_zone_test.rb | 24 |
5 files changed, 76 insertions, 3 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG index cfdefed91e..a911361d3b 100644 --- a/activesupport/CHANGELOG +++ b/activesupport/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Duration #since and #ago with no argument (e.g., 5.days.ago) return TimeWithZone when config.time_zone is set. Introducing Time.current, which returns Time.zone.now if config.time_zone is set, otherwise just returns Time.now [Geoff Buesing] + * Time#since behaves correctly when passed a Duration. Closes #11527 [kemiller] * Add #getutc alias for DateTime#utc [Geoff Buesing] diff --git a/activesupport/lib/active_support/core_ext/time/zones.rb b/activesupport/lib/active_support/core_ext/time/zones.rb index d2b95698e5..3ffc71407c 100644 --- a/activesupport/lib/active_support/core_ext/time/zones.rb +++ b/activesupport/lib/active_support/core_ext/time/zones.rb @@ -35,6 +35,11 @@ module ActiveSupport #:nodoc: ::Time.zone = old_zone end + # Returns Time.zone.now when config.time_zone is set, otherwise just returns Time.now. + def current + ::Time.zone_default ? ::Time.zone.now : ::Time.now + end + private def get_zone(time_zone) return time_zone if time_zone.nil? || time_zone.is_a?(TimeZone) diff --git a/activesupport/lib/active_support/duration.rb b/activesupport/lib/active_support/duration.rb index ff51b49dcf..8eae85d38e 100644 --- a/activesupport/lib/active_support/duration.rb +++ b/activesupport/lib/active_support/duration.rb @@ -51,14 +51,14 @@ module ActiveSupport # Calculates a new Time or Date that is as far in the future # as this Duration represents. - def since(time = ::Time.now) + def since(time = ::Time.current) sum(1, time) end alias :from_now :since # Calculates a new Time or Date that is as far in the past # as this Duration represents. - def ago(time = ::Time.now) + def ago(time = ::Time.current) sum(-1, time) end alias :until :ago @@ -73,7 +73,7 @@ module ActiveSupport protected - def sum(sign, time = ::Time.now) #:nodoc: + def sum(sign, time = ::Time.current) #:nodoc: parts.inject(time) do |t,(type,number)| if t.acts_like?(:time) || t.acts_like?(:date) if type == :seconds diff --git a/activesupport/test/core_ext/duration_test.rb b/activesupport/test/core_ext/duration_test.rb index df878f917b..7b17fe71db 100644 --- a/activesupport/test/core_ext/duration_test.rb +++ b/activesupport/test/core_ext/duration_test.rb @@ -29,4 +29,46 @@ class DurationTest < Test::Unit::TestCase flunk("ArgumentError should be raised, but we got #{$!.class} instead") end end + + uses_mocha 'TestDurationSinceAndAgoWithCurrentTime' do + def test_since_and_ago_anchored_to_time_now_when_time_zone_default_not_set + Time.zone_default = nil + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + # since + assert_equal false, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.local(2000,1,1,0,0,5), 5.seconds.since + # ago + assert_equal false, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.local(1999,12,31,23,59,55), 5.seconds.ago + end + end + + def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set + silence_warnings do # silence warnings raised by tzinfo gem + Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + # since + assert_equal true, 5.seconds.since.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(2000,1,1,0,0,5), 5.seconds.since.time + assert_equal 'Eastern Time (US & Canada)', 5.seconds.since.time_zone.name + # ago + assert_equal true, 5.seconds.ago.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(1999,12,31,23,59,55), 5.seconds.ago.time + assert_equal 'Eastern Time (US & Canada)', 5.seconds.ago.time_zone.name + end + end + ensure + Time.zone_default = nil + end + end + + protected + def with_env_tz(new_tz = 'US/Eastern') + old_tz, ENV['TZ'] = ENV['TZ'], new_tz + yield + ensure + old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') + end end diff --git a/activesupport/test/core_ext/time_with_zone_test.rb b/activesupport/test/core_ext/time_with_zone_test.rb index efab8a24c8..df70e82c1d 100644 --- a/activesupport/test/core_ext/time_with_zone_test.rb +++ b/activesupport/test/core_ext/time_with_zone_test.rb @@ -627,6 +627,30 @@ class TimeWithZoneMethodsForTimeAndDateTimeTest < Test::Unit::TestCase assert_equal nil, Time.zone end + uses_mocha 'TestTimeCurrent' do + def test_current_returns_time_now_when_zone_default_not_set + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + assert_equal false, Time.current.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.local(2000), Time.current + end + end + + def test_current_returns_time_zone_now_when_zone_default_set + silence_warnings do # silence warnings raised by tzinfo gem + Time.zone_default = TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + assert_equal true, Time.current.is_a?(ActiveSupport::TimeWithZone) + assert_equal 'Eastern Time (US & Canada)', Time.current.time_zone.name + assert_equal Time.utc(2000), Time.current.time + end + end + ensure + Time.zone_default = nil + end + end + protected def with_env_tz(new_tz = 'US/Eastern') old_tz, ENV['TZ'] = ENV['TZ'], new_tz |