1#ifndef MSTL_CORE_TIME_DURATION_HPP__
2#define MSTL_CORE_TIME_DURATION_HPP__
15template <
typename Rep,
typename Period = ratio<1>>
18template <
typename Clock,
typename Dur>
34template <
typename CommonT,
typename Period1,
typename Period2,
typename Dummy =
void>
35struct __duration_common_type {};
37template <
typename CommonT,
typename Period1,
typename Period2>
38struct __duration_common_type<CommonT, Period1, Period2,
void_t<typename CommonT::type>> {
40 using gcd_numerator = static_gcd<Period1::num, Period2::num>;
41 using gcd_denominator = static_gcd<Period1::den, Period2::den>;
42 using common_rep =
typename CommonT::type;
43 using result_ratio = ratio<
45 (Period1::den / gcd_denominator::value) * Period2::den
49 using type = duration<common_rep, typename result_ratio::type>;
55template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
57 :
_INNER __duration_common_type<common_type<Rep1, Rep2>, typename Period1::type, typename Period2::type>
60template <
typename Rep,
typename Period>
62 using type = duration<common_type_t<Rep>,
typename Period::type>;
65template <
typename Rep,
typename Period>
67 using type = duration<common_type_t<Rep>,
typename Period::type>;
73template <
typename ToDur,
typename ConvFactor,
typename CommonRep,
bool NumIsOne = false,
bool DenIsOne = false>
74struct __duration_cast_impl {
75 template <
typename Rep,
typename Period>
76 static constexpr ToDur __cast(
const duration<Rep, Period>& value) {
77 return ToDur(
static_cast<typename ToDur::rep
>(
static_cast<CommonRep
>(value.
count())
78 *
static_cast<CommonRep
>(ConvFactor::num) /
static_cast<CommonRep
>(ConvFactor::den)));
82template <
typename ToDur,
typename ConvFactor,
typename CommonRep>
83struct __duration_cast_impl<ToDur, ConvFactor, CommonRep, true, true> {
84 template <
typename Rep,
typename Period>
85 static constexpr ToDur __cast(
const duration<Rep, Period>& value) {
86 return ToDur(
static_cast<typename ToDur::rep
>(value.
count()));
90template <
typename ToDur,
typename ConvFactor,
typename CommonRep>
91struct __duration_cast_impl<ToDur, ConvFactor, CommonRep, true, false> {
92 template <
typename Rep,
typename Period>
93 static constexpr ToDur __cast(
const duration<Rep, Period>& value) {
94 return ToDur(
static_cast<typename ToDur::rep
>(
95 static_cast<CommonRep
>(value.
count()) /
static_cast<CommonRep
>(ConvFactor::den)));
99template <
typename ToDur,
typename ConvFactor,
typename CommonRep>
100struct __duration_cast_impl<ToDur, ConvFactor, CommonRep, false, true> {
101 template <
typename Rep,
typename Period>
102 static constexpr ToDur __cast(
const duration<Rep, Period>& value) {
103 return ToDur(
static_cast<typename ToDur::rep
>(
104 static_cast<CommonRep
>(value.
count()) *
static_cast<CommonRep
>(ConvFactor::num)));
125template <
typename Rep,
typename Period>
146template <typename ToDur, typename Rep, typename Period, enable_if_t<is_duration<ToDur>::value,
int> = 0>
148 using to_period =
typename ToDur::period;
149 using to_rep =
typename ToDur::rep;
152 using duration_caster =
_INNER __duration_cast_impl<ToDur, conversion_factor, common_rep,
153 conversion_factor::num == 1, conversion_factor::den == 1>;
154 return duration_caster::__cast(value);
178template <
typename Rep,
typename Period>
181 static_assert(is_ratio_v<Period>,
"period must be a specialization of ratio");
182 static_assert(Period::num > 0,
"period must be positive");
192 template <
typename R1,
typename R2,
195 using divide =
ratio<(R1::num / Gcd1) * (R2::den / Gcd2), (R1::den / Gcd2) * (R2::num / Gcd1)>;
201 template <
typename Period2>
234 template <
typename Rep2,
typename = enable_if_t<is_convertible_v<const Rep2&, rep>>>
235 constexpr explicit duration(
const Rep2& rep2) : rep_(static_cast<
rep>(rep2)) {}
246 template <
typename Rep2,
typename Period2,
typename Dummy =
enable_if_t<
260 template <
typename Clock,
typename Dur,
typename Dummy = enable_if_t<is_duration_v<Dur>>>
268 constexpr rep count() const noexcept {
return rep_; }
368 template <
typename U = rep, enable_if_t<!is_
floating_po
int_v<U>,
int> = 0>
380 template <
typename U = rep, enable_if_t<!is_
floating_po
int_v<U>,
int> = 0>
451template <
typename Rep1,
typename Rep2,
469template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
475 return common_duration(common_duration(lhs).
count() + common_duration(rhs).
count());
488template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
494 return common_duration(common_duration(lhs).
count() - common_duration(rhs).
count());
506template <
typename Rep1,
typename Period,
typename Rep2>
510 return common_duration(common_duration(value).
count() * scalar);
522template <
typename Rep1,
typename Rep2,
typename Period>
525 return value * scalar;
537template <
typename Rep1,
typename Period,
typename Rep2>
541 return common_duration(common_duration(value).
count() / scalar);
554template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
560 return common_duration(lhs).count() / common_duration(rhs).count();
572template <
typename Rep1,
typename Period,
typename Rep2>
576 return common_duration(common_duration(value).
count() % scalar);
589template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
595 return common_duration(common_duration(lhs).
count() % common_duration(rhs).
count());
608template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
613 return common_duration(lhs).count() == common_duration(rhs).count();
626template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
628 return !(lhs == rhs);
641template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
646 return common_duration(lhs).count() < common_duration(rhs).count();
659template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
674template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
689template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
703template <
typename ToDur,
typename Rep,
typename Period>
708 return to - ToDur{1};
721template <
typename ToDur,
typename Rep,
typename Period>
726 return to + ToDur{1};
742template <
typename Dur,
char... Digits>
743constexpr Dur __check_overflow() noexcept {
745 constexpr typename Dur::rep rep = value_type::value;
747 rep >= 0 && rep == value_type::value,
748 "literal value cannot be represented by duration type");
781template <
char... Digits>
782constexpr hours operator ""_h() noexcept {
783 return _INNER __check_overflow<
hours, Digits...>();
800template <
char... Digits>
819template <
char... Digits>
838template <
char... Digits>
857template <
char... Digits>
876template <
char... Digits>
905template <
typename Rep,
typename Period>
907 if (time <= time.
zero())
return;
909#ifdef MSTL_PLATFORM_WINDOWS__
912#elif defined(MSTL_PLATFORM_LINUX__)
913 const seconds s = time.to_sec();
static MSTL_NODISCARD constexpr T lowest() noexcept
获取类型的最低值
static MSTL_NODISCARD constexpr T max() noexcept
获取类型的最大值
long double decimal_t
扩展精度浮点数类型
long long int64_t
64位有符号整数类型
constexpr iter_difference_t< Iterator > count(Iterator first, Iterator last, const T &value)
统计范围内等于指定值的元素数量
duration< int64_t, micro > microseconds
微秒持续时间
MSTL_INLINE17 constexpr bool is_duration_v
is_duration的便捷变量模板
duration< int64_t, milli > milliseconds
毫秒持续时间
duration< int64_t, ratio< 86400 > > days
天持续时间
constexpr bool operator>(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
大于比较运算符
duration< int64_t, ratio< 3600 > > hours
小时持续时间
constexpr bool operator<(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
小于比较运算符
duration< int64_t, ratio< 31556952 > > years
年持续时间(天文年)
constexpr bool operator!=(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
不等于比较运算符
constexpr duration< _INNER __common_rep_t< Rep1, enable_if_t<!is_duration_v< Rep2 >, Rep2 > >, Period > operator%(const duration< Rep1, Period > &value, const Rep2 &scalar)
取模运算符(持续时间 % 标量)
MSTL_NODISCARD constexpr enable_if_t< is_duration_v< ToDur >, ToDur > ceil(const duration< Rep, Period > &dur)
向上取整持续时间转换
constexpr bool operator>=(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
大于等于比较运算符
MSTL_NODISCARD constexpr enable_if_t< is_duration_v< ToDur >, ToDur > floor(const duration< Rep, Period > &dur)
向下取整持续时间转换
constexpr bool operator==(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
等于比较运算符
constexpr duration< _INNER __common_rep_t< Rep1, enable_if_t<!is_duration_v< Rep2 >, Rep2 > >, Period > operator/(const duration< Rep1, Period > &value, const Rep2 &scalar)
除法运算符(持续时间 / 标量)
constexpr duration< _INNER __common_rep_t< Rep1, Rep2 >, Period > operator*(const duration< Rep1, Period > &value, const Rep2 &scalar)
乘法运算符(持续时间 * 标量)
duration< int64_t, nano > nanoseconds
纳秒持续时间
duration< int64_t, ratio< 60 > > minutes
分钟持续时间
duration< int64_t, ratio< 2629746 > > months
月持续时间(平均月)
constexpr common_type_t< duration< Rep1, Period1 >, duration< Rep2, Period2 > > operator+(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
加法运算符
constexpr common_type_t< duration< Rep1, Period1 >, duration< Rep2, Period2 > > operator-(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
减法运算符
duration< int64_t, ratio< 604800 > > weeks
周持续时间
constexpr bool operator<=(const duration< Rep1, Period1 > &lhs, const duration< Rep2, Period2 > &rhs)
小于等于比较运算符
constexpr ToDur time_cast(const duration< Rep, Period > &value)
持续时间类型转换
duration< int64_t > seconds
秒持续时间
MSTL_CONST_FUNCTION MSTL_CONSTEXPR14 T gcd(const T &m, const T &n) noexcept
计算最大公约数
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_LITERALS__
结束literals命名空间
#define MSTL_BEGIN_THIS_THREAD__
this_thread命名空间
#define MSTL_END_INNER__
结束inner命名空间
#define MSTL_END_THIS_THREAD__
结束this_thread命名空间
#define _INNER
inner命名空间前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
#define MSTL_BEGIN_INNER__
开始inner命名空间
#define MSTL_BEGIN_LITERALS__
开始literals命名空间(内联)
typename _INNER __ratio_divide_impl< ratio1, ratio2 >::type ratio_divide
比率除法类型别名
void sleep_for(const duration< Rep, Period > time)
使当前线程睡眠指定时间
constexpr duration(time_point< Clock, Dur > tp)
从其他时间点构造
typename common_type< Types... >::type common_type_t
common_type的便捷别名
constexpr T initialize() noexcept(is_nothrow_default_constructible< T >::value)
返回类型T的默认初始化值
bool_constant< true > true_type
表示true的类型
bool_constant< false > false_type
表示false的类型
integral_constant< bool, Value > bool_constant
布尔常量包装器
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr duration & operator+=(const duration &dur)
加法赋值运算符
constexpr duration()=default
默认构造函数
constexpr duration & operator--()
前置自减运算符
constexpr duration(const duration< Rep2, Period2 > &dur)
从其他持续时间构造
constexpr duration & operator%=(const rep &rhs)
取模赋值运算符
duration(const duration &)=default
复制构造函数
typename Period::type period
constexpr duration & operator/=(const rep &rhs)
除法赋值运算符
constexpr duration & operator-=(const duration &dur)
减法赋值运算符
constexpr duration(const Rep2 &rep2)
从数值构造
constexpr rep count() const noexcept
获取计数值
constexpr duration & operator=(const duration &)=default
复制赋值运算符
constexpr duration & operator++()
前置自增运算符
constexpr duration< common_type_t< rep >, period > operator+() const
一元正号运算符
constexpr duration< common_type_t< rep >, period > operator-() const
一元负号运算符
constexpr duration & operator*=(const rep &rhs)
乘法赋值运算符
static constexpr duration zero() noexcept
获取零持续时间
static constexpr duration min() noexcept
获取最小持续时间
static constexpr duration max() noexcept
获取最大持续时间
static constexpr bool value