Skip to content

Commit b96f4ae

Browse files
author
alvir
committed
Use application time zone when gets time as String.
1 parent 173a112 commit b96f4ae

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

activesupport/lib/active_support/testing/time_helpers.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ def travel_to(date_or_time)
156156

157157
if date_or_time.is_a?(Date) && !date_or_time.is_a?(DateTime)
158158
now = date_or_time.midnight.to_time
159+
elsif date_or_time.is_a?(String)
160+
now = Time.zone.parse(date_or_time)
159161
else
160162
now = date_or_time.to_time.change(usec: 0)
161163
end

activesupport/test/time_travel_test.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_relative "abstract_unit"
44
require "active_support/core_ext/date_time"
55
require "active_support/core_ext/numeric/time"
6+
require "active_support/core_ext/string/conversions"
67
require_relative "time_zone_test_helpers"
78

89
class TimeTravelTest < ActiveSupport::TestCase
@@ -84,6 +85,20 @@ def test_time_helper_travel_to_with_time_zone
8485
end
8586
end
8687

88+
def test_time_helper_travel_to_with_string_for_time_zone
89+
with_env_tz "US/Eastern" do
90+
with_tz_default ActiveSupport::TimeZone["UTC"] do
91+
Time.stub(:now, Time.now) do
92+
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
93+
94+
travel_to "2004-11-24 01:04:44" do
95+
assert_equal expected_time.to_s(:db), Time.zone.now.to_s(:db)
96+
end
97+
end
98+
end
99+
end
100+
end
101+
87102
def test_time_helper_travel_back
88103
Time.stub(:now, Time.now) do
89104
expected_time = Time.new(2004, 11, 24, 1, 4, 44)

0 commit comments

Comments
 (0)