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