duration.h 22 KB

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