9#include <userver/storages/postgres/exceptions.hpp>
10#include <userver/storages/postgres/io/buffer_io.hpp>
11#include <userver/storages/postgres/io/buffer_io_base.hpp>
12#include <userver/storages/postgres/io/integral_types.hpp>
13#include <userver/storages/postgres/io/traits.hpp>
14#include <userver/storages/postgres/io/type_mapping.hpp>
16USERVER_NAMESPACE_BEGIN
24 using DurationType = std::chrono::microseconds;
28 Bigint microseconds = 0;
30 constexpr Interval() =
default;
31 constexpr Interval(Integer months, Integer days, Bigint microseconds)
34 microseconds{microseconds}
36 constexpr explicit Interval(DurationType ms)
37 : microseconds{ms.count()}
40 constexpr bool operator==(
const Interval& rhs)
const {
41 return months == rhs.months && days == rhs.days && microseconds == rhs.microseconds;
44 constexpr DurationType GetDuration()
const {
48 return std::chrono::duration_cast<
49 DurationType>(std::chrono::microseconds{microseconds} + std::chrono::hours{days * 24});
56struct BufferParser<detail::Interval> : detail::BufferParserBase<detail::Interval> {
57 using BaseType = detail::BufferParserBase<detail::Interval>;
58 using ValueType =
typename BaseType::ValueType;
60 using BaseType::BaseType;
64 std::size_t offset = 0;
66 offset +=
sizeof(Bigint);
68 offset +=
sizeof(Integer);
71 std::swap(
this->value, tmp);
76struct BufferFormatter<detail::Interval> : detail::BufferFormatterBase<detail::Interval> {
77 using BaseType = detail::BufferFormatterBase<detail::Interval>;
79 using BaseType::BaseType;
81 template <
typename Buffer>
82 void operator()(
const UserTypes& types, Buffer& buffer)
const {
83 io::WriteBuffer(types, buffer,
this->value.microseconds);
84 io::WriteBuffer(types, buffer,
this->value.days);
85 io::WriteBuffer(types, buffer,
this->value.months);
90struct CppToSystemPg<detail::Interval> : PredefinedOid<
PredefinedOids::kInterval> {};