aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby
diff options
context:
space:
mode:
authorGravatar Adam Cozzette <acozzette@gmail.com>2017-07-06 08:11:50 -0700
committerGravatar GitHub <noreply@github.com>2017-07-06 08:11:50 -0700
commitbd5ab154dad4b4e2bf673a44aa32f7264a208e18 (patch)
treea02e66899638b2c0b2c910d95ce500ac21106743 /ruby
parent6bd51a59df41b99058ec8c2b03a177a218267ce5 (diff)
parent78cb804063716767966cdcd86c66500df342ad33 (diff)
Merge pull request #2482 from andreaseger/fix_ruby_timestamp_accuracy
[Ruby] fix floating point accuracy problem in Timestamp#to_f
Diffstat (limited to 'ruby')
-rw-r--r--ruby/lib/google/protobuf/well_known_types.rb2
-rw-r--r--ruby/tests/well_known_types_test.rb12
2 files changed, 11 insertions, 3 deletions
diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb
index 547de874..921ddbc0 100644
--- a/ruby/lib/google/protobuf/well_known_types.rb
+++ b/ruby/lib/google/protobuf/well_known_types.rb
@@ -80,7 +80,7 @@ module Google
end
def to_f
- self.seconds + (self.nanos.to_f / 1_000_000_000)
+ self.seconds + (self.nanos.quo(1_000_000_000))
end
end
diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb
index 9b46632b..bd24c328 100644
--- a/ruby/tests/well_known_types_test.rb
+++ b/ruby/tests/well_known_types_test.rb
@@ -13,10 +13,18 @@ class TestWellKnownTypes < Test::Unit::TestCase
assert_equal Time.at(12345), ts.to_time
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 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
def test_duration