diff options
author | Xavier Noria <fxn@hashref.com> | 2014-02-18 09:35:11 +0100 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2014-02-18 09:42:06 +0100 |
commit | 65a7c571050da47ab997a5a07fb9f1e2302bf348 (patch) | |
tree | f56f672c3357bc5cf6f6ee41d2b78a1a5f298816 /activesupport/lib/active_support/testing | |
parent | f43083609a0a16f91e5f1cfc98a289a23865aea8 (diff) | |
download | rails-65a7c571050da47ab997a5a07fb9f1e2302bf348.tar.gz rails-65a7c571050da47ab997a5a07fb9f1e2302bf348.tar.bz2 rails-65a7c571050da47ab997a5a07fb9f1e2302bf348.zip |
time helpers honor the application time zone when passed a date
Rails applications are expected to be always aware of the application
time zone.
To be consistent with that contract, we have to assume that a bare
date passed to time helpers is a date in the application time zone,
not in the system time zone. The system time zone is irrelevant, we
should totally ignore it.
For example,
travel_to user.birth_date + 40.years
should make that user be 40th years old regardless of the system
time zone. Without this patch that may not be true.
Diffstat (limited to 'activesupport/lib/active_support/testing')
-rw-r--r-- | activesupport/lib/active_support/testing/time_helpers.rb | 23 |
1 files changed, 19 insertions, 4 deletions
diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb index 9e0a3d6345..b36f3cf94c 100644 --- a/activesupport/lib/active_support/testing/time_helpers.rb +++ b/activesupport/lib/active_support/testing/time_helpers.rb @@ -61,14 +61,23 @@ module ActiveSupport travel_to Time.now + duration, &block end - # Changes current time to the given time by stubbing +Time.now+ and +Date.today+ to return the - # time or date passed into this method. + # Changes current time to the given time by stubbing +Time.now+ and + # +Date.today+ to return the time or date passed into this method. # # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 # travel_to Time.new(2004, 11, 24, 01, 04, 44) # Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00 # Date.current # => Wed, 24 Nov 2004 # + # Dates are taken as their timestamp at the beginning of the day in the + # application time zone. <tt>Time.current</tt> returns said timestamp, + # and <tt>Time.now</tt> its equivalent in the system time zone. Similarly, + # <tt>Date.current</tt> returns a date equal to the argument, and + # <tt>Date.today</tt> the date according to <tt>Time.now</tt>, which may + # be different. (Note that you rarely want to deal with <tt>Time.now</tt>, + # or <tt>Date.today</tt>, in order to honor the application time zone + # please always use <tt>Time.current</tt> and <tt>Date.current</tt>.) + # # This method also accepts a block, which will return the current time back to its original # state at the end of the block: # @@ -78,8 +87,14 @@ module ActiveSupport # end # Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00 def travel_to(date_or_time, &block) - simple_stubs.stub_object(Time, :now, date_or_time.to_time) - simple_stubs.stub_object(Date, :today, date_or_time.to_date) + if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime) + now = date_or_time.midnight.to_time + else + now = date_or_time.to_time + end + + simple_stubs.stub_object(Time, :now, now) + simple_stubs.stub_object(Date, :today, now.to_date) if block_given? block.call |