chrono 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. // <chrono> -*- C++ -*-
  2. // Copyright (C) 2008-2019 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file include/chrono
  21. * This is a Standard C++ Library header.
  22. */
  23. #ifndef _GLIBCXX_CHRONO
  24. #define _GLIBCXX_CHRONO 1
  25. #pragma GCC system_header
  26. #if __cplusplus < 201103L
  27. # include <bits/c++0x_warning.h>
  28. #else
  29. #include <ratio>
  30. #include <type_traits>
  31. #include <limits>
  32. #include <ctime>
  33. #include <bits/parse_numbers.h> // for literals support.
  34. namespace std _GLIBCXX_VISIBILITY(default)
  35. {
  36. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  37. /**
  38. * @defgroup chrono Time
  39. * @ingroup utilities
  40. *
  41. * Classes and functions for time.
  42. * @{
  43. */
  44. /** @namespace std::chrono
  45. * @brief ISO C++ 2011 entities sub-namespace for time and date.
  46. */
  47. namespace chrono
  48. {
  49. template<typename _Rep, typename _Period = ratio<1>>
  50. struct duration;
  51. template<typename _Clock, typename _Dur = typename _Clock::duration>
  52. struct time_point;
  53. }
  54. // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
  55. template<typename _CT, typename _Period1, typename _Period2>
  56. struct __duration_common_type_wrapper
  57. {
  58. private:
  59. typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
  60. typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
  61. typedef typename _CT::type __cr;
  62. typedef ratio<__gcd_num::value,
  63. (_Period1::den / __gcd_den::value) * _Period2::den> __r;
  64. public:
  65. typedef __success_type<chrono::duration<__cr, __r>> type;
  66. };
  67. template<typename _Period1, typename _Period2>
  68. struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
  69. { typedef __failure_type type; };
  70. template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
  71. struct common_type<chrono::duration<_Rep1, _Period1>,
  72. chrono::duration<_Rep2, _Period2>>
  73. : public __duration_common_type_wrapper<typename __member_type_wrapper<
  74. common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
  75. { };
  76. // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
  77. template<typename _CT, typename _Clock>
  78. struct __timepoint_common_type_wrapper
  79. {
  80. typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
  81. type;
  82. };
  83. template<typename _Clock>
  84. struct __timepoint_common_type_wrapper<__failure_type, _Clock>
  85. { typedef __failure_type type; };
  86. template<typename _Clock, typename _Duration1, typename _Duration2>
  87. struct common_type<chrono::time_point<_Clock, _Duration1>,
  88. chrono::time_point<_Clock, _Duration2>>
  89. : public __timepoint_common_type_wrapper<typename __member_type_wrapper<
  90. common_type<_Duration1, _Duration2>>::type, _Clock>::type
  91. { };
  92. namespace chrono
  93. {
  94. // Primary template for duration_cast impl.
  95. template<typename _ToDur, typename _CF, typename _CR,
  96. bool _NumIsOne = false, bool _DenIsOne = false>
  97. struct __duration_cast_impl
  98. {
  99. template<typename _Rep, typename _Period>
  100. static constexpr _ToDur
  101. __cast(const duration<_Rep, _Period>& __d)
  102. {
  103. typedef typename _ToDur::rep __to_rep;
  104. return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
  105. * static_cast<_CR>(_CF::num)
  106. / static_cast<_CR>(_CF::den)));
  107. }
  108. };
  109. template<typename _ToDur, typename _CF, typename _CR>
  110. struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
  111. {
  112. template<typename _Rep, typename _Period>
  113. static constexpr _ToDur
  114. __cast(const duration<_Rep, _Period>& __d)
  115. {
  116. typedef typename _ToDur::rep __to_rep;
  117. return _ToDur(static_cast<__to_rep>(__d.count()));
  118. }
  119. };
  120. template<typename _ToDur, typename _CF, typename _CR>
  121. struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
  122. {
  123. template<typename _Rep, typename _Period>
  124. static constexpr _ToDur
  125. __cast(const duration<_Rep, _Period>& __d)
  126. {
  127. typedef typename _ToDur::rep __to_rep;
  128. return _ToDur(static_cast<__to_rep>(
  129. static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
  130. }
  131. };
  132. template<typename _ToDur, typename _CF, typename _CR>
  133. struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
  134. {
  135. template<typename _Rep, typename _Period>
  136. static constexpr _ToDur
  137. __cast(const duration<_Rep, _Period>& __d)
  138. {
  139. typedef typename _ToDur::rep __to_rep;
  140. return _ToDur(static_cast<__to_rep>(
  141. static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
  142. }
  143. };
  144. template<typename _Tp>
  145. struct __is_duration
  146. : std::false_type
  147. { };
  148. template<typename _Rep, typename _Period>
  149. struct __is_duration<duration<_Rep, _Period>>
  150. : std::true_type
  151. { };
  152. template<typename _Tp>
  153. using __enable_if_is_duration
  154. = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
  155. template<typename _Tp>
  156. using __disable_if_is_duration
  157. = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
  158. /// duration_cast
  159. template<typename _ToDur, typename _Rep, typename _Period>
  160. constexpr __enable_if_is_duration<_ToDur>
  161. duration_cast(const duration<_Rep, _Period>& __d)
  162. {
  163. typedef typename _ToDur::period __to_period;
  164. typedef typename _ToDur::rep __to_rep;
  165. typedef ratio_divide<_Period, __to_period> __cf;
  166. typedef typename common_type<__to_rep, _Rep, intmax_t>::type
  167. __cr;
  168. typedef __duration_cast_impl<_ToDur, __cf, __cr,
  169. __cf::num == 1, __cf::den == 1> __dc;
  170. return __dc::__cast(__d);
  171. }
  172. /// treat_as_floating_point
  173. template<typename _Rep>
  174. struct treat_as_floating_point
  175. : is_floating_point<_Rep>
  176. { };
  177. #if __cplusplus > 201402L
  178. template <typename _Rep>
  179. inline constexpr bool treat_as_floating_point_v =
  180. treat_as_floating_point<_Rep>::value;
  181. #endif // C++17
  182. #if __cplusplus >= 201703L
  183. # define __cpp_lib_chrono 201611
  184. template<typename _ToDur, typename _Rep, typename _Period>
  185. constexpr __enable_if_is_duration<_ToDur>
  186. floor(const duration<_Rep, _Period>& __d)
  187. {
  188. auto __to = chrono::duration_cast<_ToDur>(__d);
  189. if (__to > __d)
  190. return __to - _ToDur{1};
  191. return __to;
  192. }
  193. template<typename _ToDur, typename _Rep, typename _Period>
  194. constexpr __enable_if_is_duration<_ToDur>
  195. ceil(const duration<_Rep, _Period>& __d)
  196. {
  197. auto __to = chrono::duration_cast<_ToDur>(__d);
  198. if (__to < __d)
  199. return __to + _ToDur{1};
  200. return __to;
  201. }
  202. template <typename _ToDur, typename _Rep, typename _Period>
  203. constexpr enable_if_t<
  204. __and_<__is_duration<_ToDur>,
  205. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  206. _ToDur>
  207. round(const duration<_Rep, _Period>& __d)
  208. {
  209. _ToDur __t0 = chrono::floor<_ToDur>(__d);
  210. _ToDur __t1 = __t0 + _ToDur{1};
  211. auto __diff0 = __d - __t0;
  212. auto __diff1 = __t1 - __d;
  213. if (__diff0 == __diff1)
  214. {
  215. if (__t0.count() & 1)
  216. return __t1;
  217. return __t0;
  218. }
  219. else if (__diff0 < __diff1)
  220. return __t0;
  221. return __t1;
  222. }
  223. template<typename _Rep, typename _Period>
  224. constexpr
  225. enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
  226. abs(duration<_Rep, _Period> __d)
  227. {
  228. if (__d >= __d.zero())
  229. return __d;
  230. return -__d;
  231. }
  232. #endif // C++17
  233. /// duration_values
  234. template<typename _Rep>
  235. struct duration_values
  236. {
  237. static constexpr _Rep
  238. zero() noexcept
  239. { return _Rep(0); }
  240. static constexpr _Rep
  241. max() noexcept
  242. { return numeric_limits<_Rep>::max(); }
  243. static constexpr _Rep
  244. min() noexcept
  245. { return numeric_limits<_Rep>::lowest(); }
  246. };
  247. template<typename _Tp>
  248. struct __is_ratio
  249. : std::false_type
  250. { };
  251. template<intmax_t _Num, intmax_t _Den>
  252. struct __is_ratio<ratio<_Num, _Den>>
  253. : std::true_type
  254. { };
  255. /// duration
  256. template<typename _Rep, typename _Period>
  257. struct duration
  258. {
  259. private:
  260. template<typename _Rep2>
  261. using __is_float = treat_as_floating_point<_Rep2>;
  262. // _Period2 is an exact multiple of _Period
  263. template<typename _Period2>
  264. using __is_harmonic
  265. = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
  266. public:
  267. typedef _Rep rep;
  268. typedef _Period period;
  269. static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
  270. static_assert(__is_ratio<_Period>::value,
  271. "period must be a specialization of ratio");
  272. static_assert(_Period::num > 0, "period must be positive");
  273. // 20.11.5.1 construction / copy / destroy
  274. constexpr duration() = default;
  275. duration(const duration&) = default;
  276. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  277. // 3050. Conversion specification problem in chrono::duration
  278. template<typename _Rep2, typename = _Require<
  279. is_convertible<const _Rep2&, rep>,
  280. __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
  281. constexpr explicit duration(const _Rep2& __rep)
  282. : __r(static_cast<rep>(__rep)) { }
  283. template<typename _Rep2, typename _Period2, typename = _Require<
  284. __or_<__is_float<rep>,
  285. __and_<__is_harmonic<_Period2>,
  286. __not_<__is_float<_Rep2>>>>>>
  287. constexpr duration(const duration<_Rep2, _Period2>& __d)
  288. : __r(duration_cast<duration>(__d).count()) { }
  289. ~duration() = default;
  290. duration& operator=(const duration&) = default;
  291. // 20.11.5.2 observer
  292. constexpr rep
  293. count() const
  294. { return __r; }
  295. // 20.11.5.3 arithmetic
  296. constexpr duration
  297. operator+() const
  298. { return *this; }
  299. constexpr duration
  300. operator-() const
  301. { return duration(-__r); }
  302. _GLIBCXX17_CONSTEXPR duration&
  303. operator++()
  304. {
  305. ++__r;
  306. return *this;
  307. }
  308. _GLIBCXX17_CONSTEXPR duration
  309. operator++(int)
  310. { return duration(__r++); }
  311. _GLIBCXX17_CONSTEXPR duration&
  312. operator--()
  313. {
  314. --__r;
  315. return *this;
  316. }
  317. _GLIBCXX17_CONSTEXPR duration
  318. operator--(int)
  319. { return duration(__r--); }
  320. _GLIBCXX17_CONSTEXPR duration&
  321. operator+=(const duration& __d)
  322. {
  323. __r += __d.count();
  324. return *this;
  325. }
  326. _GLIBCXX17_CONSTEXPR duration&
  327. operator-=(const duration& __d)
  328. {
  329. __r -= __d.count();
  330. return *this;
  331. }
  332. _GLIBCXX17_CONSTEXPR duration&
  333. operator*=(const rep& __rhs)
  334. {
  335. __r *= __rhs;
  336. return *this;
  337. }
  338. _GLIBCXX17_CONSTEXPR duration&
  339. operator/=(const rep& __rhs)
  340. {
  341. __r /= __rhs;
  342. return *this;
  343. }
  344. // DR 934.
  345. template<typename _Rep2 = rep>
  346. _GLIBCXX17_CONSTEXPR
  347. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  348. duration&>::type
  349. operator%=(const rep& __rhs)
  350. {
  351. __r %= __rhs;
  352. return *this;
  353. }
  354. template<typename _Rep2 = rep>
  355. _GLIBCXX17_CONSTEXPR
  356. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  357. duration&>::type
  358. operator%=(const duration& __d)
  359. {
  360. __r %= __d.count();
  361. return *this;
  362. }
  363. // 20.11.5.4 special values
  364. static constexpr duration
  365. zero() noexcept
  366. { return duration(duration_values<rep>::zero()); }
  367. static constexpr duration
  368. min() noexcept
  369. { return duration(duration_values<rep>::min()); }
  370. static constexpr duration
  371. max() noexcept
  372. { return duration(duration_values<rep>::max()); }
  373. private:
  374. rep __r;
  375. };
  376. template<typename _Rep1, typename _Period1,
  377. typename _Rep2, typename _Period2>
  378. constexpr typename common_type<duration<_Rep1, _Period1>,
  379. duration<_Rep2, _Period2>>::type
  380. operator+(const duration<_Rep1, _Period1>& __lhs,
  381. const duration<_Rep2, _Period2>& __rhs)
  382. {
  383. typedef duration<_Rep1, _Period1> __dur1;
  384. typedef duration<_Rep2, _Period2> __dur2;
  385. typedef typename common_type<__dur1,__dur2>::type __cd;
  386. return __cd(__cd(__lhs).count() + __cd(__rhs).count());
  387. }
  388. template<typename _Rep1, typename _Period1,
  389. typename _Rep2, typename _Period2>
  390. constexpr typename common_type<duration<_Rep1, _Period1>,
  391. duration<_Rep2, _Period2>>::type
  392. operator-(const duration<_Rep1, _Period1>& __lhs,
  393. const duration<_Rep2, _Period2>& __rhs)
  394. {
  395. typedef duration<_Rep1, _Period1> __dur1;
  396. typedef duration<_Rep2, _Period2> __dur2;
  397. typedef typename common_type<__dur1,__dur2>::type __cd;
  398. return __cd(__cd(__lhs).count() - __cd(__rhs).count());
  399. }
  400. // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
  401. // is implicitly convertible to it.
  402. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  403. // 3050. Conversion specification problem in chrono::duration constructor
  404. template<typename _Rep1, typename _Rep2,
  405. typename _CRep = typename common_type<_Rep1, _Rep2>::type>
  406. using __common_rep_t = typename
  407. enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
  408. template<typename _Rep1, typename _Period, typename _Rep2>
  409. constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
  410. operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  411. {
  412. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  413. __cd;
  414. return __cd(__cd(__d).count() * __s);
  415. }
  416. template<typename _Rep1, typename _Rep2, typename _Period>
  417. constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
  418. operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
  419. { return __d * __s; }
  420. template<typename _Rep1, typename _Period, typename _Rep2>
  421. constexpr
  422. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  423. operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  424. {
  425. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  426. __cd;
  427. return __cd(__cd(__d).count() / __s);
  428. }
  429. template<typename _Rep1, typename _Period1,
  430. typename _Rep2, typename _Period2>
  431. constexpr typename common_type<_Rep1, _Rep2>::type
  432. operator/(const duration<_Rep1, _Period1>& __lhs,
  433. const duration<_Rep2, _Period2>& __rhs)
  434. {
  435. typedef duration<_Rep1, _Period1> __dur1;
  436. typedef duration<_Rep2, _Period2> __dur2;
  437. typedef typename common_type<__dur1,__dur2>::type __cd;
  438. return __cd(__lhs).count() / __cd(__rhs).count();
  439. }
  440. // DR 934.
  441. template<typename _Rep1, typename _Period, typename _Rep2>
  442. constexpr
  443. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  444. operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  445. {
  446. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  447. __cd;
  448. return __cd(__cd(__d).count() % __s);
  449. }
  450. template<typename _Rep1, typename _Period1,
  451. typename _Rep2, typename _Period2>
  452. constexpr typename common_type<duration<_Rep1, _Period1>,
  453. duration<_Rep2, _Period2>>::type
  454. operator%(const duration<_Rep1, _Period1>& __lhs,
  455. const duration<_Rep2, _Period2>& __rhs)
  456. {
  457. typedef duration<_Rep1, _Period1> __dur1;
  458. typedef duration<_Rep2, _Period2> __dur2;
  459. typedef typename common_type<__dur1,__dur2>::type __cd;
  460. return __cd(__cd(__lhs).count() % __cd(__rhs).count());
  461. }
  462. // comparisons
  463. template<typename _Rep1, typename _Period1,
  464. typename _Rep2, typename _Period2>
  465. constexpr bool
  466. operator==(const duration<_Rep1, _Period1>& __lhs,
  467. const duration<_Rep2, _Period2>& __rhs)
  468. {
  469. typedef duration<_Rep1, _Period1> __dur1;
  470. typedef duration<_Rep2, _Period2> __dur2;
  471. typedef typename common_type<__dur1,__dur2>::type __ct;
  472. return __ct(__lhs).count() == __ct(__rhs).count();
  473. }
  474. template<typename _Rep1, typename _Period1,
  475. typename _Rep2, typename _Period2>
  476. constexpr bool
  477. operator<(const duration<_Rep1, _Period1>& __lhs,
  478. const duration<_Rep2, _Period2>& __rhs)
  479. {
  480. typedef duration<_Rep1, _Period1> __dur1;
  481. typedef duration<_Rep2, _Period2> __dur2;
  482. typedef typename common_type<__dur1,__dur2>::type __ct;
  483. return __ct(__lhs).count() < __ct(__rhs).count();
  484. }
  485. template<typename _Rep1, typename _Period1,
  486. typename _Rep2, typename _Period2>
  487. constexpr bool
  488. operator!=(const duration<_Rep1, _Period1>& __lhs,
  489. const duration<_Rep2, _Period2>& __rhs)
  490. { return !(__lhs == __rhs); }
  491. template<typename _Rep1, typename _Period1,
  492. typename _Rep2, typename _Period2>
  493. constexpr bool
  494. operator<=(const duration<_Rep1, _Period1>& __lhs,
  495. const duration<_Rep2, _Period2>& __rhs)
  496. { return !(__rhs < __lhs); }
  497. template<typename _Rep1, typename _Period1,
  498. typename _Rep2, typename _Period2>
  499. constexpr bool
  500. operator>(const duration<_Rep1, _Period1>& __lhs,
  501. const duration<_Rep2, _Period2>& __rhs)
  502. { return __rhs < __lhs; }
  503. template<typename _Rep1, typename _Period1,
  504. typename _Rep2, typename _Period2>
  505. constexpr bool
  506. operator>=(const duration<_Rep1, _Period1>& __lhs,
  507. const duration<_Rep2, _Period2>& __rhs)
  508. { return !(__lhs < __rhs); }
  509. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  510. # define _GLIBCXX_CHRONO_INT64_T int64_t
  511. #elif defined __INT64_TYPE__
  512. # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
  513. #else
  514. static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
  515. "Representation type for nanoseconds must have at least 64 bits");
  516. # define _GLIBCXX_CHRONO_INT64_T long long
  517. #endif
  518. /// nanoseconds
  519. typedef duration<_GLIBCXX_CHRONO_INT64_T, nano> nanoseconds;
  520. /// microseconds
  521. typedef duration<_GLIBCXX_CHRONO_INT64_T, micro> microseconds;
  522. /// milliseconds
  523. typedef duration<_GLIBCXX_CHRONO_INT64_T, milli> milliseconds;
  524. /// seconds
  525. typedef duration<_GLIBCXX_CHRONO_INT64_T> seconds;
  526. /// minutes
  527. typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>> minutes;
  528. /// hours
  529. typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>> hours;
  530. #undef _GLIBCXX_CHRONO_INT64_T
  531. /// time_point
  532. template<typename _Clock, typename _Dur>
  533. struct time_point
  534. {
  535. typedef _Clock clock;
  536. typedef _Dur duration;
  537. typedef typename duration::rep rep;
  538. typedef typename duration::period period;
  539. constexpr time_point() : __d(duration::zero())
  540. { }
  541. constexpr explicit time_point(const duration& __dur)
  542. : __d(__dur)
  543. { }
  544. // conversions
  545. template<typename _Dur2,
  546. typename = _Require<is_convertible<_Dur2, _Dur>>>
  547. constexpr time_point(const time_point<clock, _Dur2>& __t)
  548. : __d(__t.time_since_epoch())
  549. { }
  550. // observer
  551. constexpr duration
  552. time_since_epoch() const
  553. { return __d; }
  554. // arithmetic
  555. _GLIBCXX17_CONSTEXPR time_point&
  556. operator+=(const duration& __dur)
  557. {
  558. __d += __dur;
  559. return *this;
  560. }
  561. _GLIBCXX17_CONSTEXPR time_point&
  562. operator-=(const duration& __dur)
  563. {
  564. __d -= __dur;
  565. return *this;
  566. }
  567. // special values
  568. static constexpr time_point
  569. min() noexcept
  570. { return time_point(duration::min()); }
  571. static constexpr time_point
  572. max() noexcept
  573. { return time_point(duration::max()); }
  574. private:
  575. duration __d;
  576. };
  577. /// time_point_cast
  578. template<typename _ToDur, typename _Clock, typename _Dur>
  579. constexpr typename enable_if<__is_duration<_ToDur>::value,
  580. time_point<_Clock, _ToDur>>::type
  581. time_point_cast(const time_point<_Clock, _Dur>& __t)
  582. {
  583. typedef time_point<_Clock, _ToDur> __time_point;
  584. return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
  585. }
  586. #if __cplusplus > 201402L
  587. template<typename _ToDur, typename _Clock, typename _Dur>
  588. constexpr
  589. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  590. floor(const time_point<_Clock, _Dur>& __tp)
  591. {
  592. return time_point<_Clock, _ToDur>{
  593. chrono::floor<_ToDur>(__tp.time_since_epoch())};
  594. }
  595. template<typename _ToDur, typename _Clock, typename _Dur>
  596. constexpr
  597. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  598. ceil(const time_point<_Clock, _Dur>& __tp)
  599. {
  600. return time_point<_Clock, _ToDur>{
  601. chrono::ceil<_ToDur>(__tp.time_since_epoch())};
  602. }
  603. template<typename _ToDur, typename _Clock, typename _Dur>
  604. constexpr enable_if_t<
  605. __and_<__is_duration<_ToDur>,
  606. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  607. time_point<_Clock, _ToDur>>
  608. round(const time_point<_Clock, _Dur>& __tp)
  609. {
  610. return time_point<_Clock, _ToDur>{
  611. chrono::round<_ToDur>(__tp.time_since_epoch())};
  612. }
  613. #endif // C++17
  614. template<typename _Clock, typename _Dur1,
  615. typename _Rep2, typename _Period2>
  616. constexpr time_point<_Clock,
  617. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  618. operator+(const time_point<_Clock, _Dur1>& __lhs,
  619. const duration<_Rep2, _Period2>& __rhs)
  620. {
  621. typedef duration<_Rep2, _Period2> __dur2;
  622. typedef typename common_type<_Dur1,__dur2>::type __ct;
  623. typedef time_point<_Clock, __ct> __time_point;
  624. return __time_point(__lhs.time_since_epoch() + __rhs);
  625. }
  626. template<typename _Rep1, typename _Period1,
  627. typename _Clock, typename _Dur2>
  628. constexpr time_point<_Clock,
  629. typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
  630. operator+(const duration<_Rep1, _Period1>& __lhs,
  631. const time_point<_Clock, _Dur2>& __rhs)
  632. {
  633. typedef duration<_Rep1, _Period1> __dur1;
  634. typedef typename common_type<__dur1,_Dur2>::type __ct;
  635. typedef time_point<_Clock, __ct> __time_point;
  636. return __time_point(__rhs.time_since_epoch() + __lhs);
  637. }
  638. template<typename _Clock, typename _Dur1,
  639. typename _Rep2, typename _Period2>
  640. constexpr time_point<_Clock,
  641. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  642. operator-(const time_point<_Clock, _Dur1>& __lhs,
  643. const duration<_Rep2, _Period2>& __rhs)
  644. {
  645. typedef duration<_Rep2, _Period2> __dur2;
  646. typedef typename common_type<_Dur1,__dur2>::type __ct;
  647. typedef time_point<_Clock, __ct> __time_point;
  648. return __time_point(__lhs.time_since_epoch() -__rhs);
  649. }
  650. template<typename _Clock, typename _Dur1, typename _Dur2>
  651. constexpr typename common_type<_Dur1, _Dur2>::type
  652. operator-(const time_point<_Clock, _Dur1>& __lhs,
  653. const time_point<_Clock, _Dur2>& __rhs)
  654. { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
  655. template<typename _Clock, typename _Dur1, typename _Dur2>
  656. constexpr bool
  657. operator==(const time_point<_Clock, _Dur1>& __lhs,
  658. const time_point<_Clock, _Dur2>& __rhs)
  659. { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
  660. template<typename _Clock, typename _Dur1, typename _Dur2>
  661. constexpr bool
  662. operator!=(const time_point<_Clock, _Dur1>& __lhs,
  663. const time_point<_Clock, _Dur2>& __rhs)
  664. { return !(__lhs == __rhs); }
  665. template<typename _Clock, typename _Dur1, typename _Dur2>
  666. constexpr bool
  667. operator<(const time_point<_Clock, _Dur1>& __lhs,
  668. const time_point<_Clock, _Dur2>& __rhs)
  669. { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
  670. template<typename _Clock, typename _Dur1, typename _Dur2>
  671. constexpr bool
  672. operator<=(const time_point<_Clock, _Dur1>& __lhs,
  673. const time_point<_Clock, _Dur2>& __rhs)
  674. { return !(__rhs < __lhs); }
  675. template<typename _Clock, typename _Dur1, typename _Dur2>
  676. constexpr bool
  677. operator>(const time_point<_Clock, _Dur1>& __lhs,
  678. const time_point<_Clock, _Dur2>& __rhs)
  679. { return __rhs < __lhs; }
  680. template<typename _Clock, typename _Dur1, typename _Dur2>
  681. constexpr bool
  682. operator>=(const time_point<_Clock, _Dur1>& __lhs,
  683. const time_point<_Clock, _Dur2>& __rhs)
  684. { return !(__lhs < __rhs); }
  685. // Clocks.
  686. // Why nanosecond resolution as the default?
  687. // Why have std::system_clock always count in the highest
  688. // resolution (ie nanoseconds), even if on some OSes the low 3
  689. // or 9 decimal digits will be always zero? This allows later
  690. // implementations to change the system_clock::now()
  691. // implementation any time to provide better resolution without
  692. // changing function signature or units.
  693. // To support the (forward) evolution of the library's defined
  694. // clocks, wrap inside inline namespace so that the current
  695. // defintions of system_clock, steady_clock, and
  696. // high_resolution_clock types are uniquely mangled. This way, new
  697. // code can use the latests clocks, while the library can contain
  698. // compatibility definitions for previous versions. At some
  699. // point, when these clocks settle down, the inlined namespaces
  700. // can be removed. XXX GLIBCXX_ABI Deprecated
  701. inline namespace _V2 {
  702. /**
  703. * @brief System clock.
  704. *
  705. * Time returned represents wall time from the system-wide clock.
  706. */
  707. struct system_clock
  708. {
  709. typedef chrono::nanoseconds duration;
  710. typedef duration::rep rep;
  711. typedef duration::period period;
  712. typedef chrono::time_point<system_clock, duration> time_point;
  713. static_assert(system_clock::duration::min()
  714. < system_clock::duration::zero(),
  715. "a clock's minimum duration cannot be less than its epoch");
  716. static constexpr bool is_steady = false;
  717. static time_point
  718. now() noexcept;
  719. // Map to C API
  720. static std::time_t
  721. to_time_t(const time_point& __t) noexcept
  722. {
  723. return std::time_t(duration_cast<chrono::seconds>
  724. (__t.time_since_epoch()).count());
  725. }
  726. static time_point
  727. from_time_t(std::time_t __t) noexcept
  728. {
  729. typedef chrono::time_point<system_clock, seconds> __from;
  730. return time_point_cast<system_clock::duration>
  731. (__from(chrono::seconds(__t)));
  732. }
  733. };
  734. /**
  735. * @brief Monotonic clock
  736. *
  737. * Time returned has the property of only increasing at a uniform rate.
  738. */
  739. struct steady_clock
  740. {
  741. typedef chrono::nanoseconds duration;
  742. typedef duration::rep rep;
  743. typedef duration::period period;
  744. typedef chrono::time_point<steady_clock, duration> time_point;
  745. static constexpr bool is_steady = true;
  746. static time_point
  747. now() noexcept;
  748. };
  749. /**
  750. * @brief Highest-resolution clock
  751. *
  752. * This is the clock "with the shortest tick period." Alias to
  753. * std::system_clock until higher-than-nanosecond definitions
  754. * become feasible.
  755. */
  756. using high_resolution_clock = system_clock;
  757. } // end inline namespace _V2
  758. } // namespace chrono
  759. #if __cplusplus > 201103L
  760. #define __cpp_lib_chrono_udls 201304
  761. inline namespace literals
  762. {
  763. inline namespace chrono_literals
  764. {
  765. #pragma GCC diagnostic push
  766. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  767. template<typename _Dur, char... _Digits>
  768. constexpr _Dur __check_overflow()
  769. {
  770. using _Val = __parse_int::_Parse_int<_Digits...>;
  771. constexpr typename _Dur::rep __repval = _Val::value;
  772. static_assert(__repval >= 0 && __repval == _Val::value,
  773. "literal value cannot be represented by duration type");
  774. return _Dur(__repval);
  775. }
  776. constexpr chrono::duration<long double, ratio<3600,1>>
  777. operator""h(long double __hours)
  778. { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
  779. template <char... _Digits>
  780. constexpr chrono::hours
  781. operator""h()
  782. { return __check_overflow<chrono::hours, _Digits...>(); }
  783. constexpr chrono::duration<long double, ratio<60,1>>
  784. operator""min(long double __mins)
  785. { return chrono::duration<long double, ratio<60,1>>{__mins}; }
  786. template <char... _Digits>
  787. constexpr chrono::minutes
  788. operator""min()
  789. { return __check_overflow<chrono::minutes, _Digits...>(); }
  790. constexpr chrono::duration<long double>
  791. operator""s(long double __secs)
  792. { return chrono::duration<long double>{__secs}; }
  793. template <char... _Digits>
  794. constexpr chrono::seconds
  795. operator""s()
  796. { return __check_overflow<chrono::seconds, _Digits...>(); }
  797. constexpr chrono::duration<long double, milli>
  798. operator""ms(long double __msecs)
  799. { return chrono::duration<long double, milli>{__msecs}; }
  800. template <char... _Digits>
  801. constexpr chrono::milliseconds
  802. operator""ms()
  803. { return __check_overflow<chrono::milliseconds, _Digits...>(); }
  804. constexpr chrono::duration<long double, micro>
  805. operator""us(long double __usecs)
  806. { return chrono::duration<long double, micro>{__usecs}; }
  807. template <char... _Digits>
  808. constexpr chrono::microseconds
  809. operator""us()
  810. { return __check_overflow<chrono::microseconds, _Digits...>(); }
  811. constexpr chrono::duration<long double, nano>
  812. operator""ns(long double __nsecs)
  813. { return chrono::duration<long double, nano>{__nsecs}; }
  814. template <char... _Digits>
  815. constexpr chrono::nanoseconds
  816. operator""ns()
  817. { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
  818. #pragma GCC diagnostic pop
  819. } // inline namespace chrono_literals
  820. } // inline namespace literals
  821. namespace chrono
  822. {
  823. using namespace literals::chrono_literals;
  824. } // namespace chrono
  825. #endif // C++14
  826. // @} group chrono
  827. _GLIBCXX_END_NAMESPACE_VERSION
  828. } // namespace std
  829. #endif // C++11
  830. #endif //_GLIBCXX_CHRONO