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] Add Google::Protobuf::Timestamp.from_time #6685

Closed
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
4 changes: 4 additions & 0 deletions ruby/lib/google/protobuf/well_known_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ def to_time
end
end

def self.from_time(time)
new(seconds: time.to_i, nanos: time.nsec)
end

def from_time(time)
self.seconds = time.to_i
self.nanos = time.nsec
Expand Down
5 changes: 5 additions & 0 deletions ruby/tests/well_known_types_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ def test_timestamp
ts.from_time(time)
assert_equal 654321321, ts.nanos
assert_equal time, ts.to_time

timestamp = Google::Protobuf::Timestamp.from_time(Time.at(123456, Rational(654321321, 1000)))
ts.from_time(time)
assert_equal 654321321, timestamp.nanos
assert_equal ts, timestamp
end

def test_duration
Expand Down