Skip to content

Commit

Permalink
Duration #since and #ago with no argument (e.g., 5.days.ago) return T…
Browse files Browse the repository at this point in the history
…imeWithZone 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
  • Loading branch information
gbuesing committed Apr 21, 2008
1 parent ef8d266 commit 32b82e4
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 3 deletions.
2 changes: 2 additions & 0 deletions 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]
Expand Down
5 changes: 5 additions & 0 deletions activesupport/lib/active_support/core_ext/time/zones.rb
Expand Up @@ -35,6 +35,11 @@ def use_zone(time_zone)
::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)
Expand Down
6 changes: 3 additions & 3 deletions activesupport/lib/active_support/duration.rb
Expand Up @@ -51,14 +51,14 @@ def self.===(other) #:nodoc:

# 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
Expand All @@ -73,7 +73,7 @@ def inspect #:nodoc:

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
Expand Down
42 changes: 42 additions & 0 deletions activesupport/test/core_ext/duration_test.rb
Expand Up @@ -29,4 +29,46 @@ def test_argument_error
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
24 changes: 24 additions & 0 deletions activesupport/test/core_ext/time_with_zone_test.rb
Expand Up @@ -627,6 +627,30 @@ def test_time_zone_setter_with_non_identifying_argument_returns_nil
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
Expand Down

0 comments on commit 32b82e4

Please sign in to comment.