Skip to content

Commit

Permalink
fix calculations with nanoseconds (close travisjeffery#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
Travis Jeffery committed Apr 28, 2013
2 parents 2636765 + da2943b commit 81f078b
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 58 deletions.
24 changes: 9 additions & 15 deletions lib/timecop/time_stack_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,21 +49,19 @@ def scaling_factor
@scaling_factor
end

def time(klass = time_klass) #:nodoc:
begin
actual_time = klass.at(@time)
calculated_time = klass.at(@time.to_f)
time = times_are_equal_within_epsilon(actual_time, calculated_time, 1) ? actual_time : calculated_time
rescue
time = klass.at(@time.to_f)
def time(time_klass = Time) #:nodoc:
if @time.respond_to?(:in_time_zone)
time = time_klass.at(@time.utc.to_r)
else
time = time_klass.at(@time)
end

if travel_offset.nil?
time
elsif scaling_factor.nil?
klass.at(Time.now_without_mock_time + travel_offset)
time_klass.at(Time.now_without_mock_time + travel_offset)
else
klass.at(scaled_time)
time_klass.at(scaled_time)
end
end

Expand Down Expand Up @@ -97,11 +95,7 @@ def utc_offset_to_rational(utc_offset)
def parse_time(*args)
arg = args.shift
if arg.is_a?(Time)
if Timecop.active_support != false && arg.respond_to?(:in_time_zone)
arg.in_time_zone
else
arg.getlocal
end
arg
elsif Object.const_defined?(:DateTime) && arg.is_a?(DateTime)
time_klass.new(arg.year, arg.month, arg.day, arg.hour, arg.min, arg.sec, arg.offset*24*60*60).getlocal
elsif Object.const_defined?(:Date) && arg.is_a?(Date)
Expand All @@ -111,7 +105,7 @@ def parse_time(*args)
elsif arg.nil?
Time.now
else
if arg.is_a?(String) && Timecop.active_support != false && Time.respond_to?(:parse)
if arg.is_a?(String) && Time.respond_to?(:parse)
Time.parse(arg)
else
# we'll just assume it's a list of y/m/d/h/m/s
Expand Down
2 changes: 0 additions & 2 deletions lib/timecop/timecop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ class Timecop
include Singleton

class << self
attr_accessor :active_support

# Allows you to run a block of code and "fake" a time throughout the execution of that block.
# This is particularly useful for writing test methods where the passage of time is critical to the business
# logic being tested. For example:
Expand Down
81 changes: 40 additions & 41 deletions test/time_stack_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@
require 'active_support/all'

class TestTimeStackItem < Test::Unit::TestCase
def setup
Timecop.active_support = false
Time.zone = nil
end

def teardown
Timecop.active_support = nil
Timecop.return
Time.zone = nil
end

def test_new_with_time
Expand Down Expand Up @@ -164,8 +159,6 @@ def test_set_travel_offset_for_freeze
end

def test_timezones
Timecop.active_support = true

Time.zone = "Europe/Zurich"
time = Time.zone.parse("2012-12-27T12:12:12+08:00")
Timecop.freeze(time) do |frozen_time|
Expand All @@ -174,12 +167,11 @@ def test_timezones
end

def test_timezones_apply_dates
require 'active_support/all'
Time.zone = "Marshall Is."
Time.zone = "Central Time (US & Canada)"
time = Time.zone.local(2013,1,3)

Timecop.freeze(time) do
assert_equal time.to_date, Date.today
assert_equal time.to_date, Time.now.to_date
end
end

Expand All @@ -193,38 +185,11 @@ def test_set_scaling_factor_for_scale
assert_equal tsi.send(:scaling_factor), 4, "Scaling factor not set"
end

def test_parse_string_date_with_active_support
Timecop.active_support = true
date = '2012-01-02'
Time.expects(:parse).with(date).returns(Time.local(2012, 01, 02))
Timecop.freeze(date)
end

def test_parse_only_string_with_active_support
Time.expects(:parse).never
Timecop.freeze(2011, 01, 02, hour=0, minute=0, second=0)
end

def test_parse_with_active_support_off
date = '2012-01-02'
Timecop.active_support = false
Time.expects(:parse).never
Timecop.freeze(date)
end

def test_uses_active_supports_in_time_zone
Timecop.active_support = true
time = Time.now
Time.any_instance.expects(:in_time_zone).returns(time)
Timecop::TimeStackItem.new(:freeze, time)
end

def test_configured_off_active_support_in_time_zone_xxx
Timecop.active_support = false
Time.any_instance.expects(:in_time_zone).never
Timecop::TimeStackItem.new(:freeze, Time.now)
end

def test_parse_date
assert_nothing_raised do
Timecop.freeze(Date.new(2012, 6, 9))
Expand All @@ -245,13 +210,47 @@ def test_nsecs_are_set
assert_equal time.nsec, Time.now.nsec if (Time.now.respond_to?(:nsec))
end

def test_time_with_different_timezone
Timecop.active_support = true
def test_time_with_different_timezone_keeps_nsec

Time.zone = "Tokyo"
t = Time.now
Timecop.freeze(t) do
assert_times_effectively_equal t, Time.now
assert_equal t, Time.now
assert_equal t.nsec, Time.now.nsec if (Time.now.respond_to?(:nsec))
end
end

def test_time_now_always_returns_local_time

Time.zone = "Tokyo"
t = Time.utc(2000, 1, 1)
Timecop.freeze(t) do
assert_equal t.getlocal.zone, Time.now.zone
end
end

def test_time_zone_now_returns_time_in_that_zone

Time.zone = "Hawaii"
t = Time.utc(2000, 1, 1)
Timecop.freeze(t) do
assert_equal t, Time.zone.now
assert_equal 'HST', Time.zone.now.zone
end
end

def test_freezing_a_time_with_zone_returns_proper_zones

Time.zone = "Hawaii"
t = ActiveSupport::TimeWithZone.new(Time.utc(2000, 1, 1), ActiveSupport::TimeZone['Tokyo'])
Timecop.freeze(t) do
local_now = Time.now
assert_equal t, local_now
assert_equal t.getlocal.zone, local_now.zone

zoned_now = Time.zone.now
assert_equal t, zoned_now
assert_equal 'HST', zoned_now.zone
end
end
end

0 comments on commit 81f078b

Please sign in to comment.