Skip to content
Browse files

Stop DateTime from losing precision after travel (fixes #22)

…and fix an apostrophe
  • Loading branch information...
1 parent 7c55bf0 commit 7c41d86343225ccb9ccf58701424674d9f0f851b Adam Milligan & Andrew Bruce committed with Feb 1, 2012
Showing with 9 additions and 2 deletions.
  1. +3 −1 lib/timecop/time_stack_item.rb
  2. +1 −1 test/time_stack_item_test.rb
  3. +5 −0 test/timecop_test.rb
View
4 lib/timecop/time_stack_item.rb
@@ -60,7 +60,9 @@ def date
def datetime
# DateTime doesn't know about DST, so let's remove its presence
our_offset = utc_offset + dst_adjustment
- DateTime.new(year, month, day, hour, min, sec, utc_offset_to_rational(our_offset))
+ fractions_of_a_second = time.to_f % 1
+ DateTime.new(year, month, day, hour, min, sec + fractions_of_a_second,
+ utc_offset_to_rational(our_offset))
@ngan
ngan added a note Jul 27, 2012

I'm getting an error from this change (on REE 1.8.7):

undefined method `to_r' for 32907.0:Float
gems/timecop-0.4.1/lib/timecop/time_stack_item.rb:64:in `datetime'
gems/timecop-0.4.1/lib/timecop/time_extensions.rb:53:in `mock_time'
gems/timecop-0.4.1/lib/timecop/time_extensions.rb:66:in `now'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
end
def dst_adjustment
View
2 test/time_stack_item_test.rb
@@ -129,7 +129,7 @@ def test_compute_dst_adjustment_for_non_dst_to_dst
assert_equal -1 * 60 * 60, tsi.send(:dst_adjustment)
end
- # Ensure DateTime's handle changing DST properly
+ # Ensure DateTimes handle changing DST properly
def test_datetime_for_dst_to_non_dst
Timecop.freeze(DateTime.parse("2009-12-1 00:38:00 -0500"))
t = DateTime.parse("2009-10-11 00:00:00 -0400")
View
5 test/timecop_test.rb
@@ -48,6 +48,11 @@ def test_travel_with_block_unsets_mock_time
Timecop.travel(1) do; end
assert_nil Time.send(:mock_time)
end
+
+ def test_travel_does_not_reduce_precision_of_datetime
+ Timecop.travel(1)
+ assert_not_equal DateTime.now, DateTime.now
+ end
def test_recursive_freeze
t = Time.local(2008, 10, 10, 10, 10, 10)

0 comments on commit 7c41d86

Please sign in to comment.
Something went wrong with that request. Please try again.