Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nanosecond fix #70

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions lib/timecop/time_stack_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,10 @@ def scaling_factor
end

def time(time_klass = Time) #:nodoc:
begin
actual_time = time_klass.at(@time)
calculated_time = time_klass.at(@time.to_f)
time = times_are_equal_within_epsilon(actual_time, calculated_time, 1) ? actual_time : calculated_time
rescue
time = time_klass.at(@time.to_f)
if Timecop.active_support != false && @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?
Expand Down Expand Up @@ -106,11 +104,7 @@ def parse_time(*args)
time_klass = Time.respond_to?(:zone) && Time.zone ? Time.zone : Time
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)
expected_time = time_klass.local(arg.year, arg.month, arg.day, arg.hour, arg.min, arg.sec)
expected_time += expected_time.utc_offset - rational_to_utc_offset(arg.offset)
Expand Down
70 changes: 47 additions & 23 deletions test/time_stack_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@
require File.join(File.dirname(__FILE__), "test_helper")
require File.join(File.dirname(__FILE__), '..', 'lib', 'timecop')

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
Expand Down Expand Up @@ -184,7 +191,8 @@ def test_set_travel_offset_for_freeze
end

def test_timezones
require 'active_support/all'
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 @@ -202,12 +210,6 @@ 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
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)
Expand All @@ -220,18 +222,6 @@ def test_parse_with_active_support_off
Timecop.freeze(date)
end

def test_uses_active_supports_in_time_zone
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 +235,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
require 'active_support/all'

def test_time_with_different_timezone_keeps_nsec
Timecop.active_support = true
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
Timecop.active_support = true
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
Timecop.active_support = true
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
Timecop.active_support = true
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