duration.h 21 KB

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