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;
43 constexpr bool operator!=(
const Interval& rhs)
const {
return !(*
this == rhs); }
45 constexpr DurationType GetDuration()
const {
49 return std::chrono::duration_cast<
50 DurationType>(std::chrono::microseconds{microseconds} + std::chrono::hours{days * 24});
57struct BufferParser<detail::Interval> : detail::BufferParserBase<detail::Interval> {
58 using BaseType = detail::BufferParserBase<detail::Interval>;
59 using ValueType =
typename BaseType::ValueType;
61 using BaseType::BaseType;
65 std::size_t offset = 0;
67 offset +=
sizeof(Bigint);
69 offset +=
sizeof(Integer);
72 std::swap(
this->value, tmp);
77struct BufferFormatter<detail::Interval> : detail::BufferFormatterBase<detail::Interval> {
78 using BaseType = detail::BufferFormatterBase<detail::Interval>;
80 using BaseType::BaseType;
82 template <
typename Buffer>
83 void operator()(
const UserTypes& types, Buffer& buffer)
const {
84 io::WriteBuffer(types, buffer,
this->value.microseconds);
85 io::WriteBuffer(types, buffer,
this->value.days);
86 io::WriteBuffer(types, buffer,
this->value.months);
91struct CppToSystemPg<detail::Interval> : PredefinedOid<
PredefinedOids::kInterval> {};