userver
C++ Async Framework
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
15
USERVER_NAMESPACE_BEGIN
16
17
namespace
storages
::
postgres
{
18
19
/// Corresponds to DATE
20
using
Date = USERVER_NAMESPACE::utils::
datetime
::Date;
21
22
/// Postgres epoch date (2000-01-01)
23
Date
PostgresEpochDate
();
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
28
inline
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
33
inline
constexpr
Date
kDateNegativeInfinity
=
Date
::
SysDays
::
min
();
34
35
namespace
io
{
36
37
/// @brief Binary formatter for utils::datetime::Date
38
template
<>
39
struct
BufferFormatter
<
Date
> {
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
();
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
());
51
}
else
{
52
auto
pg_days
=
static_cast
<
Integer
>(
53
(
value
.
GetSysDays
() -
kPgEpoch
.
GetSysDays
()).
count
());
54
WriteBuffer
(
types
,
buffer
,
pg_days
);
55
}
56
}
57
};
58
59
/// @brief Binary parser for utils::datetime::Date
60
template
<>
61
struct
BufferParser
<
Date
> :
detail
::
BufferParserBase
<
Date
> {
62
using
BaseType
=
detail
::
BufferParserBase
<
Date
>;
63
64
using
BaseType
::
BaseType
;
65
66
void
operator
()(
const
FieldBuffer
&
buffer
) {
67
static
const
auto
kPgEpoch
=
PostgresEpochDate
();
68
Integer
pg_days
{0};
69
ReadBuffer
(
buffer
,
pg_days
);
70
if
(
pg_days
==
std
::
numeric_limits
<
Integer
>::
max
()) {
71
this
->
value
=
kDatePositiveInfinity
;
72
}
else
if
(
pg_days
==
std
::
numeric_limits
<
Integer
>::
min
()) {
73
this
->
value
=
kDateNegativeInfinity
;
74
}
else
{
75
this
->
value
=
kPgEpoch
.
GetSysDays
() +
Date
::
Days
{
pg_days
};
76
}
77
}
78
};
79
80
template
<>
81
struct
CppToSystemPg<Date> : PredefinedOid<PredefinedOids::kDate> {};
82
83
}
// namespace io
84
}
// namespace storages::postgres
85
86
USERVER_NAMESPACE_END
userver
storages
postgres
io
date.hpp
Generated on Wed May 15 2024 22:34:31 for userver by
Doxygen
1.10.0