15 #ifndef GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TIMESTAMP_H 16 #define GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TIMESTAMP_H 20 #include <google/protobuf/timestamp.pb.h> 38 template <
typename Duration>
39 using sys_time = std::chrono::time_point<std::chrono::system_clock, Duration>;
47 template <
typename Duration>
53 StatusOr<Timestamp> TimestampFromRFC3339(std::string
const&);
54 std::string TimestampToRFC3339(
Timestamp);
55 Timestamp TimestampFromProto(protobuf::Timestamp
const&);
56 protobuf::Timestamp TimestampToProto(
Timestamp);
89 return std::make_tuple(a.sec_, a.nsec_) == std::make_tuple(b.sec_, b.nsec_);
95 return std::make_tuple(a.sec_, a.nsec_) < std::make_tuple(b.sec_, b.nsec_);
110 return os << ts.ToRFC3339();
129 template <
typename T>
131 return ConvertTo(T{});
135 template <
typename Duration>
138 friend StatusOr<Timestamp> internal::TimestampFromRFC3339(std::string
const&);
139 friend std::string internal::TimestampToRFC3339(
Timestamp);
140 friend Timestamp internal::TimestampFromProto(protobuf::Timestamp
const&);
141 friend protobuf::Timestamp internal::TimestampToProto(
Timestamp);
144 Timestamp(std::int64_t sec, std::int32_t nsec) : sec_(sec), nsec_(nsec) {}
149 static StatusOr<Timestamp> FromCounts(std::intmax_t sec, std::intmax_t nsec);
152 static StatusOr<Timestamp> FromRFC3339(std::string
const&);
153 std::string ToRFC3339()
const;
157 static Timestamp FromProto(protobuf::Timestamp
const&);
158 protobuf::Timestamp ToProto()
const;
161 template <
typename Duration>
162 static sys_time<Duration> UnixEpoch() {
163 return std::chrono::time_point_cast<Duration>(
164 sys_time<Duration>::clock::from_time_t(0));
166 static StatusOr<Timestamp> FromRatio(std::intmax_t count,
167 std::intmax_t numerator,
168 std::intmax_t denominator);
169 StatusOr<std::intmax_t> ToRatio(std::intmax_t min, std::intmax_t max,
170 std::intmax_t numerator,
171 std::intmax_t denominator)
const;
176 template <
typename Duration>
177 StatusOr<sys_time<Duration>> ConvertTo(sys_time<Duration>
const&)
const {
179 ToRatio(std::numeric_limits<typename Duration::rep>::min(),
180 std::numeric_limits<typename Duration::rep>::max(),
181 Duration::period::num, Duration::period::den);
182 if (!count)
return count.status();
183 return Timestamp::UnixEpoch<Duration>() +
184 Duration(static_cast<typename Duration::rep>(*count));
193 template <
typename Duration>
195 return Timestamp::FromRatio((tp - Timestamp::UnixEpoch<Duration>()).count(),
196 Duration::period::num, Duration::period::den);
214 #endif // GOOGLE_CLOUD_CPP_GOOGLE_CLOUD_SPANNER_TIMESTAMP_H A sentinel type used to update a commit timestamp column.
Timestamp()
Default construction yields 1970-01-01T00:00:00Z.
friend bool operator!=(CommitTimestamp, CommitTimestamp)
friend bool operator<(Timestamp const &a, Timestamp const &b)
StatusOr< T > get() const
Convert the Timestamp to the user-specified template type.
friend bool operator==(CommitTimestamp, CommitTimestamp)
friend std::ostream & operator<<(std::ostream &os, Timestamp ts)
Contains all the Cloud Spanner C++ client types and functions.
friend bool operator==(Timestamp const &a, Timestamp const &b)
StatusOr< Timestamp > MakeTimestamp(sys_time< Duration > const &)
Construct a Timestamp from a std::chrono::time_point on the system clock.
friend bool operator>(Timestamp const &a, Timestamp const &b)
#define SPANNER_CLIENT_NS
friend bool operator>=(Timestamp const &a, Timestamp const &b)
std::chrono::time_point< std::chrono::system_clock, Duration > sys_time
Convenience alias.
friend bool operator!=(Timestamp const &a, Timestamp const &b)
A representation of the Spanner TIMESTAMP type: An instant in time.
friend bool operator<=(Timestamp const &a, Timestamp const &b)