aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/testing/time_helpers.rb
diff options
context:
space:
mode:
authorDavid Heinemeier Hansson <david@loudthinking.com>2014-08-17 12:52:54 -0700
committerDavid Heinemeier Hansson <david@loudthinking.com>2014-08-17 12:52:54 -0700
commit9f6e82ee4783e491c20f5244a613fdeb4024beb5 (patch)
treec6a933f829e2df666aefeb3150681790f3490565 /activesupport/lib/active_support/testing/time_helpers.rb
parent88d27ae9181151f448f39298adce9d1b7401a376 (diff)
downloadrails-9f6e82ee4783e491c20f5244a613fdeb4024beb5.tar.gz
rails-9f6e82ee4783e491c20f5244a613fdeb4024beb5.tar.bz2
rails-9f6e82ee4783e491c20f5244a613fdeb4024beb5.zip
Fix rounding errors with #travel_to by resetting the usec on any passed time to zero, so we only travel with per-second precision, not anything deeper than that.
Diffstat (limited to 'activesupport/lib/active_support/testing/time_helpers.rb')
-rw-r--r--activesupport/lib/active_support/testing/time_helpers.rb6
1 files changed, 5 insertions, 1 deletions
diff --git a/activesupport/lib/active_support/testing/time_helpers.rb b/activesupport/lib/active_support/testing/time_helpers.rb
index eefa84262e..1112c6e0b1 100644
--- a/activesupport/lib/active_support/testing/time_helpers.rb
+++ b/activesupport/lib/active_support/testing/time_helpers.rb
@@ -78,6 +78,10 @@ module ActiveSupport
# 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>.)
#
+ # Note that the usec for the time passed will be set to 0 to prevent rounding
+ # errors with external services, like MySQL (which will round instead of floor,
+ # leading to off-by-one-second errors).
+ #
# This method also accepts a block, which will return the current time back to its original
# state at the end of the block:
#
@@ -90,7 +94,7 @@ module ActiveSupport
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
+ now = date_or_time.to_time.change(usec: 0)
end
simple_stubs.stub_object(Time, :now, now)