-
Notifications
You must be signed in to change notification settings - Fork 0
/
Duration.hpp
201 lines (159 loc) · 6.8 KB
/
Duration.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#pragma once
#include <chrono>
#include "Maths.hpp"
namespace MathsCPP {
using namespace std::chrono_literals;
template<class T>
struct is_duration : std::false_type {};
template<class Rep, class Period>
struct is_duration<std::chrono::duration<Rep, Period>> : std::true_type {};
template<class T>
inline constexpr bool is_duration_v = is_duration<T>::value;
template<typename T, typename = std::enable_if_t<is_duration_v<T>>>
class Duration {
public:
constexpr Duration() = default;
template<typename Rep, typename Period>
constexpr Duration(const std::chrono::duration<Rep, Period> &d) : value(std::chrono::duration_cast<T>(d).count()) {}
template<typename T1>
constexpr Duration(const Duration<T1> &d) : value(std::chrono::duration_cast<T>(d.value).count()) {}
template<typename Rep, typename Period>
constexpr Duration &operator=(const std::chrono::duration<Rep, Period> &d) {
value = {std::chrono::duration_cast<T>(d).count()};
return *this;
}
template<typename T1>
constexpr Duration &operator=(const Duration<T1> &d) {
value = {std::chrono::duration_cast<T>(d.value).count()};
return *this;
}
template<typename T1, typename T2 = typename T1::rep, typename = std::enable_if_t<is_duration_v<T1>>>
constexpr auto Cast() const {
return static_cast<T2>(value.count()) / static_cast<T2>(typename std::ratio_divide<typename T::period, typename T1::period>::den);
}
static Duration Now() {
static const auto LocalEpoch = std::chrono::high_resolution_clock::now();
return std::chrono::duration_cast<T>(std::chrono::high_resolution_clock::now() - LocalEpoch);
//auto now = std::chrono::system_clock::now();
//return std::chrono::duration_cast<T>(now.time_since_epoch());
}
static std::string GetDateTime(const std::string &format = "%Y-%m-%d %H:%M:%S") {
auto now = std::chrono::system_clock::now();
auto timeT = std::chrono::system_clock::to_time_t(now);
std::stringstream ss;
ss << std::put_time(std::localtime(&timeT), format.c_str());
return ss.str();
}
template<typename Rep, typename Period>
constexpr operator std::chrono::duration<Rep, Period>() const {
return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator==(const Duration &lhs, const T1 &rhs) {
return lhs.value == Duration<T>(rhs).value;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator!=(const Duration &lhs, const T1 &rhs) {
return lhs.value != Duration<T>(rhs).value;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator<(const Duration &lhs, const T1 &rhs) {
return lhs.value < Duration<T>(rhs).value;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator<=(const Duration &lhs, const T1 &rhs) {
return lhs.value <= Duration<T>(rhs).value;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator>(const Duration &lhs, const T1 &rhs) {
return lhs.value > Duration<T>(rhs).value;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_convertible_v<T1, T>>>
constexpr friend auto operator>=(const Duration &lhs, const T1 &rhs) {
return lhs.value >= Duration<T>(rhs).value;
}
template<typename T1>
constexpr friend auto operator+(const Duration &lhs, const Duration<T1> &rhs) {
return Duration(lhs.value + rhs.value);
}
template<typename T1>
constexpr friend auto operator-(const Duration &lhs, const Duration<T1> &rhs) {
return Duration(lhs.value - rhs.value);
}
template<typename T1>
constexpr friend auto operator*(const Duration &lhs, const Duration<T1> &rhs) {
return Duration(lhs.value * rhs.value);
}
template<typename T1>
constexpr friend auto operator/(const Duration &lhs, const Duration<T1> &rhs) {
return Duration(lhs.value / rhs.value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1>>>
constexpr friend auto operator+(const Duration &lhs, T1 rhs) {
return Duration(lhs.value + rhs);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1>>>
constexpr friend auto operator-(const Duration &lhs, T1 rhs) {
return Duration(lhs.value - rhs);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_arithmetic_v<T1>>>
constexpr friend auto operator*(const Duration &lhs, T1 rhs) {
return Duration(lhs.value * rhs);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_arithmetic_v<T1>>>
constexpr friend auto operator/(const Duration &lhs, T1 rhs) {
return Duration(lhs.value / rhs);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_integral_v<T1>>>
constexpr friend auto operator%(const Duration &lhs, T1 rhs) {
return lhs.value % rhs;
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1>>>
constexpr friend auto operator+(T1 lhs, const Duration &rhs) {
return Duration(lhs + rhs.value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1>>>
constexpr friend auto operator-(T1 lhs, const Duration &rhs) {
return Duration(lhs - rhs.value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_arithmetic_v<T1>>>
constexpr friend auto operator*(T1 lhs, const Duration &rhs) {
return Duration(lhs * rhs.value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_arithmetic_v<T1>>>
constexpr friend auto operator/(T1 lhs, const Duration &rhs) {
return Duration(lhs / rhs.value);
}
template<typename T1, typename = std::enable_if_t<is_duration_v<T1> || std::is_integral_v<T1>>>
constexpr friend auto operator%(T1 lhs, const Duration &rhs) {
return lhs % rhs.value;
}
template<typename T1>
constexpr friend auto operator+=(Duration &lhs, const T1 &rhs) {
return lhs = lhs + rhs;
}
template<typename T1>
constexpr friend auto operator-=(Duration &lhs, const T1 &rhs) {
return lhs = lhs - rhs;
}
template<typename T1>
constexpr friend auto operator*=(Duration &lhs, const T1 &rhs) {
return lhs = lhs * rhs;
}
template<typename T1>
constexpr friend auto operator/=(Duration &lhs, const T1 &rhs) {
return lhs = lhs / rhs;
}
T value{};
};
using Nanoseconds = std::chrono::nanoseconds;
using Microseconds = std::chrono::microseconds;
using Milliseconds = std::chrono::milliseconds;
using Seconds = std::chrono::seconds;
using Minutes = std::chrono::minutes;
using Hours = std::chrono::hours;
//using Durationn = Duration<Nanoseconds>;
//using Durationu = Duration<Microseconds>;
//using Durationm = Duration<Milliseconds>;
//using Durations = Duration<Seconds>;
}