15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TIMESTAMP_H
16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TIMESTAMP_H
18 #include "google/cloud/spanner/version.h"
19 #include "google/cloud/status_or.h"
20 #include "absl/time/time.h"
21 #include <google/protobuf/timestamp.pb.h>
36 template <
typename Duration>
37 using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
90 friend std::ostream& operator<<(std::ostream& os,
Timestamp ts);
113 template <
typename T>
118 return ConvertTo(T{});
124 StatusOr<std::int64_t> ToRatio(std::int64_t min, std::int64_t max,
125 std::int64_t num, std::int64_t den)
const;
130 template <
typename Duration>
131 StatusOr<sys_time<Duration>> ConvertTo(sys_time<Duration>
const&)
const {
132 using Rep =
typename Duration::rep;
133 using Period =
typename Duration::period;
134 static_assert(std::ratio_greater_equal<Period, std::nano>::value,
135 "Duration must be no more precise than std::nano");
137 ToRatio(std::numeric_limits<Rep>::min(),
138 std::numeric_limits<Rep>::max(), Period::num, Period::den);
139 if (!count)
return std::move(count).status();
140 auto const unix_epoch = std::chrono::time_point_cast<Duration>(
141 sys_time<Duration>::clock::from_time_t(0));
142 return unix_epoch + Duration(
static_cast<Rep>(*count));
146 StatusOr<absl::Time> ConvertTo(absl::Time)
const {
return t_; }
150 StatusOr<protobuf::Timestamp> ConvertTo(protobuf::Timestamp
const&)
const;
152 explicit Timestamp(absl::Time t) : t_(t) {}
178 template <
typename Duration>
180 using Period =
typename Duration::period;
181 static_assert(std::ratio_greater_equal<Period, std::nano>::value,
182 "Duration must be no more precise than std::nano");
183 auto const unix_epoch = std::chrono::time_point_cast<Duration>(
184 sys_time<Duration>::clock::from_time_t(0));
185 auto const period = absl::Seconds(Period::num) / Period::den;
186 auto const count = (tp - unix_epoch).count();
187 return MakeTimestamp(absl::UnixEpoch() + count * period);
203 namespace spanner_internal {