| #include "tlbmc/time/time.h" |
| |
| #include <sys/types.h> |
| |
| #include <cstdint> |
| |
| #include "google/protobuf/duration.pb.h" |
| #include "google/protobuf/timestamp.pb.h" |
| #include "absl/time/time.h" |
| |
| namespace milotic_tlbmc { |
| |
| google::protobuf::Timestamp Now(absl::Time time) { |
| google::protobuf::Timestamp timestamp; |
| const int64_t seconds = absl::ToUnixSeconds(time); // NOLINT |
| timestamp.set_seconds(seconds); |
| timestamp.set_nanos(static_cast<int32_t>( |
| (time - absl::FromUnixSeconds(seconds)) / absl::Nanoseconds(1))); |
| return timestamp; |
| } |
| |
| google::protobuf::Timestamp MilliSecondsToTimestamp(uint64_t milliseconds) { |
| google::protobuf::Timestamp timestamp; |
| // int64_t has max value of 9223372036854775807, it should be enough for |
| // milliseconds till April 2262. |
| timestamp.set_seconds(static_cast<int64_t>(milliseconds) / 1000); |
| timestamp.set_nanos(static_cast<int32_t>((milliseconds % 1000) * 1000000)); |
| return timestamp; |
| } |
| |
| google::protobuf::Duration EncodeGoogleApiProto(absl::Duration d) { |
| google::protobuf::Duration proto; |
| const int64_t s = absl::IDivDuration(d, absl::Seconds(1), &d); |
| const int64_t n = absl::IDivDuration(d, absl::Nanoseconds(1), &d); |
| proto.set_seconds(s); |
| proto.set_nanos(static_cast<int32_t>(n)); |
| return proto; |
| } |
| |
| absl::Time DecodeGoogleApiProto(const google::protobuf::Timestamp& timestamp) { |
| return absl::FromUnixSeconds(timestamp.seconds()) + |
| absl::Nanoseconds(timestamp.nanos()); |
| } |
| |
| absl::Duration DecodeGoogleApiProto( |
| const google::protobuf::Duration& duration) { |
| return absl::Seconds(duration.seconds()) + |
| absl::Nanoseconds(duration.nanos()); |
| } |
| |
| } // namespace milotic_tlbmc |