blob: 4e7ca54962b11fcb79989543c890ab016b4f61c6 [file] [log] [blame]
#include "tlbmc/time/time.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::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