diff options
author | Prem Sichanugrist <s@sikachu.com> | 2010-09-10 22:43:02 +0700 |
---|---|---|
committer | Santiago Pastorino <santiago@wyeworks.com> | 2010-09-19 11:26:20 -0700 |
commit | af6757a1ca7027b526b35d74c94a520d6bb24d7a (patch) | |
tree | 162d9567c963f26800416b2396c8bdc9842132b8 /activesupport | |
parent | e3d38e7da24197057f19529580f9f94424cd4848 (diff) | |
download | rails-af6757a1ca7027b526b35d74c94a520d6bb24d7a.tar.gz rails-af6757a1ca7027b526b35d74c94a520d6bb24d7a.tar.bz2 rails-af6757a1ca7027b526b35d74c94a520d6bb24d7a.zip |
Use `Time.current` to maintain consistency with AS::Duration
In [32b82e4c6f5523cdf5ee78c3022c50b46e018351], the committer has switch
methods in AS::Duration to use `Time.current` to return the correct
duration based on the `Time.default_zone` instead of using `Time.now`.
[#5607 state:committed]
Signed-off-by: Santiago Pastorino <santiago@wyeworks.com>
Diffstat (limited to 'activesupport')
-rw-r--r-- | activesupport/lib/active_support/core_ext/numeric/time.rb | 4 | ||||
-rw-r--r-- | activesupport/test/core_ext/numeric_ext_test.rb | 38 |
2 files changed, 40 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/core_ext/numeric/time.rb b/activesupport/lib/active_support/core_ext/numeric/time.rb index d32a9e0531..e73915ffcf 100644 --- a/activesupport/lib/active_support/core_ext/numeric/time.rb +++ b/activesupport/lib/active_support/core_ext/numeric/time.rb @@ -60,7 +60,7 @@ class Numeric alias :fortnight :fortnights # Reads best without arguments: 10.minutes.ago - def ago(time = ::Time.now) + def ago(time = ::Time.current) time - self end @@ -68,7 +68,7 @@ class Numeric alias :until :ago # Reads best with argument: 10.minutes.since(time) - def since(time = ::Time.now) + def since(time = ::Time.current) time + self end diff --git a/activesupport/test/core_ext/numeric_ext_test.rb b/activesupport/test/core_ext/numeric_ext_test.rb index e40b487753..6ef4e37b26 100644 --- a/activesupport/test/core_ext/numeric_ext_test.rb +++ b/activesupport/test/core_ext/numeric_ext_test.rb @@ -88,6 +88,44 @@ class NumericExtTimeAndDateTimeTest < Test::Unit::TestCase assert_equal Time.utc(2005,2,28,15,15,10), Time.utc(2004,2,29,15,15,10) + 1.year assert_equal DateTime.civil(2005,2,28,15,15,10), DateTime.civil(2004,2,29,15,15,10) + 1.year end + + 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.since.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.local(2000,1,1,0,0,5), 5.since + # ago + assert_equal false, 5.ago.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.local(1999,12,31,23,59,55), 5.ago + end + end + + def test_since_and_ago_anchored_to_time_zone_now_when_time_zone_default_set + Time.zone_default = ActiveSupport::TimeZone['Eastern Time (US & Canada)'] + with_env_tz 'US/Eastern' do + Time.stubs(:now).returns Time.local(2000) + # since + assert_equal true, 5.since.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(2000,1,1,0,0,5), 5.since.time + assert_equal 'Eastern Time (US & Canada)', 5.since.time_zone.name + # ago + assert_equal true, 5.ago.is_a?(ActiveSupport::TimeWithZone) + assert_equal Time.utc(1999,12,31,23,59,55), 5.ago.time + assert_equal 'Eastern Time (US & Canada)', 5.ago.time_zone.name + end + ensure + Time.zone_default = nil + 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 class NumericExtDateTest < Test::Unit::TestCase |