Skip to content

Commit 446daa0

Browse files
authored
Merge pull request rails#52880 from mopp/fix_travel_to_with_ms
Set usec 0 unless with_usec
2 parents 6fbf8d0 + 839ad34 commit 446daa0

File tree

3 files changed

+107
-1
lines changed

3 files changed

+107
-1
lines changed

activesupport/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@
6565
6666
*Frederik Erbs Spang Thomsen*
6767
68+
* Fix `travel_to` to set usec 0 when `with_usec` is `false` and the given argument String or DateTime.
69+
70+
*mopp*
71+
6872
Please check [7-2-stable](https://github.com/rails/rails/blob/7-2-stable/activesupport/CHANGELOG.md) for previous changes.

activesupport/lib/active_support/testing/time_helpers.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,10 @@ def travel_to(date_or_time, with_usec: false)
166166
else
167167
now = date_or_time
168168
now = now.to_time unless now.is_a?(Time)
169-
now = now.change(usec: 0) unless with_usec
170169
end
171170

171+
now = now.change(usec: 0) unless with_usec
172+
172173
# +now+ must be in local system timezone, because +Time.at(now)+
173174
# and +now.to_date+ (see stubs below) will use +now+'s timezone too!
174175
now = now.getlocal

activesupport/test/time_travel_test.rb

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,20 @@ def test_time_helper_travel_to_with_string_for_time_zone
174174
end
175175
end
176176

177+
def test_time_helper_travel_to_with_string_and_milliseconds
178+
with_env_tz "US/Eastern" do
179+
with_tz_default ActiveSupport::TimeZone["UTC"] do
180+
Time.stub(:now, Time.now) do
181+
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
182+
183+
travel_to "2004-11-24T01:04:44.123-05:00" do
184+
assert_equal expected_time, Time.zone.now
185+
end
186+
end
187+
end
188+
end
189+
end
190+
177191
def test_time_helper_travel_to_with_separate_class
178192
travel_object = TravelClass.new
179193
date1 = Date.new(2004, 11, 24)
@@ -330,6 +344,93 @@ def test_time_helper_with_usec_true
330344
end
331345
end
332346

347+
def test_time_helper_travel_to_with_datetime_and_usec
348+
with_env_tz "US/Eastern" do
349+
with_tz_default ActiveSupport::TimeZone["UTC"] do
350+
Time.stub(:now, Time.now) do
351+
duration_usec = 0.1.seconds
352+
traveled_time = DateTime.iso8601("2004-11-24T01:04:44.000-05:00") + duration_usec
353+
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
354+
355+
assert_nothing_raised do
356+
travel_to traveled_time
357+
358+
assert_equal expected_time, Time.zone.now
359+
360+
travel_back
361+
end
362+
ensure
363+
travel_back
364+
end
365+
end
366+
end
367+
end
368+
369+
def test_time_helper_travel_to_with_datetime_and_usec_true
370+
with_env_tz "US/Eastern" do
371+
with_tz_default ActiveSupport::TimeZone["UTC"] do
372+
Time.stub(:now, Time.now) do
373+
duration_usec = 0.1.seconds
374+
traveled_time = DateTime.iso8601("2004-11-24T01:04:44.000-05:00") + duration_usec
375+
expected_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
376+
377+
assert_nothing_raised do
378+
travel_to traveled_time, with_usec: true
379+
380+
assert_equal expected_time, Time.now
381+
382+
travel_back
383+
end
384+
ensure
385+
travel_back
386+
end
387+
end
388+
end
389+
end
390+
391+
def test_time_helper_travel_to_with_string_and_usec
392+
with_tz_default ActiveSupport::TimeZone["UTC"] do
393+
Time.stub(:now, Time.now) do
394+
duration_usec = 0.1.seconds
395+
traveled_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
396+
expected_time = Time.new(2004, 11, 24, 1, 4, 44)
397+
398+
assert_nothing_raised do
399+
travel_to traveled_time.iso8601(3)
400+
401+
assert_equal expected_time, Time.now
402+
403+
travel_back
404+
end
405+
ensure
406+
travel_back
407+
end
408+
end
409+
end
410+
411+
def test_time_helper_travel_to_with_string_and_usec_true
412+
with_tz_default ActiveSupport::TimeZone["UTC"] do
413+
Time.stub(:now, Time.now) do
414+
duration_usec = 0.1.seconds
415+
expected_time = Time.new(2004, 11, 24, 1, 4, 44) + duration_usec
416+
417+
assert_nothing_raised do
418+
travel_to expected_time.iso8601(3), with_usec: true
419+
420+
assert_equal expected_time.to_f, Time.now.to_f
421+
422+
travel 0.5, with_usec: true
423+
424+
assert_equal((expected_time + 0.5).to_f, Time.now.to_f)
425+
426+
travel_back
427+
end
428+
ensure
429+
travel_back
430+
end
431+
end
432+
end
433+
333434
def test_time_helper_freeze_time_with_usec_true
334435
# repeatedly test in case Time.now happened to actually be 0 usec
335436
checks = 9.times.map do

0 commit comments

Comments
 (0)