userver: userver/utils/datetime/timepoint_tz.hpp Source File
Loading...
Searching...
No Matches
timepoint_tz.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/datetime/timepoint_tz.hpp
4/// @brief Timepoint with timezone
5/// @ingroup userver_universal
6
7#include <chrono>
8#include <string>
9
10#include <userver/logging/log_helper_fwd.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace utils::datetime {
15
17public:
18 using TimePoint = std::chrono::system_clock::time_point;
19
20 constexpr TimePointTzBase() = default;
21 constexpr explicit TimePointTzBase(TimePoint tp)
22 : tp_(tp)
23 {}
24 constexpr TimePointTzBase(TimePoint tp, std::chrono::seconds tz_offset)
25 : tp_(tp),
26 tz_offset_(tz_offset)
27 {}
28 TimePointTzBase(const TimePointTzBase& other);
29 TimePointTzBase(TimePointTzBase&& other) noexcept;
30
31 TimePointTzBase& operator=(const TimePointTzBase& other);
32 TimePointTzBase& operator=(TimePointTzBase&& other) noexcept;
33
34 // Deliberately no 'explicit' to be compatible with legacy util/swaggen's
35 // plain time_point types
36 operator TimePoint() const;
37
38 /// Get timezone in seconds (may be negative)
39 std::chrono::seconds GetTzOffset() const;
40
41 /// Get std's time_point
42 TimePoint GetTimePoint() const;
43
44 bool operator==(const TimePointTzBase& other) const;
45
46 bool operator<(const TimePointTzBase& other) const;
47
48 bool operator>(const TimePointTzBase& other) const;
49
50 bool operator<=(const TimePointTzBase& other) const;
51
52 bool operator>=(const TimePointTzBase& other) const;
53
54private:
55 TimePoint tp_{};
56 std::chrono::seconds tz_offset_{};
57};
58
59bool operator<(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
60
61bool operator>(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
62
63bool operator<=(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
64
65bool operator>=(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
66
67/// Timepoint with timezone parsed in kFractionFormat
68class TimePointTzFraction final : public TimePointTzBase {
69 using TimePointTzBase::TimePointTzBase;
70
71 explicit TimePointTzFraction(const std::string& timestring);
72};
73
74logging::LogHelper& operator<<(logging::LogHelper& os, const TimePointTzFraction& v);
75
76/// Timepoint with timezone parsed in kRfc3339Format
77class TimePointTz final : public TimePointTzBase {
78 using TimePointTzBase::TimePointTzBase;
79
80 explicit TimePointTz(const std::string& timestring);
81};
82
83logging::LogHelper& operator<<(logging::LogHelper& os, const TimePointTz& v);
84
85/// Timepoint with timezone parsed in kDefaultFormat
86class TimePointTzIsoBasic final : public TimePointTzBase {
87 using TimePointTzBase::TimePointTzBase;
88
89 explicit TimePointTzIsoBasic(const std::string& timestring);
90};
91
92logging::LogHelper& operator<<(logging::LogHelper& os, const TimePointTzIsoBasic& v);
93
94} // namespace utils::datetime
95
96USERVER_NAMESPACE_END