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) : tp_(tp) {}
22 constexpr TimePointTzBase(TimePoint tp, std::chrono::seconds tz_offset) : tp_(tp), tz_offset_(tz_offset) {}
23 TimePointTzBase(const TimePointTzBase& other);
24 TimePointTzBase(TimePointTzBase&& other) noexcept;
25
26 TimePointTzBase& operator=(const TimePointTzBase& other);
27 TimePointTzBase& operator=(TimePointTzBase&& other) noexcept;
28
29 // Deliberately no 'explicit' to be compatible with legacy util/swaggen's
30 // plain time_point types
31 operator TimePoint() const;
32
33 /// Get timezone in seconds (may be negative)
35
36 /// Get std's time_point
37 TimePoint GetTimePoint() const;
38
39 bool operator==(const TimePointTzBase& other) const;
40
41 bool operator<(const TimePointTzBase& other) const;
42
43 bool operator>(const TimePointTzBase& other) const;
44
45 bool operator<=(const TimePointTzBase& other) const;
46
47 bool operator>=(const TimePointTzBase& other) const;
48
49private:
50 TimePoint tp_{};
51 std::chrono::seconds tz_offset_{};
52};
53
54bool operator<(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
55
56bool operator>(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
57
58bool operator<=(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
59
60bool operator>=(const TimePointTzBase::TimePoint& lhs, const TimePointTzBase& rhs);
61
62/// Timepoint with timezone parsed in kRfc3339Format
63class TimePointTz final : public TimePointTzBase {
64 using TimePointTzBase::TimePointTzBase;
65
66 explicit TimePointTz(const std::string& timestring);
67};
68
69logging::LogHelper& operator<<(logging::LogHelper& os, const TimePointTz& v);
70
71/// Timepoint with timezone parsed in kDefaultFormat
72class TimePointTzIsoBasic final : public TimePointTzBase {
73 using TimePointTzBase::TimePointTzBase;
74
75 explicit TimePointTzIsoBasic(const std::string& timestring);
76};
77
78logging::LogHelper& operator<<(logging::LogHelper& os, const TimePointTzIsoBasic& v);
79
80} // namespace utils::datetime
81
82USERVER_NAMESPACE_END