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>
31GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
34
35
36template <
typename Duration>
37using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
90 friend std::ostream& operator<<(std::ostream& os,
Timestamp ts);
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113 template <
typename T>
114 StatusOr<T>
get()
const {
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) {}
158
159
160
161
165
166
167
168
172
173
174
175
176
177
178template <
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);
191
192
193
194
200GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
203namespace spanner_internal {
204GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN
207GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
A representation of the Spanner TIMESTAMP type: An instant in time.
Definition: timestamp.h:54
friend bool operator<=(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:78
friend bool operator!=(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:72
Timestamp()
Default construction yields 1970-01-01T00:00:00Z.
Definition: timestamp.h:57
friend bool operator>(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:84
Timestamp(Timestamp const &)=default
friend bool operator==(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:69
Timestamp & operator=(Timestamp &&)=default
StatusOr< T > get() const
Convert the Timestamp to the user-specified template type.
Definition: timestamp.h:114
Timestamp(Timestamp &&)=default
Timestamp & operator=(Timestamp const &)=default
friend StatusOr< Timestamp > MakeTimestamp(absl::Time)
Construct a Timestamp from an absl::Time.
friend bool operator>=(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:81
friend bool operator<(Timestamp const &a, Timestamp const &b)
Definition: timestamp.h:75
Contains all the Cloud Spanner C++ client types and functions.
Definition: backoff_policy.h:23
StatusOr< Timestamp > MakeTimestamp(protobuf::Timestamp const &)
Construct a Timestamp from a google::protobuf::Timestamp.
StatusOr< Timestamp > MakeTimestamp(sys_time< Duration > const &tp)
Construct a Timestamp from a std::chrono::time_point on the system clock.
Definition: timestamp.h:179
A sentinel type used to update a commit timestamp column.
Definition: timestamp.h:195
friend bool operator!=(CommitTimestamp, CommitTimestamp)
Definition: timestamp.h:197
friend bool operator==(CommitTimestamp, CommitTimestamp)
Definition: timestamp.h:196