duration.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___CHRONO_DURATION_H
  10. #define _LIBCPP___CHRONO_DURATION_H
  11. #include <__compare/ordering.h>
  12. #include <__compare/three_way_comparable.h>
  13. #include <__config>
  14. #include <__type_traits/common_type.h>
  15. #include <__type_traits/enable_if.h>
  16. #include <__type_traits/is_convertible.h>
  17. #include <__type_traits/is_floating_point.h>
  18. #include <limits>
  19. #include <ratio>
  20. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  21. # pragma GCC system_header
  22. #endif
  23. _LIBCPP_PUSH_MACROS
  24. #include <__undef_macros>
  25. _LIBCPP_BEGIN_NAMESPACE_STD
  26. namespace chrono {
  27. template <class _Rep, class _Period = ratio<1> >
  28. class _LIBCPP_TEMPLATE_VIS duration;
  29. template <class _Tp>
  30. struct __is_duration : false_type {};
  31. template <class _Rep, class _Period>
  32. struct __is_duration<duration<_Rep, _Period> > : true_type {};
  33. template <class _Rep, class _Period>
  34. struct __is_duration<const duration<_Rep, _Period> > : true_type {};
  35. template <class _Rep, class _Period>
  36. struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
  37. template <class _Rep, class _Period>
  38. struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
  39. } // namespace chrono
  40. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  41. struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>, chrono::duration<_Rep2, _Period2> > {
  42. typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type, typename __ratio_gcd<_Period1, _Period2>::type>
  43. type;
  44. };
  45. namespace chrono {
  46. // duration_cast
  47. template <class _FromDuration,
  48. class _ToDuration,
  49. class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
  50. bool = _Period::num == 1,
  51. bool = _Period::den == 1>
  52. struct __duration_cast;
  53. template <class _FromDuration, class _ToDuration, class _Period>
  54. struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true> {
  55. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration operator()(const _FromDuration& __fd) const {
  56. return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
  57. }
  58. };
  59. template <class _FromDuration, class _ToDuration, class _Period>
  60. struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false> {
  61. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration operator()(const _FromDuration& __fd) const {
  62. typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
  63. return _ToDuration(
  64. static_cast<typename _ToDuration::rep>(static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
  65. }
  66. };
  67. template <class _FromDuration, class _ToDuration, class _Period>
  68. struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true> {
  69. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration operator()(const _FromDuration& __fd) const {
  70. typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
  71. return _ToDuration(
  72. static_cast<typename _ToDuration::rep>(static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
  73. }
  74. };
  75. template <class _FromDuration, class _ToDuration, class _Period>
  76. struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false> {
  77. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration operator()(const _FromDuration& __fd) const {
  78. typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
  79. return _ToDuration(static_cast<typename _ToDuration::rep>(
  80. static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num) / static_cast<_Ct>(_Period::den)));
  81. }
  82. };
  83. template <class _ToDuration, class _Rep, class _Period, __enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  84. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration duration_cast(const duration<_Rep, _Period>& __fd) {
  85. return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
  86. }
  87. template <class _Rep>
  88. struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
  89. #if _LIBCPP_STD_VER >= 17
  90. template <class _Rep>
  91. inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
  92. #endif
  93. template <class _Rep>
  94. struct _LIBCPP_TEMPLATE_VIS duration_values {
  95. public:
  96. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR _Rep zero() _NOEXCEPT { return _Rep(0); }
  97. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR _Rep max() _NOEXCEPT { return numeric_limits<_Rep>::max(); }
  98. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR _Rep min() _NOEXCEPT { return numeric_limits<_Rep>::lowest(); }
  99. };
  100. #if _LIBCPP_STD_VER >= 17
  101. template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  102. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration floor(const duration<_Rep, _Period>& __d) {
  103. _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
  104. if (__t > __d)
  105. __t = __t - _ToDuration{1};
  106. return __t;
  107. }
  108. template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  109. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration ceil(const duration<_Rep, _Period>& __d) {
  110. _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
  111. if (__t < __d)
  112. __t = __t + _ToDuration{1};
  113. return __t;
  114. }
  115. template <class _ToDuration, class _Rep, class _Period, enable_if_t<__is_duration<_ToDuration>::value, int> = 0>
  116. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR _ToDuration round(const duration<_Rep, _Period>& __d) {
  117. _ToDuration __lower = chrono::floor<_ToDuration>(__d);
  118. _ToDuration __upper = __lower + _ToDuration{1};
  119. auto __lower_diff = __d - __lower;
  120. auto __upper_diff = __upper - __d;
  121. if (__lower_diff < __upper_diff)
  122. return __lower;
  123. if (__lower_diff > __upper_diff)
  124. return __upper;
  125. return __lower.count() & 1 ? __upper : __lower;
  126. }
  127. #endif
  128. // duration
  129. template <class _Rep, class _Period>
  130. class _LIBCPP_TEMPLATE_VIS duration {
  131. static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
  132. static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
  133. static_assert(_Period::num > 0, "duration period must be positive");
  134. template <class _R1, class _R2>
  135. struct __no_overflow {
  136. private:
  137. static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
  138. static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
  139. static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
  140. static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
  141. static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
  142. static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
  143. static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
  144. template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
  145. struct __mul // __overflow == false
  146. {
  147. static const intmax_t value = _Xp * _Yp;
  148. };
  149. template <intmax_t _Xp, intmax_t _Yp>
  150. struct __mul<_Xp, _Yp, true> {
  151. static const intmax_t value = 1;
  152. };
  153. public:
  154. static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
  155. typedef ratio<__mul<__n1, __d2, !value>::value, __mul<__n2, __d1, !value>::value> type;
  156. };
  157. public:
  158. typedef _Rep rep;
  159. typedef typename _Period::type period;
  160. private:
  161. rep __rep_;
  162. public:
  163. #ifndef _LIBCPP_CXX03_LANG
  164. constexpr duration() = default;
  165. #else
  166. _LIBCPP_HIDE_FROM_ABI duration() {}
  167. #endif
  168. template <class _Rep2,
  169. __enable_if_t<is_convertible<const _Rep2&, rep>::value &&
  170. (treat_as_floating_point<rep>::value || !treat_as_floating_point<_Rep2>::value),
  171. int> = 0>
  172. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR explicit duration(const _Rep2& __r) : __rep_(__r) {}
  173. // conversions
  174. template <class _Rep2,
  175. class _Period2,
  176. __enable_if_t<__no_overflow<_Period2, period>::value && (treat_as_floating_point<rep>::value ||
  177. (__no_overflow<_Period2, period>::type::den == 1 &&
  178. !treat_as_floating_point<_Rep2>::value)),
  179. int> = 0>
  180. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration(const duration<_Rep2, _Period2>& __d)
  181. : __rep_(chrono::duration_cast<duration>(__d).count()) {}
  182. // observer
  183. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR rep count() const { return __rep_; }
  184. // arithmetic
  185. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {
  186. return typename common_type<duration>::type(*this);
  187. }
  188. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {
  189. return typename common_type<duration>::type(-__rep_);
  190. }
  191. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator++() {
  192. ++__rep_;
  193. return *this;
  194. }
  195. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration operator++(int) { return duration(__rep_++); }
  196. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator--() {
  197. --__rep_;
  198. return *this;
  199. }
  200. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration operator--(int) { return duration(__rep_--); }
  201. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator+=(const duration& __d) {
  202. __rep_ += __d.count();
  203. return *this;
  204. }
  205. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator-=(const duration& __d) {
  206. __rep_ -= __d.count();
  207. return *this;
  208. }
  209. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator*=(const rep& __rhs) {
  210. __rep_ *= __rhs;
  211. return *this;
  212. }
  213. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator/=(const rep& __rhs) {
  214. __rep_ /= __rhs;
  215. return *this;
  216. }
  217. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const rep& __rhs) {
  218. __rep_ %= __rhs;
  219. return *this;
  220. }
  221. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const duration& __rhs) {
  222. __rep_ %= __rhs.count();
  223. return *this;
  224. }
  225. // special values
  226. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR duration zero() _NOEXCEPT {
  227. return duration(duration_values<rep>::zero());
  228. }
  229. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR duration min() _NOEXCEPT {
  230. return duration(duration_values<rep>::min());
  231. }
  232. _LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR duration max() _NOEXCEPT {
  233. return duration(duration_values<rep>::max());
  234. }
  235. };
  236. typedef duration<long long, nano> nanoseconds;
  237. typedef duration<long long, micro> microseconds;
  238. typedef duration<long long, milli> milliseconds;
  239. typedef duration<long long > seconds;
  240. typedef duration< long, ratio< 60> > minutes;
  241. typedef duration< long, ratio<3600> > hours;
  242. #if _LIBCPP_STD_VER >= 20
  243. typedef duration< int, ratio_multiply<ratio<24>, hours::period>> days;
  244. typedef duration< int, ratio_multiply<ratio<7>, days::period>> weeks;
  245. typedef duration< int, ratio_multiply<ratio<146097, 400>, days::period>> years;
  246. typedef duration< int, ratio_divide<years::period, ratio<12>>> months;
  247. #endif
  248. // Duration ==
  249. template <class _LhsDuration, class _RhsDuration>
  250. struct __duration_eq {
  251. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const {
  252. typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
  253. return _Ct(__lhs).count() == _Ct(__rhs).count();
  254. }
  255. };
  256. template <class _LhsDuration>
  257. struct __duration_eq<_LhsDuration, _LhsDuration> {
  258. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const {
  259. return __lhs.count() == __rhs.count();
  260. }
  261. };
  262. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  263. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  264. operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  265. return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
  266. }
  267. #if _LIBCPP_STD_VER <= 17
  268. // Duration !=
  269. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  270. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  271. operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  272. return !(__lhs == __rhs);
  273. }
  274. #endif // _LIBCPP_STD_VER <= 17
  275. // Duration <
  276. template <class _LhsDuration, class _RhsDuration>
  277. struct __duration_lt {
  278. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const {
  279. typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
  280. return _Ct(__lhs).count() < _Ct(__rhs).count();
  281. }
  282. };
  283. template <class _LhsDuration>
  284. struct __duration_lt<_LhsDuration, _LhsDuration> {
  285. _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const {
  286. return __lhs.count() < __rhs.count();
  287. }
  288. };
  289. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  290. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  291. operator<(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  292. return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
  293. }
  294. // Duration >
  295. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  296. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  297. operator>(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  298. return __rhs < __lhs;
  299. }
  300. // Duration <=
  301. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  302. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  303. operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  304. return !(__rhs < __lhs);
  305. }
  306. // Duration >=
  307. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  308. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool
  309. operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  310. return !(__lhs < __rhs);
  311. }
  312. #if _LIBCPP_STD_VER >= 20
  313. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  314. requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
  315. _LIBCPP_HIDE_FROM_ABI constexpr auto
  316. operator<=>(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  317. using _Ct = common_type_t<duration<_Rep1, _Period1>, duration<_Rep2, _Period2>>;
  318. return _Ct(__lhs).count() <=> _Ct(__rhs).count();
  319. }
  320. #endif // _LIBCPP_STD_VER >= 20
  321. // Duration +
  322. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  323. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  324. typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
  325. operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  326. typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
  327. return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
  328. }
  329. // Duration -
  330. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  331. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  332. typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
  333. operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  334. typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
  335. return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
  336. }
  337. // Duration *
  338. template <class _Rep1,
  339. class _Period,
  340. class _Rep2,
  341. __enable_if_t<is_convertible<const _Rep2&, typename common_type<_Rep1, _Rep2>::type>::value, int> = 0>
  342. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  343. operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) {
  344. typedef typename common_type<_Rep1, _Rep2>::type _Cr;
  345. typedef duration<_Cr, _Period> _Cd;
  346. return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
  347. }
  348. template <class _Rep1,
  349. class _Period,
  350. class _Rep2,
  351. __enable_if_t<is_convertible<const _Rep1&, typename common_type<_Rep1, _Rep2>::type>::value, int> = 0>
  352. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  353. operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) {
  354. return __d * __s;
  355. }
  356. // Duration /
  357. template <class _Rep1,
  358. class _Period,
  359. class _Rep2,
  360. __enable_if_t<!__is_duration<_Rep2>::value &&
  361. is_convertible<const _Rep2&, typename common_type<_Rep1, _Rep2>::type>::value,
  362. int> = 0>
  363. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  364. operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) {
  365. typedef typename common_type<_Rep1, _Rep2>::type _Cr;
  366. typedef duration<_Cr, _Period> _Cd;
  367. return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
  368. }
  369. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  370. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR typename common_type<_Rep1, _Rep2>::type
  371. operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  372. typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
  373. return _Ct(__lhs).count() / _Ct(__rhs).count();
  374. }
  375. // Duration %
  376. template <class _Rep1,
  377. class _Period,
  378. class _Rep2,
  379. __enable_if_t<!__is_duration<_Rep2>::value &&
  380. is_convertible<const _Rep2&, typename common_type<_Rep1, _Rep2>::type>::value,
  381. int> = 0>
  382. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  383. operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) {
  384. typedef typename common_type<_Rep1, _Rep2>::type _Cr;
  385. typedef duration<_Cr, _Period> _Cd;
  386. return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
  387. }
  388. template <class _Rep1, class _Period1, class _Rep2, class _Period2>
  389. inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR
  390. typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
  391. operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) {
  392. typedef typename common_type<_Rep1, _Rep2>::type _Cr;
  393. typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
  394. return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
  395. }
  396. } // namespace chrono
  397. #if _LIBCPP_STD_VER >= 14
  398. // Suffixes for duration literals [time.duration.literals]
  399. inline namespace literals {
  400. inline namespace chrono_literals {
  401. _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours operator""h(unsigned long long __h) {
  402. return chrono::hours(static_cast<chrono::hours::rep>(__h));
  403. }
  404. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<3600, 1>> operator""h(long double __h) {
  405. return chrono::duration<long double, ratio<3600, 1>>(__h);
  406. }
  407. _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes operator""min(unsigned long long __m) {
  408. return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
  409. }
  410. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<60, 1>> operator""min(long double __m) {
  411. return chrono::duration<long double, ratio<60, 1>>(__m);
  412. }
  413. _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds operator""s(unsigned long long __s) {
  414. return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
  415. }
  416. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double> operator""s(long double __s) {
  417. return chrono::duration<long double>(__s);
  418. }
  419. _LIBCPP_HIDE_FROM_ABI constexpr chrono::milliseconds operator""ms(unsigned long long __ms) {
  420. return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
  421. }
  422. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, milli> operator""ms(long double __ms) {
  423. return chrono::duration<long double, milli>(__ms);
  424. }
  425. _LIBCPP_HIDE_FROM_ABI constexpr chrono::microseconds operator""us(unsigned long long __us) {
  426. return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
  427. }
  428. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, micro> operator""us(long double __us) {
  429. return chrono::duration<long double, micro>(__us);
  430. }
  431. _LIBCPP_HIDE_FROM_ABI constexpr chrono::nanoseconds operator""ns(unsigned long long __ns) {
  432. return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
  433. }
  434. _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, nano> operator""ns(long double __ns) {
  435. return chrono::duration<long double, nano>(__ns);
  436. }
  437. } // namespace chrono_literals
  438. } // namespace literals
  439. namespace chrono { // hoist the literals into namespace std::chrono
  440. using namespace literals::chrono_literals;
  441. } // namespace chrono
  442. #endif // _LIBCPP_STD_VER >= 14
  443. _LIBCPP_END_NAMESPACE_STD
  444. _LIBCPP_POP_MACROS
  445. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  446. # include <type_traits>
  447. #endif
  448. #endif // _LIBCPP___CHRONO_DURATION_H