duration.h 21 KB

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