9#include <userver/storages/postgres/io/buffer_io.hpp>
10#include <userver/storages/postgres/io/buffer_io_base.hpp>
11#include <userver/storages/postgres/io/integral_types.hpp>
12#include <userver/storages/postgres/io/type_mapping.hpp>
13#include <userver/utils/datetime/date.hpp>
15USERVER_NAMESPACE_BEGIN
39struct BufferFormatter<Date> {
42 explicit BufferFormatter(Date value) : value{value} {}
44 template <
typename Buffer>
45 void operator()(
const UserTypes& types, Buffer& buffer) {
46 static const auto kPgEpoch = PostgresEpochDate();
47 if (value == kDatePositiveInfinity) {
48 WriteBuffer(types, buffer, std::numeric_limits<Integer>::max());
49 }
else if (value == kDateNegativeInfinity) {
50 WriteBuffer(types, buffer, std::numeric_limits<Integer>::min());
52 auto pg_days =
static_cast<Integer>((value.GetSysDays() - kPgEpoch.GetSysDays()).count());
53 WriteBuffer(types, buffer, pg_days);
60struct BufferParser<Date> : detail::BufferParserBase<Date> {
61 using BaseType = detail::BufferParserBase<Date>;
63 using BaseType::BaseType;
65 void operator()(
const FieldBuffer& buffer) {
66 static const auto kPgEpoch = PostgresEpochDate();
68 ReadBuffer(buffer, pg_days);
69 if (pg_days == std::numeric_limits<Integer>::max()) {
70 this->value = kDatePositiveInfinity;
71 }
else if (pg_days == std::numeric_limits<Integer>::min()) {
72 this->value = kDateNegativeInfinity;
74 this->value = kPgEpoch.GetSysDays() + Date::Days{pg_days};
80struct CppToSystemPg<Date> : PredefinedOid<PredefinedOids::kDate> {};