From 65a7c571050da47ab997a5a07fb9f1e2302bf348 Mon Sep 17 00:00:00 2001 From: Xavier Noria Date: Tue, 18 Feb 2014 09:35:11 +0100 Subject: 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. --- activesupport/CHANGELOG.md | 4 ++++ .../lib/active_support/testing/time_helpers.rb | 23 ++++++++++++++++++---- activesupport/test/time_zone_test.rb | 14 +++++++++++++ 3 files changed, 37 insertions(+), 4 deletions(-) (limited to 'activesupport') diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index 43bfeff079..f25172c15c 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,7 @@ +* Time helpers honor the application time zone when passed a date. + + *Xavier Noria* + * Fix the implementation of Multibyte::Unicode.tidy_bytes for JRuby The existing implementation caused JRuby to raise the error: 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. Time.current returns said timestamp, + # and Time.now its equivalent in the system time zone. Similarly, + # Date.current returns a date equal to the argument, and + # Date.today the date according to Time.now, which may + # be different. (Note that you rarely want to deal with Time.now, + # or Date.today, in order to honor the application time zone + # please always use Time.current and Date.current.) + # # 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 diff --git a/activesupport/test/time_zone_test.rb b/activesupport/test/time_zone_test.rb index cd79efbe8c..48bcfa6929 100644 --- a/activesupport/test/time_zone_test.rb +++ b/activesupport/test/time_zone_test.rb @@ -124,6 +124,20 @@ class TimeZoneTest < ActiveSupport::TestCase travel_back end + def test_travel_to_a_date + with_env_tz do + Time.use_zone('Hawaii') do + date = Date.new(2014, 2, 18) + time = date.midnight + + travel_to date + + assert_equal date, Date.current + assert_equal time, Time.current + end + end + end + def test_local time = ActiveSupport::TimeZone["Hawaii"].local(2007, 2, 5, 15, 30, 45) assert_equal Time.utc(2007, 2, 5, 15, 30, 45), time.time -- cgit v1.2.3