userver: userver/storages/postgres/io/time_of_day.hpp Source File
Loading...
Searching...
No Matches
time_of_day.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/io/time_of_day.hpp
4/// @brief utils::datetime::TimeOfDay I/O support
5/// @ingroup userver_postgres_parse_and_format
6
7#include <userver/utils/time_of_day.hpp>
8
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/type_mapping.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace storages::postgres::io {
17
18/// @brief Binary formatter for utils::datetime::TimeOfDay mapped to postgres
19/// time
20/// This datatype is time-zone agnostic, it should't be mixed with timetz type
21/// or sudden TZ adjustments will be applied.
22template <typename Duration>
23struct BufferFormatter<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>>
24 : detail::BufferFormatterBase<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>> {
25 using BaseType = detail::BufferFormatterBase<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>>;
26 using BaseType::BaseType;
27
28 template <typename Buffer>
29 void operator()(const UserTypes& types, Buffer& buffer) const {
30 Bigint usec = std::chrono::duration_cast<std::chrono::microseconds>(this->value.SinceMidnight()).count();
31 io::WriteBuffer(types, buffer, usec);
32 }
33};
34
35/// @brief Binary parser for utils::datetime::TimeOfDay mapped to postgres time
36template <typename Duration>
37struct BufferParser<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>>
38 : detail::BufferParserBase<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>> {
39 using BaseType = detail::BufferParserBase<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>>;
40 using ValueType = typename BaseType::ValueType;
41 using BaseType::BaseType;
42
43 void operator()(const FieldBuffer& buffer) {
44 Bigint usec{0};
45 io::ReadBuffer(buffer, usec);
46 this->value = ValueType{std::chrono::duration_cast<Duration>(std::chrono::microseconds{usec})};
47 }
48};
49
50template <typename Duration>
51struct CppToSystemPg<USERVER_NAMESPACE::utils::datetime::TimeOfDay<Duration>> : PredefinedOid<PredefinedOids::kTime> {};
52
53} // namespace storages::postgres::io
54
55USERVER_NAMESPACE_END