Merge pull request #2482 from andreaseger/fix_ruby_timestamp_accuracy

[Ruby] fix floating point accuracy problem in Timestamp#to_f
pull/3327/head
Adam Cozzette 8 years ago committed by GitHub
commit bd5ab154da
  1. 2
      ruby/lib/google/protobuf/well_known_types.rb
  2. 12
      ruby/tests/well_known_types_test.rb

@ -80,7 +80,7 @@ module Google
end end
def to_f def to_f
self.seconds + (self.nanos.to_f / 1_000_000_000) self.seconds + (self.nanos.quo(1_000_000_000))
end end
end end

@ -13,10 +13,18 @@ class TestWellKnownTypes < Test::Unit::TestCase
assert_equal Time.at(12345), ts.to_time assert_equal Time.at(12345), ts.to_time
assert_equal 12345, ts.to_i assert_equal 12345, ts.to_i
ts.from_time(Time.at(123456, 654321)) # millisecond accuracy
time = Time.at(123456, 654321)
ts.from_time(time)
assert_equal 123456, ts.seconds assert_equal 123456, ts.seconds
assert_equal 654321000, ts.nanos assert_equal 654321000, ts.nanos
assert_equal Time.at(123456.654321), ts.to_time assert_equal time, ts.to_time
# nanosecond accuracy
time = Time.at(123456, Rational(654321321, 1000))
ts.from_time(time)
assert_equal 654321321, ts.nanos
assert_equal time, ts.to_time
end end
def test_duration def test_duration

Loading…
Cancel
Save