Skip to content

Commit

Permalink
Use application time zone when gets time as String.
Browse files Browse the repository at this point in the history
  • Loading branch information
alvir committed Nov 26, 2020
1 parent 173a112 commit b96f4ae
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions activesupport/lib/active_support/testing/time_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ def travel_to(date_or_time)

if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
now = date_or_time.midnight.to_time
elsif date_or_time.is_a?(String)
now = Time.zone.parse(date_or_time)
else
now = date_or_time.to_time.change(usec: 0)
end
Expand Down
15 changes: 15 additions & 0 deletions activesupport/test/time_travel_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "abstract_unit"
require "active_support/core_ext/date_time"
require "active_support/core_ext/numeric/time"
require "active_support/core_ext/string/conversions"
require_relative "time_zone_test_helpers"

class TimeTravelTest < ActiveSupport::TestCase
Expand Down Expand Up @@ -84,6 +85,20 @@ def test_time_helper_travel_to_with_time_zone
end
end

def test_time_helper_travel_to_with_string_for_time_zone
with_env_tz "US/Eastern" do
with_tz_default ActiveSupport::TimeZone["UTC"] do
Time.stub(:now, Time.now) do
expected_time = Time.new(2004, 11, 24, 1, 4, 44)

travel_to "2004-11-24 01:04:44" do
assert_equal expected_time.to_s(:db), Time.zone.now.to_s(:db)
end
end
end
end
end

def test_time_helper_travel_back
Time.stub(:now, Time.now) do
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
Expand Down

0 comments on commit b96f4ae

Please sign in to comment.