userver: userver/storages/postgres/io/date.hpp Source File
Loading...
Searching...
No Matches
date.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/date.hpp
4/// @brief utils::datetime::Date I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <limits>
8
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>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace storages::postgres {
18
19/// Corresponds to DATE
20using Date = USERVER_NAMESPACE::utils::datetime::Date;
21
22/// Postgres epoch date (2000-01-01)
24
25/// Constant equivalent to PostgreSQL 'infinity'::date, a date that is later
26/// than all other dates.
27/// https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-TABLE
28inline constexpr Date kDatePositiveInfinity = Date::SysDays::max();
29
30/// Constant equivalent to PostgreSQL '-infinity'::date, a date that is earlier
31/// than all other dates.
32/// https://www.postgresql.org/docs/current/datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-TABLE
33inline constexpr Date kDateNegativeInfinity = Date::SysDays::min();
34
35namespace io {
36
37/// @brief Binary formatter for utils::datetime::Date
38template <>
40 const Date value;
41
42 explicit BufferFormatter(Date value) : value{value} {}
43
44 template <typename Buffer>
45 void operator()(const UserTypes& types, Buffer& buffer) {
46 static const auto kPgEpoch = PostgresEpochDate();
49 } else if (value == kDateNegativeInfinity) {
51 } else {
52 auto pg_days = static_cast<Integer>(
55 }
56 }
57};
58
59/// @brief Binary parser for utils::datetime::Date
60template <>
63
64 using BaseType::BaseType;
65
66 void operator()(const FieldBuffer& buffer) {
67 static const auto kPgEpoch = PostgresEpochDate();
70 if (pg_days == std::numeric_limits<Integer>::max()) {
72 } else if (pg_days == std::numeric_limits<Integer>::min()) {
74 } else {
76 }
77 }
78};
79
80template <>
81struct CppToSystemPg<Date> : PredefinedOid<PredefinedOids::kDate> {};
82
83} // namespace io
84} // namespace storages::postgres
85
86USERVER_NAMESPACE_END