Skip to content

Commit

Permalink
Merge pull request #127 from mschulkind/master
Browse files Browse the repository at this point in the history
Fix a bug wrt to datetime precision after travel
  • Loading branch information
Travis Jeffery committed Feb 19, 2015
2 parents 7af9058 + 6ffcc10 commit f8ae044
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 8 deletions.
8 changes: 2 additions & 6 deletions lib/timecop/time_stack_item.rb
Expand Up @@ -76,12 +76,8 @@ def date(date_klass = Date)

def datetime(datetime_klass = DateTime)
if Float.method_defined?(:to_r)
if !sec.zero?
fractions_of_a_second = time.to_f % 1
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
else
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
end
fractions_of_a_second = time.to_f % 1
datetime_klass.new(year, month, day, hour, min, (fractions_of_a_second + sec), utc_offset_to_rational(utc_offset))
else
datetime_klass.new(year, month, day, hour, min, sec, utc_offset_to_rational(utc_offset))
end
Expand Down
2 changes: 1 addition & 1 deletion test/time_stack_item_test.rb
Expand Up @@ -269,7 +269,7 @@ def test_freezing_a_time_with_zone_returns_proper_zones

def test_datetime_timezones
dt = DateTime.new(2011,1,3,15,25,0,"-6")
Timecop.travel(dt) do
Timecop.freeze(dt) do
now = DateTime.now
assert_equal dt, now, "#{dt.to_f}, #{now.to_f}"
end
Expand Down
5 changes: 4 additions & 1 deletion test/timecop_test.rb
Expand Up @@ -58,7 +58,10 @@ def test_travel_with_block_unsets_mock_time
def test_travel_does_not_reduce_precision_of_datetime
# requires to_r on Float (>= 1.9)
if Float.method_defined?(:to_r)
Timecop.travel(1)
Timecop.travel(Time.new(2014, 1, 1, 0, 0, 0))
assert DateTime.now != DateTime.now

Timecop.travel(Time.new(2014, 1, 1, 0, 0, 59))
assert DateTime.now != DateTime.now
end
end
Expand Down

0 comments on commit f8ae044

Please sign in to comment.