Merge pull request #8562 from mcclymont/timestamp-from-time-class-method
Add class method Timestamp.from_time to ruby well known types
diff --git a/ruby/lib/google/protobuf/well_known_types.rb b/ruby/lib/google/protobuf/well_known_types.rb
index 37f8d5b..c9a9ab1 100755
--- a/ruby/lib/google/protobuf/well_known_types.rb
+++ b/ruby/lib/google/protobuf/well_known_types.rb
@@ -85,6 +85,11 @@
def from_time(time)
self.seconds = time.to_i
self.nanos = time.nsec
+ self
+ end
+
+ def self.from_time(time)
+ new.from_time(time)
end
def to_i
diff --git a/ruby/tests/well_known_types_test.rb b/ruby/tests/well_known_types_test.rb
index ea042eb..c069764 100755
--- a/ruby/tests/well_known_types_test.rb
+++ b/ruby/tests/well_known_types_test.rb
@@ -15,16 +15,20 @@
# millisecond accuracy
time = Time.at(123456, 654321)
- ts.from_time(time)
+ ts = Google::Protobuf::Timestamp.from_time(time)
assert_equal 123456, ts.seconds
assert_equal 654321000, ts.nanos
assert_equal time, ts.to_time
# nanosecond accuracy
time = Time.at(123456, Rational(654321321, 1000))
- ts.from_time(time)
+ ts = Google::Protobuf::Timestamp.from_time(time)
assert_equal 654321321, ts.nanos
assert_equal time, ts.to_time
+
+ # Instance method returns the same value as class method
+ assert_equal Google::Protobuf::Timestamp.new.from_time(time),
+ Google::Protobuf::Timestamp.from_time(time)
end
def test_duration