MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
time_point.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_TIME_TIME_POINT_HPP__
2#define MSTL_CORE_TIME_TIME_POINT_HPP__
3
10
13
14template <typename Clock, typename Dur = typename Clock::duration>
15struct time_point;
16
19
29template <typename CommonT, typename Clock, typename Dummy = void>
30struct __timepoint_common_type {};
31
32template <typename CommonT, typename Clock>
33struct __timepoint_common_type<CommonT, Clock, void_t<typename CommonT::type>> {
34 using type = time_point<Clock, typename CommonT::type>;
35};
36
39
40template <typename Clock, typename Dur1, typename Dur2>
41struct common_type<time_point<Clock, Dur1>, time_point<Clock, Dur2>>
42 : _INNER __timepoint_common_type<common_type<Dur1, Dur2>, Clock>
43{};
44
45template <typename Clock, typename Dur>
46struct common_type<time_point<Clock, Dur>, time_point<Clock, Dur>> {
47 using type = time_point<Clock, Dur>;
48};
49
50template <typename Clock, typename Dur>
51struct common_type<time_point<Clock, Dur>> {
52 using type = time_point<Clock, Dur>;
53};
54
60
71template <typename ToDur, typename Clock, typename Dur, enable_if_t<is_duration_v<ToDur>, int> = 0>
72constexpr time_point<Clock, ToDur> time_cast(const time_point<Clock, Dur>& time_point_value) {
74}
75
84template <typename Clock, typename Dur>
85struct time_point {
86 static_assert(is_duration_v<Dur>, "duration must be a specialization of duration");
87
88 using clock_type = Clock;
89 using duration_type = Dur;
90 using rep = typename duration_type::rep;
91 using period = typename duration_type::period;
92
93private:
94 duration_type value_;
95
96public:
100 constexpr time_point() : value_(duration_type::zero()) {}
101
106 constexpr explicit time_point(const duration_type& dur) : value_(dur) {}
107
113 template <typename Dur2, typename = enable_if_t<is_convertible_v<Dur2, duration_type>>>
115 : value_(value.value_) {}
116
117
122 constexpr time_point& operator ++() {
123 ++value_;
124 return *this;
125 }
126
131 constexpr time_point operator ++(int) {
132 return time_point{value_++};
133 }
134
139 constexpr time_point& operator --() {
140 --value_;
141 return *this;
142 }
143
148 constexpr time_point operator --(int) {
149 return time_point{value_--};
150 }
151
152
158 constexpr time_point& operator +=(const duration_type& dur) {
159 value_ += dur;
160 return *this;
161 }
162
168 constexpr time_point& operator -=(const duration_type& dur) {
169 value_ -= dur;
170 return *this;
171 }
172
177 constexpr duration_type since_epoch() const noexcept {
178 return value_;
179 }
180
185 static constexpr time_point min() noexcept {
186 return time_point(duration_type::min());
187 }
188
193 static constexpr time_point max() noexcept {
194 return time_point(duration_type::max());
195 }
196
197 MSTL_ALWAYS_INLINE time_point<Clock, nanoseconds> to_nano() const {
198 return _MSTL time_cast<nanoseconds>(*this);
199 }
200 MSTL_ALWAYS_INLINE time_point<Clock, microseconds> to_micro() const {
201 return _MSTL time_cast<microseconds>(*this);
202 }
203 MSTL_ALWAYS_INLINE time_point<Clock, milliseconds> to_milli() const {
204 return _MSTL time_cast<milliseconds>(*this);
205 }
206 MSTL_ALWAYS_INLINE time_point<Clock, seconds> to_sec() const {
207 return _MSTL time_cast<seconds>(*this);
208 }
209 MSTL_ALWAYS_INLINE time_point<Clock, minutes> to_minu() const {
210 return _MSTL time_cast<minutes>(*this);
211 }
212 MSTL_ALWAYS_INLINE time_point<Clock, hours> to_hour() const {
213 return _MSTL time_cast<hours>(*this);
214 }
215 MSTL_ALWAYS_INLINE time_point<Clock, days> to_day() const {
216 return _MSTL time_cast<days>(*this);
217 }
218 MSTL_ALWAYS_INLINE time_point<Clock, weeks> to_week() const {
219 return _MSTL time_cast<weeks>(*this);
220 }
221 MSTL_ALWAYS_INLINE time_point<Clock, years> to_year() const {
222 return _MSTL time_cast<years>(*this);
223 }
224 MSTL_ALWAYS_INLINE time_point<Clock, months> to_month() const {
225 return _MSTL time_cast<months>(*this);
226 }
227};
228
229template <typename Rep, typename Period>
230template <typename Clock, typename Dur, typename Dummy>
233
244template <typename Clock, typename Dur1, typename Rep2, typename Period2>
247 using duration2 = duration<Rep2, Period2>;
248 using common_duration = common_type_t<Dur1, duration2>;
249 using result_time_point = time_point<Clock, common_duration>;
250 return result_time_point(lhs.since_epoch() + rhs);
251}
252
263template <typename Rep1, typename Period1, typename Clock, typename Dur2>
266 using duration1 = duration<Rep1, Period1>;
267 using common_duration = common_type_t<duration1, Dur2>;
268 using result_time_point = time_point<Clock, common_duration>;
269 return result_time_point(rhs.since_epoch() + lhs);
270}
271
282template <typename Clock, typename Dur1, typename Rep2, typename Period2>
285 using duration2 = duration<Rep2, Period2>;
286 using common_duration = common_type_t<Dur1, duration2>;
287 using result_time_point = time_point<Clock, common_duration>;
288 return result_time_point(lhs.since_epoch() - rhs);
289}
290
300template <typename Clock, typename Dur1, typename Dur2>
303 return lhs.since_epoch() - rhs.since_epoch();
304}
305
315template <typename Clock, typename Dur1, typename Dur2>
316constexpr bool operator ==(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
317 return lhs.since_epoch() == rhs.since_epoch();
318}
319
329template <typename Clock, typename Dur1, typename Dur2>
330constexpr bool operator !=(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
331 return !(lhs == rhs);
332}
333
343template <typename Clock, typename Dur1, typename Dur2>
344constexpr bool operator <(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
345 return lhs.since_epoch() < rhs.since_epoch();
346}
347
357template <typename Clock, typename Dur1, typename Dur2>
358constexpr bool operator <=(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
359 return !(rhs < lhs);
360}
361
371template <typename Clock, typename Dur1, typename Dur2>
372constexpr bool operator >(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
373 return rhs < lhs;
374}
375
385template <typename Clock, typename Dur1, typename Dur2>
386constexpr bool operator >=(const time_point<Clock, Dur1>& lhs, const time_point<Clock, Dur2>& rhs) {
387 return !(lhs < rhs);
388}
389 // TimePoint
391
393#endif // MSTL_CORE_TIME_TIME_POINT_HPP__
MSTL持续时间类型
MSTL_INLINE17 constexpr bool is_duration_v
is_duration的便捷变量模板
constexpr ToDur time_cast(const duration< Rep, Period > &value)
持续时间类型转换
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_INNER__
结束inner命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
constexpr bool operator>=(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
大于等于比较运算符
constexpr bool operator!=(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
不等于比较运算符
constexpr time_point< Clock, common_type_t< Dur1, duration< Rep2, Period2 > > > operator-(const time_point< Clock, Dur1 > &lhs, const duration< Rep2, Period2 > &rhs)
减法运算符(时间点 - 持续时间)
constexpr time_point< Clock, ToDur > time_cast(const time_point< Clock, Dur > &time_point_value)
时间点类型转换
constexpr bool operator>(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
大于比较运算符
constexpr time_point< Clock, common_type_t< Dur1, duration< Rep2, Period2 > > > operator+(const time_point< Clock, Dur1 > &lhs, const duration< Rep2, Period2 > &rhs)
加法运算符(时间点 + 持续时间)
constexpr bool operator<=(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
小于等于比较运算符
constexpr bool operator<(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
小于比较运算符
constexpr bool operator==(const time_point< Clock, Dur1 > &lhs, const time_point< Clock, Dur2 > &rhs)
等于比较运算符
typename common_type< Types... >::type common_type_t
common_type的便捷别名
void void_t
将任意类型映射为void
查找多个类型的公共类型
持续时间类模板
constexpr duration()=default
默认构造函数
时间点类模板
typename duration_type::period period
时间单位比例
constexpr time_point()
默认构造函数
constexpr time_point & operator++()
前置自增运算符
constexpr time_point(const duration_type &dur)
从持续时间构造
constexpr time_point & operator+=(const duration_type &dur)
加法赋值运算符
constexpr time_point(const time_point< clock_type, Dur2 > &value)
从其他时间点构造
static constexpr time_point max() noexcept
获取最大时间点
constexpr duration_type since_epoch() const noexcept
获取自纪元以来的时间
constexpr time_point & operator-=(const duration_type &dur)
减法赋值运算符
typename duration_type::rep rep
数值类型
Clock clock_type
时钟类型
static constexpr time_point min() noexcept
获取最小时间点
constexpr time_point & operator--()
前置自减运算符
Dur duration_type
持续时间类型