Created
May 20, 2016 07:41
-
-
Save mchung/7348897fe4b95068af3a6f46e3c1739e to your computer and use it in GitHub Desktop.
Protobuf: Compute Timestamp from current time in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example lifted from https://github.com/google/protobuf/blob/master/src/google/protobuf/timestamp.proto | |
// | |
// Example 6: Compute Timestamp from current time in Ruby | |
// | |
// [1] pry(main)> now = Time.now | |
// => 2016-05-20 00:33:04 -0700 | |
// [2] pry(main)> seconds = now.to_i | |
// => 1463729584 | |
// [3] pry(main)> nanos = now.nsec | |
// => 204053000 | |
// [4] pry(main)> timestamp = Timestamp.new(seconds: seconds, nanos: nanos) | |
// => #<Timestamp seconds=1463729584 nanos=204053000> | |
// [5] pry(main)> Time.at(timestamp.seconds, timestamp.nanos / 1000) | |
// => 2016-05-20 00:33:04 -0700 | |
// [6] pry(main)> now == _ | |
// => true | |
message Timestamp { | |
// Represents seconds of UTC time since Unix epoch | |
// 1970-01-01T00:00:00Z. Must be from from 0001-01-01T00:00:00Z to | |
// 9999-12-31T23:59:59Z inclusive. | |
optional int64 seconds = 1; | |
// Non-negative fractions of a second at nanosecond resolution. Negative | |
// second values with fractions must still have non-negative nanos values | |
// that count forward in time. Must be from 0 to 999,999,999 | |
// inclusive. | |
optional int32 nanos = 2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment