Skip to content

Commit

Permalink
Merge branch 'tb/freeze_preserves_nanoseconds_for_datetime'
Browse files Browse the repository at this point in the history
  • Loading branch information
ballcheck committed Dec 29, 2016
2 parents a8dd3ac + e334e0c commit fba5744
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/timecop/time_stack_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def date(date_klass = Date)

def datetime(datetime_klass = DateTime)
if Float.method_defined?(:to_r)
fractions_of_a_second = time.to_f % 1
fractions_of_a_second = time.to_r - time.to_i
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))
Expand All @@ -98,7 +98,7 @@ def parse_time(*args)
if arg.is_a?(Time)
arg
elsif Object.const_defined?(:DateTime) && arg.is_a?(DateTime)
time_klass.at(arg.to_time.to_f).getlocal
time_klass.at(arg.to_time).getlocal
elsif Object.const_defined?(:Date) && arg.is_a?(Date)
time_klass.local(arg.year, arg.month, arg.day, 0, 0, 0)
elsif args.empty? && (arg.kind_of?(Integer) || arg.kind_of?(Float))
Expand Down
6 changes: 6 additions & 0 deletions test/timecop_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ def test_travel_does_not_reduce_precision_of_datetime
end
end

def test_freeze_preserves_nanoseconds_for_datetime
dt = DateTime.new(2008, 10, 10, 10, 10, 10.123456789)
Timecop.freeze(dt)
assert_equal dt.sec_fraction, DateTime.now.sec_fraction
end

def test_freeze_in_time_subclass_returns_mocked_subclass
t = Time.local(2008, 10, 10, 10, 10, 10)
custom_timeklass = Class.new(Time) do
Expand Down

0 comments on commit fba5744

Please sign in to comment.