-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchronoext.hpp
More file actions
149 lines (127 loc) · 5.73 KB
/
chronoext.hpp
File metadata and controls
149 lines (127 loc) · 5.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Chrono library extentions.
//
// Copyright (C) 2025, Martin Young <martin_young@live.cn>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//------------------------------------------------------------------------
#pragma once
#include <concepts>
#include <chrono>
#include <map>
#include <string>
#include <stdexcept>
#if defined(__cpp_lib_chrono) && (__cpp_lib_chrono >= 201907L)
#define HAS_ZONED_TIME 1
#else
#define HAS_ZONED_TIME 0
#endif
#if !HAS_ZONED_TIME
#include <iomanip>
#include <ctime>
#endif
using namespace std::chrono_literals;
namespace chrono = std::chrono;
using StdClkTP = chrono::time_point<chrono::steady_clock>;
using StdClkDur = chrono::steady_clock::duration;
using SysClkTP = chrono::time_point<chrono::system_clock>;
using SysClkDur = chrono::system_clock::duration;
enum class TimeUnit { Nano, Micro, Milli, Second, Minute, Hour, Day, Week, Month, Year };
inline StdClkDur time_dure(time_t value, TimeUnit unit)
{
switch (unit) {
case TimeUnit::Nano: return chrono::nanoseconds(value);
case TimeUnit::Micro: return chrono::microseconds(value);
case TimeUnit::Milli: return chrono::milliseconds(value);
case TimeUnit::Second: return chrono::seconds(value);
case TimeUnit::Minute: return chrono::minutes(value);
case TimeUnit::Hour: return chrono::hours(value);
case TimeUnit::Day: return chrono::days(value); // Since C++ 20
case TimeUnit::Week: return chrono::weeks(value); // Since C++ 20
case TimeUnit::Month: return chrono::months(value); // Since C++ 20
case TimeUnit::Year: return chrono::years(value); // Since C++ 20
default:
throw std::invalid_argument("Unsupported time unit");
}
}
template <typename T> inline StdClkDur time_dure(time_t value) noexcept { return T(value); }
inline auto stdnow() noexcept { return chrono::steady_clock::now(); }
inline auto sysnow() noexcept { return chrono::system_clock::now(); }
#if HAS_ZONED_TIME
inline auto locnow() { return chrono::zoned_time{chrono::current_zone(), sysnow()}; }
#else
inline auto locnow() { return chrono::system_clock::to_time_t(sysnow()); }
#endif
enum class DateChar { dash, dot };
//------------------------------------------------------------------------------------------------
template <typename Clock, typename Duration>
inline std::string str_datetime(const std::chrono::time_point<Clock, Duration>& tp, DateChar deli=DateChar::dash) noexcept
{
#if HAS_ZONED_TIME
const auto local_time = chrono::zoned_time{chrono::current_zone(), chrono::floor<chrono::seconds>(chrono::clock_cast<chrono::system_clock>(tp))}.get_local_time();
return deli==DateChar::dash? std::format("{:%Y-%m-%d %H:%M:%S}",local_time) : std::format("{:%Y.%m.%d %H:%M:%S}",local_time);
#else
static_assert(
std::is_same_v<Clock, std::chrono::system_clock>,
"Only support system_clock::time_point parameter"
);
const auto tt = Clock::to_time_t(tp);
std::ostringstream oss;
oss << std::put_time(std::localtime(&tt), deli==DateChar::dash? "%Y-%m-%d %H:%M:%S" : "%Y.%m.%d %H:%M:%S");
return oss.str();
#endif
}
//------------------------------------------------------------------------------------------------
template <typename Clock, typename Duration>
inline std::string str_date(const std::chrono::time_point<Clock, Duration>& tp, DateChar deli=DateChar::dash) noexcept
{
#if HAS_ZONED_TIME
const auto local_time = chrono::zoned_time{chrono::current_zone(), chrono::floor<chrono::days>(chrono::clock_cast<chrono::system_clock>(tp))}.get_local_time();
return deli==DateChar::dash? std::format("{:%Y-%m-%d}",local_time) : std::format("{:%Y.%m.%d}",local_time);
#else
static_assert(
std::is_same_v<Clock, std::chrono::system_clock>,
"Only support system_clock::time_point parameter"
);
const auto tt = Clock::to_time_t(tp);
std::ostringstream oss;
oss << std::put_time(std::localtime(&tt), deli==DateChar::dash? "%Y-%m-%d" : "%Y.%m.%d");
return oss.str();
#endif
}
//------------------------------------------------------------------------------------------------
class TTimeout {
private:
StdClkDur dur_{}; // declaration must before tp_ to ensure init sequence
StdClkTP tp_{}; // init value set to stdnow()-dur_, to ensure that first time calling expires() returns true
public:
TTimeout(StdClkDur dur)
: dur_(dur), tp_(stdnow()-dur_) {}
TTimeout(time_t d, TimeUnit unit)
: dur_(time_dure(d, unit)), tp_(stdnow()-dur_) {}
template <typename T> TTimeout(time_t d)
: dur_(time_dure<T>(d)), tp_(stdnow()-dur_) {}
TTimeout& operator=(StdClkDur dur) {
dur_ = dur;
tp_ = stdnow() - dur_;
return *this;
}
bool expires() noexcept { return stdnow()-tp_ >= dur_; }
void reset() noexcept { tp_ = stdnow(); }
TTimeout(const TTimeout&) = default;
TTimeout& operator=(const TTimeout&) = default;
TTimeout() = default;
~TTimeout() = default;
};
using TimeoutManager = std::map<std::string, TTimeout>;
//------------------------------------------------------------------------------------------------