module ActiveSupport::Testing::TimeHelpers
def travel_to(date_or_time)
end
Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
state at the end of the block:
This method also accepts a block, which will return the current time back to its original
leading to off-by-one-second errors).
errors with external services, like MySQL (which will round instead of floor,
Note that the usec for the time passed will be set to 0 to prevent rounding
please always use Time.current and Date.current.)
or Date.today, in order to honor the application time zone
be different. (Note that you rarely want to deal with Time.now,
Date.today the date according to Time.now, which may
Date.current returns a date equal to the argument, and
and Time.now its equivalent in the system time zone. Similarly,
application time zone. Time.current returns said timestamp,
Dates are taken as their timestamp at the beginning of the day in the
Date.current # => Wed, 24 Nov 2004
Time.current # => Wed, 24 Nov 2004 01:04:44 EST -05:00
travel_to Time.new(2004, 11, 24, 01, 04, 44)
Time.current # => Sat, 09 Nov 2013 15:34:49 EST -05:00
+Date.today+ to return the time or date passed into this method.
Changes current time to the given time by stubbing +Time.now+ and
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 else now = date_or_time.to_time.change(usec: 0) end simple_stubs.stub_object(Time, :now, now) simple_stubs.stub_object(Date, :today, now.to_date) if block_given? begin yield ensure travel_back end end end