Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Ruby] Fix for truncating behavior when converting Float to Duration. #8320

Merged
merged 1 commit into from Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion ruby/ext/google/protobuf_c/message.c
Expand Up @@ -1287,7 +1287,7 @@ const upb_msg* Message_GetUpbMessage(VALUE value, const upb_msgdef* m,
if (!rb_obj_is_kind_of(value, rb_cNumeric)) goto badtype;

sec.int64_val = NUM2LL(value);
nsec.int32_val = (NUM2DBL(value) - NUM2LL(value)) * 1000000000;
nsec.int32_val = round((NUM2DBL(value) - NUM2LL(value)) * 1000000000);
upb_msg_set(msg, sec_f, sec, arena);
upb_msg_set(msg, nsec_f, nsec, arena);
return msg;
Expand Down
6 changes: 6 additions & 0 deletions ruby/tests/common_tests.rb
Expand Up @@ -1701,6 +1701,12 @@ def test_converts_duration
m = proto_module::TimeMessage.new(duration: 1.1)
assert_equal Google::Protobuf::Duration.new(seconds: 1, nanos: 100_000_000), m.duration

m = proto_module::TimeMessage.new(duration: 123.321)
assert_equal Google::Protobuf::Duration.new(seconds: 123, nanos: 321_000_000), m.duration

m = proto_module::TimeMessage.new(duration: -123.321)
assert_equal Google::Protobuf::Duration.new(seconds: -123, nanos: -321_000_000), m.duration

assert_raise(Google::Protobuf::TypeError) { m.duration = '2' }
assert_raise(Google::Protobuf::TypeError) { m.duration = proto_module::TimeMessage.new }
end
Expand Down