chrono 26 KB

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