chrono 29 KB

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