11#include <userver/storages/postgres/io/buffer_io_base.hpp>
13#include <userver/storages/postgres/io/transform_io.hpp>
14#include <userver/storages/postgres/io/type_mapping.hpp>
17USERVER_NAMESPACE_BEGIN
21using ClockType = std::chrono::system_clock;
32using IntervalType = std::chrono::microseconds;
89template <
typename Duration>
91 using ValueType = std::chrono::time_point<ClockType, Duration>;
93 const ValueType value;
97 template <
typename Buffer>
98 void operator()(
const UserTypes& types, Buffer& buf)
const {
99 static const ValueType pg_epoch =
102 WriteBuffer(types, buf, std::numeric_limits<Bigint>::max());
104 WriteBuffer(types, buf, std::numeric_limits<Bigint>::min());
106 auto tmp = std::chrono::duration_cast<std::chrono::microseconds>(value -
109 WriteBuffer(types, buf, tmp);
115template <
typename Duration>
117 : detail::BufferParserBase<std::chrono::time_point<ClockType, Duration>> {
119 detail::BufferParserBase<std::chrono::time_point<ClockType, Duration>>;
120 using ValueType =
typename BaseType::ValueType;
121 using BaseType::BaseType;
124 static const ValueType pg_epoch =
128 if (usec == std::numeric_limits<Bigint>::max()) {
130 }
else if (usec == std::numeric_limits<Bigint>::min()) {
133 ValueType tmp = pg_epoch + std::chrono::microseconds{usec};
134 std::swap(tmp, this->value);
141template <
typename Rep,
typename Period>
142struct DurationIntervalCvt {
143 using UserType = std::chrono::duration<Rep, Period>;
144 UserType operator()(
const Interval& wire_val)
const {
145 return std::chrono::duration_cast<UserType>(wire_val.GetDuration());
147 Interval operator()(
const UserType& user_val)
const {
148 return Interval{std::chrono::duration_cast<IntervalType>(user_val)};
157template <
typename Rep,
typename Period>
160 io::detail::Interval,
161 io::detail::DurationIntervalCvt<Rep, Period>>;
165template <
typename Rep,
typename Period>
169 io::detail::DurationIntervalCvt<Rep, Period>>;
176 : PredefinedOid<PredefinedOids::kTimestamptz> {};
178template <
typename Duration>
180 : PredefinedOid<PredefinedOids::kTimestamp> {};
182template <
typename Rep,
typename Period>
184 : PredefinedOid<PredefinedOids::kInterval> {};