You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1272 lines
38KB

  1. // <chrono> -*- C++ -*-
  2. // Copyright (C) 2008-2020 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. * @ingroup chrono
  23. */
  24. #ifndef _GLIBCXX_CHRONO
  25. #define _GLIBCXX_CHRONO 1
  26. #pragma GCC system_header
  27. #if __cplusplus < 201103L
  28. # include <bits/c++0x_warning.h>
  29. #else
  30. #include <ratio>
  31. #include <type_traits>
  32. #include <limits>
  33. #include <ctime>
  34. #include <bits/parse_numbers.h> // for literals support.
  35. #if __cplusplus > 201703L
  36. # include <concepts>
  37. # include <compare>
  38. #endif
  39. namespace std _GLIBCXX_VISIBILITY(default)
  40. {
  41. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  42. #if __cplusplus >= 201703L
  43. namespace filesystem { struct __file_clock; };
  44. #endif
  45. /**
  46. * @defgroup chrono Time
  47. * @ingroup utilities
  48. *
  49. * Classes and functions for time.
  50. * @{
  51. */
  52. /** @namespace std::chrono
  53. * @brief ISO C++ 2011 namespace for date and time utilities
  54. */
  55. namespace chrono
  56. {
  57. template<typename _Rep, typename _Period = ratio<1>>
  58. struct duration;
  59. template<typename _Clock, typename _Dur = typename _Clock::duration>
  60. struct time_point;
  61. }
  62. // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
  63. /// @cond undocumented
  64. template<typename _CT, typename _Period1, typename _Period2, typename = void>
  65. struct __duration_common_type
  66. { };
  67. template<typename _CT, typename _Period1, typename _Period2>
  68. struct __duration_common_type<_CT, _Period1, _Period2,
  69. __void_t<typename _CT::type>>
  70. {
  71. private:
  72. using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
  73. using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
  74. using __cr = typename _CT::type;
  75. using __r = ratio<__gcd_num::value,
  76. (_Period1::den / __gcd_den::value) * _Period2::den>;
  77. public:
  78. using type = chrono::duration<__cr, __r>;
  79. };
  80. template<typename _Period1, typename _Period2>
  81. struct __duration_common_type<__failure_type, _Period1, _Period2>
  82. { typedef __failure_type type; };
  83. /// @endcond
  84. /// Specialization of common_type for chrono::duration types.
  85. /// @relates duration
  86. template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
  87. struct common_type<chrono::duration<_Rep1, _Period1>,
  88. chrono::duration<_Rep2, _Period2>>
  89. : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
  90. { };
  91. // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
  92. /// @cond undocumented
  93. template<typename _CT, typename _Clock, typename = void>
  94. struct __timepoint_common_type
  95. { };
  96. template<typename _CT, typename _Clock>
  97. struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
  98. {
  99. using type = chrono::time_point<_Clock, typename _CT::type>;
  100. };
  101. /// @endcond
  102. /// Specialization of common_type for chrono::time_point types.
  103. /// @relates time_point
  104. template<typename _Clock, typename _Duration1, typename _Duration2>
  105. struct common_type<chrono::time_point<_Clock, _Duration1>,
  106. chrono::time_point<_Clock, _Duration2>>
  107. : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
  108. { };
  109. // @} group chrono
  110. namespace chrono
  111. {
  112. /// @addtogroup chrono
  113. /// @{
  114. /// @cond undocumented
  115. // Primary template for duration_cast impl.
  116. template<typename _ToDur, typename _CF, typename _CR,
  117. bool _NumIsOne = false, bool _DenIsOne = false>
  118. struct __duration_cast_impl
  119. {
  120. template<typename _Rep, typename _Period>
  121. static constexpr _ToDur
  122. __cast(const duration<_Rep, _Period>& __d)
  123. {
  124. typedef typename _ToDur::rep __to_rep;
  125. return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
  126. * static_cast<_CR>(_CF::num)
  127. / static_cast<_CR>(_CF::den)));
  128. }
  129. };
  130. template<typename _ToDur, typename _CF, typename _CR>
  131. struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
  132. {
  133. template<typename _Rep, typename _Period>
  134. static constexpr _ToDur
  135. __cast(const duration<_Rep, _Period>& __d)
  136. {
  137. typedef typename _ToDur::rep __to_rep;
  138. return _ToDur(static_cast<__to_rep>(__d.count()));
  139. }
  140. };
  141. template<typename _ToDur, typename _CF, typename _CR>
  142. struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
  143. {
  144. template<typename _Rep, typename _Period>
  145. static constexpr _ToDur
  146. __cast(const duration<_Rep, _Period>& __d)
  147. {
  148. typedef typename _ToDur::rep __to_rep;
  149. return _ToDur(static_cast<__to_rep>(
  150. static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
  151. }
  152. };
  153. template<typename _ToDur, typename _CF, typename _CR>
  154. struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
  155. {
  156. template<typename _Rep, typename _Period>
  157. static constexpr _ToDur
  158. __cast(const duration<_Rep, _Period>& __d)
  159. {
  160. typedef typename _ToDur::rep __to_rep;
  161. return _ToDur(static_cast<__to_rep>(
  162. static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
  163. }
  164. };
  165. template<typename _Tp>
  166. struct __is_duration
  167. : std::false_type
  168. { };
  169. template<typename _Rep, typename _Period>
  170. struct __is_duration<duration<_Rep, _Period>>
  171. : std::true_type
  172. { };
  173. template<typename _Tp>
  174. using __enable_if_is_duration
  175. = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
  176. template<typename _Tp>
  177. using __disable_if_is_duration
  178. = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
  179. /// @endcond
  180. /// duration_cast
  181. template<typename _ToDur, typename _Rep, typename _Period>
  182. constexpr __enable_if_is_duration<_ToDur>
  183. duration_cast(const duration<_Rep, _Period>& __d)
  184. {
  185. typedef typename _ToDur::period __to_period;
  186. typedef typename _ToDur::rep __to_rep;
  187. typedef ratio_divide<_Period, __to_period> __cf;
  188. typedef typename common_type<__to_rep, _Rep, intmax_t>::type
  189. __cr;
  190. typedef __duration_cast_impl<_ToDur, __cf, __cr,
  191. __cf::num == 1, __cf::den == 1> __dc;
  192. return __dc::__cast(__d);
  193. }
  194. /// treat_as_floating_point
  195. template<typename _Rep>
  196. struct treat_as_floating_point
  197. : is_floating_point<_Rep>
  198. { };
  199. #if __cplusplus > 201402L
  200. template <typename _Rep>
  201. inline constexpr bool treat_as_floating_point_v =
  202. treat_as_floating_point<_Rep>::value;
  203. #endif // C++17
  204. #if __cplusplus > 201703L
  205. template<typename _Tp>
  206. struct is_clock;
  207. template<typename _Tp>
  208. inline constexpr bool is_clock_v = is_clock<_Tp>::value;
  209. #if __cpp_lib_concepts
  210. template<typename _Tp>
  211. struct is_clock : false_type
  212. { };
  213. template<typename _Tp>
  214. requires requires {
  215. typename _Tp::rep;
  216. typename _Tp::period;
  217. typename _Tp::duration;
  218. typename _Tp::time_point::clock;
  219. typename _Tp::time_point::duration;
  220. { &_Tp::is_steady } -> same_as<const bool*>;
  221. { _Tp::now() } -> same_as<typename _Tp::time_point>;
  222. requires same_as<typename _Tp::duration,
  223. duration<typename _Tp::rep, typename _Tp::period>>;
  224. requires same_as<typename _Tp::time_point::duration,
  225. typename _Tp::duration>;
  226. }
  227. struct is_clock<_Tp> : true_type
  228. { };
  229. #else
  230. template<typename _Tp, typename = void>
  231. struct __is_clock_impl : false_type
  232. { };
  233. template<typename _Tp>
  234. struct __is_clock_impl<_Tp,
  235. void_t<typename _Tp::rep, typename _Tp::period,
  236. typename _Tp::duration,
  237. typename _Tp::time_point::duration,
  238. decltype(_Tp::is_steady),
  239. decltype(_Tp::now())>>
  240. : __and_<is_same<typename _Tp::duration,
  241. duration<typename _Tp::rep, typename _Tp::period>>,
  242. is_same<typename _Tp::time_point::duration,
  243. typename _Tp::duration>,
  244. is_same<decltype(&_Tp::is_steady), const bool*>,
  245. is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
  246. { };
  247. template<typename _Tp>
  248. struct is_clock : __is_clock_impl<_Tp>::type
  249. { };
  250. #endif
  251. #endif // C++20
  252. #if __cplusplus >= 201703L
  253. # define __cpp_lib_chrono 201611
  254. template<typename _ToDur, typename _Rep, typename _Period>
  255. constexpr __enable_if_is_duration<_ToDur>
  256. floor(const duration<_Rep, _Period>& __d)
  257. {
  258. auto __to = chrono::duration_cast<_ToDur>(__d);
  259. if (__to > __d)
  260. return __to - _ToDur{1};
  261. return __to;
  262. }
  263. template<typename _ToDur, typename _Rep, typename _Period>
  264. constexpr __enable_if_is_duration<_ToDur>
  265. ceil(const duration<_Rep, _Period>& __d)
  266. {
  267. auto __to = chrono::duration_cast<_ToDur>(__d);
  268. if (__to < __d)
  269. return __to + _ToDur{1};
  270. return __to;
  271. }
  272. template <typename _ToDur, typename _Rep, typename _Period>
  273. constexpr enable_if_t<
  274. __and_<__is_duration<_ToDur>,
  275. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  276. _ToDur>
  277. round(const duration<_Rep, _Period>& __d)
  278. {
  279. _ToDur __t0 = chrono::floor<_ToDur>(__d);
  280. _ToDur __t1 = __t0 + _ToDur{1};
  281. auto __diff0 = __d - __t0;
  282. auto __diff1 = __t1 - __d;
  283. if (__diff0 == __diff1)
  284. {
  285. if (__t0.count() & 1)
  286. return __t1;
  287. return __t0;
  288. }
  289. else if (__diff0 < __diff1)
  290. return __t0;
  291. return __t1;
  292. }
  293. template<typename _Rep, typename _Period>
  294. constexpr
  295. enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
  296. abs(duration<_Rep, _Period> __d)
  297. {
  298. if (__d >= __d.zero())
  299. return __d;
  300. return -__d;
  301. }
  302. #endif // C++17
  303. /// duration_values
  304. template<typename _Rep>
  305. struct duration_values
  306. {
  307. static constexpr _Rep
  308. zero() noexcept
  309. { return _Rep(0); }
  310. static constexpr _Rep
  311. max() noexcept
  312. { return numeric_limits<_Rep>::max(); }
  313. static constexpr _Rep
  314. min() noexcept
  315. { return numeric_limits<_Rep>::lowest(); }
  316. };
  317. /// @cond undocumented
  318. template<typename _Tp>
  319. struct __is_ratio
  320. : std::false_type
  321. { };
  322. template<intmax_t _Num, intmax_t _Den>
  323. struct __is_ratio<ratio<_Num, _Den>>
  324. : std::true_type
  325. { };
  326. /// @endcond
  327. /// duration
  328. template<typename _Rep, typename _Period>
  329. struct duration
  330. {
  331. private:
  332. template<typename _Rep2>
  333. using __is_float = treat_as_floating_point<_Rep2>;
  334. // _Period2 is an exact multiple of _Period
  335. template<typename _Period2>
  336. using __is_harmonic
  337. = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
  338. public:
  339. typedef _Rep rep;
  340. typedef _Period period;
  341. static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
  342. static_assert(__is_ratio<_Period>::value,
  343. "period must be a specialization of ratio");
  344. static_assert(_Period::num > 0, "period must be positive");
  345. // 20.11.5.1 construction / copy / destroy
  346. constexpr duration() = default;
  347. duration(const duration&) = default;
  348. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  349. // 3050. Conversion specification problem in chrono::duration
  350. template<typename _Rep2, typename = _Require<
  351. is_convertible<const _Rep2&, rep>,
  352. __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
  353. constexpr explicit duration(const _Rep2& __rep)
  354. : __r(static_cast<rep>(__rep)) { }
  355. template<typename _Rep2, typename _Period2, typename = _Require<
  356. __or_<__is_float<rep>,
  357. __and_<__is_harmonic<_Period2>,
  358. __not_<__is_float<_Rep2>>>>>>
  359. constexpr duration(const duration<_Rep2, _Period2>& __d)
  360. : __r(duration_cast<duration>(__d).count()) { }
  361. ~duration() = default;
  362. duration& operator=(const duration&) = default;
  363. // 20.11.5.2 observer
  364. constexpr rep
  365. count() const
  366. { return __r; }
  367. // 20.11.5.3 arithmetic
  368. constexpr duration
  369. operator+() const
  370. { return *this; }
  371. constexpr duration
  372. operator-() const
  373. { return duration(-__r); }
  374. _GLIBCXX17_CONSTEXPR duration&
  375. operator++()
  376. {
  377. ++__r;
  378. return *this;
  379. }
  380. _GLIBCXX17_CONSTEXPR duration
  381. operator++(int)
  382. { return duration(__r++); }
  383. _GLIBCXX17_CONSTEXPR duration&
  384. operator--()
  385. {
  386. --__r;
  387. return *this;
  388. }
  389. _GLIBCXX17_CONSTEXPR duration
  390. operator--(int)
  391. { return duration(__r--); }
  392. _GLIBCXX17_CONSTEXPR duration&
  393. operator+=(const duration& __d)
  394. {
  395. __r += __d.count();
  396. return *this;
  397. }
  398. _GLIBCXX17_CONSTEXPR duration&
  399. operator-=(const duration& __d)
  400. {
  401. __r -= __d.count();
  402. return *this;
  403. }
  404. _GLIBCXX17_CONSTEXPR duration&
  405. operator*=(const rep& __rhs)
  406. {
  407. __r *= __rhs;
  408. return *this;
  409. }
  410. _GLIBCXX17_CONSTEXPR duration&
  411. operator/=(const rep& __rhs)
  412. {
  413. __r /= __rhs;
  414. return *this;
  415. }
  416. // DR 934.
  417. template<typename _Rep2 = rep>
  418. _GLIBCXX17_CONSTEXPR
  419. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  420. duration&>::type
  421. operator%=(const rep& __rhs)
  422. {
  423. __r %= __rhs;
  424. return *this;
  425. }
  426. template<typename _Rep2 = rep>
  427. _GLIBCXX17_CONSTEXPR
  428. typename enable_if<!treat_as_floating_point<_Rep2>::value,
  429. duration&>::type
  430. operator%=(const duration& __d)
  431. {
  432. __r %= __d.count();
  433. return *this;
  434. }
  435. // 20.11.5.4 special values
  436. static constexpr duration
  437. zero() noexcept
  438. { return duration(duration_values<rep>::zero()); }
  439. static constexpr duration
  440. min() noexcept
  441. { return duration(duration_values<rep>::min()); }
  442. static constexpr duration
  443. max() noexcept
  444. { return duration(duration_values<rep>::max()); }
  445. private:
  446. rep __r;
  447. };
  448. /// @relates duration @{
  449. /// The sum of two durations.
  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. /// The difference between two durations.
  463. template<typename _Rep1, typename _Period1,
  464. typename _Rep2, typename _Period2>
  465. constexpr typename common_type<duration<_Rep1, _Period1>,
  466. duration<_Rep2, _Period2>>::type
  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 __cd;
  473. return __cd(__cd(__lhs).count() - __cd(__rhs).count());
  474. }
  475. /// @}
  476. /// @cond undocumented
  477. // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
  478. // is implicitly convertible to it.
  479. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  480. // 3050. Conversion specification problem in chrono::duration constructor
  481. template<typename _Rep1, typename _Rep2,
  482. typename _CRep = typename common_type<_Rep1, _Rep2>::type>
  483. using __common_rep_t = typename
  484. enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
  485. /// @endcond
  486. /// @relates duration @{
  487. /// Multiply a duration by a scalar value.
  488. template<typename _Rep1, typename _Period, typename _Rep2>
  489. constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
  490. operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  491. {
  492. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  493. __cd;
  494. return __cd(__cd(__d).count() * __s);
  495. }
  496. /// Multiply a duration by a scalar value.
  497. template<typename _Rep1, typename _Rep2, typename _Period>
  498. constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
  499. operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
  500. { return __d * __s; }
  501. template<typename _Rep1, typename _Period, typename _Rep2>
  502. constexpr
  503. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  504. operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  505. {
  506. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  507. __cd;
  508. return __cd(__cd(__d).count() / __s);
  509. }
  510. template<typename _Rep1, typename _Period1,
  511. typename _Rep2, typename _Period2>
  512. constexpr typename common_type<_Rep1, _Rep2>::type
  513. operator/(const duration<_Rep1, _Period1>& __lhs,
  514. const duration<_Rep2, _Period2>& __rhs)
  515. {
  516. typedef duration<_Rep1, _Period1> __dur1;
  517. typedef duration<_Rep2, _Period2> __dur2;
  518. typedef typename common_type<__dur1,__dur2>::type __cd;
  519. return __cd(__lhs).count() / __cd(__rhs).count();
  520. }
  521. // DR 934.
  522. template<typename _Rep1, typename _Period, typename _Rep2>
  523. constexpr
  524. duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
  525. operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
  526. {
  527. typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
  528. __cd;
  529. return __cd(__cd(__d).count() % __s);
  530. }
  531. template<typename _Rep1, typename _Period1,
  532. typename _Rep2, typename _Period2>
  533. constexpr typename common_type<duration<_Rep1, _Period1>,
  534. duration<_Rep2, _Period2>>::type
  535. operator%(const duration<_Rep1, _Period1>& __lhs,
  536. const duration<_Rep2, _Period2>& __rhs)
  537. {
  538. typedef duration<_Rep1, _Period1> __dur1;
  539. typedef duration<_Rep2, _Period2> __dur2;
  540. typedef typename common_type<__dur1,__dur2>::type __cd;
  541. return __cd(__cd(__lhs).count() % __cd(__rhs).count());
  542. }
  543. // comparisons
  544. template<typename _Rep1, typename _Period1,
  545. typename _Rep2, typename _Period2>
  546. constexpr bool
  547. operator==(const duration<_Rep1, _Period1>& __lhs,
  548. const duration<_Rep2, _Period2>& __rhs)
  549. {
  550. typedef duration<_Rep1, _Period1> __dur1;
  551. typedef duration<_Rep2, _Period2> __dur2;
  552. typedef typename common_type<__dur1,__dur2>::type __ct;
  553. return __ct(__lhs).count() == __ct(__rhs).count();
  554. }
  555. template<typename _Rep1, typename _Period1,
  556. typename _Rep2, typename _Period2>
  557. constexpr bool
  558. operator<(const duration<_Rep1, _Period1>& __lhs,
  559. const duration<_Rep2, _Period2>& __rhs)
  560. {
  561. typedef duration<_Rep1, _Period1> __dur1;
  562. typedef duration<_Rep2, _Period2> __dur2;
  563. typedef typename common_type<__dur1,__dur2>::type __ct;
  564. return __ct(__lhs).count() < __ct(__rhs).count();
  565. }
  566. #if __cpp_lib_three_way_comparison
  567. template<typename _Rep1, typename _Period1,
  568. typename _Rep2, typename _Period2>
  569. requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
  570. constexpr auto
  571. operator<=>(const duration<_Rep1, _Period1>& __lhs,
  572. const duration<_Rep2, _Period2>& __rhs)
  573. {
  574. using __ct = common_type_t<duration<_Rep1, _Period1>,
  575. duration<_Rep2, _Period2>>;
  576. return __ct(__lhs).count() <=> __ct(__rhs).count();
  577. }
  578. #else
  579. template<typename _Rep1, typename _Period1,
  580. typename _Rep2, typename _Period2>
  581. constexpr bool
  582. operator!=(const duration<_Rep1, _Period1>& __lhs,
  583. const duration<_Rep2, _Period2>& __rhs)
  584. { return !(__lhs == __rhs); }
  585. #endif
  586. template<typename _Rep1, typename _Period1,
  587. typename _Rep2, typename _Period2>
  588. constexpr bool
  589. operator<=(const duration<_Rep1, _Period1>& __lhs,
  590. const duration<_Rep2, _Period2>& __rhs)
  591. { return !(__rhs < __lhs); }
  592. template<typename _Rep1, typename _Period1,
  593. typename _Rep2, typename _Period2>
  594. constexpr bool
  595. operator>(const duration<_Rep1, _Period1>& __lhs,
  596. const duration<_Rep2, _Period2>& __rhs)
  597. { return __rhs < __lhs; }
  598. template<typename _Rep1, typename _Period1,
  599. typename _Rep2, typename _Period2>
  600. constexpr bool
  601. operator>=(const duration<_Rep1, _Period1>& __lhs,
  602. const duration<_Rep2, _Period2>& __rhs)
  603. { return !(__lhs < __rhs); }
  604. /// @}
  605. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  606. # define _GLIBCXX_CHRONO_INT64_T int64_t
  607. #elif defined __INT64_TYPE__
  608. # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
  609. #else
  610. static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
  611. "Representation type for nanoseconds must have at least 64 bits");
  612. # define _GLIBCXX_CHRONO_INT64_T long long
  613. #endif
  614. /// nanoseconds
  615. using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
  616. /// microseconds
  617. using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
  618. /// milliseconds
  619. using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
  620. /// seconds
  621. using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
  622. /// minutes
  623. using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
  624. /// hours
  625. using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
  626. #if __cplusplus > 201703L
  627. /// days
  628. using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
  629. /// weeks
  630. using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
  631. /// years
  632. using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
  633. /// months
  634. using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
  635. #endif // C++20
  636. #undef _GLIBCXX_CHRONO_INT64_T
  637. /// time_point
  638. template<typename _Clock, typename _Dur>
  639. struct time_point
  640. {
  641. static_assert(__is_duration<_Dur>::value,
  642. "duration must be a specialization of std::chrono::duration");
  643. typedef _Clock clock;
  644. typedef _Dur duration;
  645. typedef typename duration::rep rep;
  646. typedef typename duration::period period;
  647. constexpr time_point() : __d(duration::zero())
  648. { }
  649. constexpr explicit time_point(const duration& __dur)
  650. : __d(__dur)
  651. { }
  652. // conversions
  653. template<typename _Dur2,
  654. typename = _Require<is_convertible<_Dur2, _Dur>>>
  655. constexpr time_point(const time_point<clock, _Dur2>& __t)
  656. : __d(__t.time_since_epoch())
  657. { }
  658. // observer
  659. constexpr duration
  660. time_since_epoch() const
  661. { return __d; }
  662. // arithmetic
  663. _GLIBCXX17_CONSTEXPR time_point&
  664. operator+=(const duration& __dur)
  665. {
  666. __d += __dur;
  667. return *this;
  668. }
  669. _GLIBCXX17_CONSTEXPR time_point&
  670. operator-=(const duration& __dur)
  671. {
  672. __d -= __dur;
  673. return *this;
  674. }
  675. // special values
  676. static constexpr time_point
  677. min() noexcept
  678. { return time_point(duration::min()); }
  679. static constexpr time_point
  680. max() noexcept
  681. { return time_point(duration::max()); }
  682. private:
  683. duration __d;
  684. };
  685. /// time_point_cast
  686. template<typename _ToDur, typename _Clock, typename _Dur>
  687. constexpr typename enable_if<__is_duration<_ToDur>::value,
  688. time_point<_Clock, _ToDur>>::type
  689. time_point_cast(const time_point<_Clock, _Dur>& __t)
  690. {
  691. typedef time_point<_Clock, _ToDur> __time_point;
  692. return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
  693. }
  694. #if __cplusplus > 201402L
  695. template<typename _ToDur, typename _Clock, typename _Dur>
  696. constexpr
  697. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  698. floor(const time_point<_Clock, _Dur>& __tp)
  699. {
  700. return time_point<_Clock, _ToDur>{
  701. chrono::floor<_ToDur>(__tp.time_since_epoch())};
  702. }
  703. template<typename _ToDur, typename _Clock, typename _Dur>
  704. constexpr
  705. enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
  706. ceil(const time_point<_Clock, _Dur>& __tp)
  707. {
  708. return time_point<_Clock, _ToDur>{
  709. chrono::ceil<_ToDur>(__tp.time_since_epoch())};
  710. }
  711. template<typename _ToDur, typename _Clock, typename _Dur>
  712. constexpr enable_if_t<
  713. __and_<__is_duration<_ToDur>,
  714. __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
  715. time_point<_Clock, _ToDur>>
  716. round(const time_point<_Clock, _Dur>& __tp)
  717. {
  718. return time_point<_Clock, _ToDur>{
  719. chrono::round<_ToDur>(__tp.time_since_epoch())};
  720. }
  721. #endif // C++17
  722. /// @relates time_point @{
  723. /// Adjust a time point forwards by the given duration.
  724. template<typename _Clock, typename _Dur1,
  725. typename _Rep2, typename _Period2>
  726. constexpr time_point<_Clock,
  727. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  728. operator+(const time_point<_Clock, _Dur1>& __lhs,
  729. const duration<_Rep2, _Period2>& __rhs)
  730. {
  731. typedef duration<_Rep2, _Period2> __dur2;
  732. typedef typename common_type<_Dur1,__dur2>::type __ct;
  733. typedef time_point<_Clock, __ct> __time_point;
  734. return __time_point(__lhs.time_since_epoch() + __rhs);
  735. }
  736. /// Adjust a time point forwards by the given duration.
  737. template<typename _Rep1, typename _Period1,
  738. typename _Clock, typename _Dur2>
  739. constexpr time_point<_Clock,
  740. typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
  741. operator+(const duration<_Rep1, _Period1>& __lhs,
  742. const time_point<_Clock, _Dur2>& __rhs)
  743. {
  744. typedef duration<_Rep1, _Period1> __dur1;
  745. typedef typename common_type<__dur1,_Dur2>::type __ct;
  746. typedef time_point<_Clock, __ct> __time_point;
  747. return __time_point(__rhs.time_since_epoch() + __lhs);
  748. }
  749. /// Adjust a time point backwards by the given duration.
  750. template<typename _Clock, typename _Dur1,
  751. typename _Rep2, typename _Period2>
  752. constexpr time_point<_Clock,
  753. typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
  754. operator-(const time_point<_Clock, _Dur1>& __lhs,
  755. const duration<_Rep2, _Period2>& __rhs)
  756. {
  757. typedef duration<_Rep2, _Period2> __dur2;
  758. typedef typename common_type<_Dur1,__dur2>::type __ct;
  759. typedef time_point<_Clock, __ct> __time_point;
  760. return __time_point(__lhs.time_since_epoch() -__rhs);
  761. }
  762. /// @}
  763. /// @relates time_point @{
  764. /// The difference between two time points (as a duration)
  765. template<typename _Clock, typename _Dur1, typename _Dur2>
  766. constexpr typename common_type<_Dur1, _Dur2>::type
  767. operator-(const time_point<_Clock, _Dur1>& __lhs,
  768. const time_point<_Clock, _Dur2>& __rhs)
  769. { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
  770. template<typename _Clock, typename _Dur1, typename _Dur2>
  771. constexpr bool
  772. operator==(const time_point<_Clock, _Dur1>& __lhs,
  773. const time_point<_Clock, _Dur2>& __rhs)
  774. { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
  775. #if __cpp_lib_three_way_comparison
  776. template<typename _Clock, typename _Dur1,
  777. three_way_comparable_with<_Dur1> _Dur2>
  778. constexpr auto
  779. operator<=>(const time_point<_Clock, _Dur1>& __lhs,
  780. const time_point<_Clock, _Dur2>& __rhs)
  781. { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
  782. #else
  783. template<typename _Clock, typename _Dur1, typename _Dur2>
  784. constexpr bool
  785. operator!=(const time_point<_Clock, _Dur1>& __lhs,
  786. const time_point<_Clock, _Dur2>& __rhs)
  787. { return !(__lhs == __rhs); }
  788. #endif
  789. template<typename _Clock, typename _Dur1, typename _Dur2>
  790. constexpr bool
  791. operator<(const time_point<_Clock, _Dur1>& __lhs,
  792. const time_point<_Clock, _Dur2>& __rhs)
  793. { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
  794. template<typename _Clock, typename _Dur1, typename _Dur2>
  795. constexpr bool
  796. operator<=(const time_point<_Clock, _Dur1>& __lhs,
  797. const time_point<_Clock, _Dur2>& __rhs)
  798. { return !(__rhs < __lhs); }
  799. template<typename _Clock, typename _Dur1, typename _Dur2>
  800. constexpr bool
  801. operator>(const time_point<_Clock, _Dur1>& __lhs,
  802. const time_point<_Clock, _Dur2>& __rhs)
  803. { return __rhs < __lhs; }
  804. template<typename _Clock, typename _Dur1, typename _Dur2>
  805. constexpr bool
  806. operator>=(const time_point<_Clock, _Dur1>& __lhs,
  807. const time_point<_Clock, _Dur2>& __rhs)
  808. { return !(__lhs < __rhs); }
  809. // @}
  810. // Clocks.
  811. // Why nanosecond resolution as the default?
  812. // Why have std::system_clock always count in the highest
  813. // resolution (ie nanoseconds), even if on some OSes the low 3
  814. // or 9 decimal digits will be always zero? This allows later
  815. // implementations to change the system_clock::now()
  816. // implementation any time to provide better resolution without
  817. // changing function signature or units.
  818. // To support the (forward) evolution of the library's defined
  819. // clocks, wrap inside inline namespace so that the current
  820. // defintions of system_clock, steady_clock, and
  821. // high_resolution_clock types are uniquely mangled. This way, new
  822. // code can use the latests clocks, while the library can contain
  823. // compatibility definitions for previous versions. At some
  824. // point, when these clocks settle down, the inlined namespaces
  825. // can be removed. XXX GLIBCXX_ABI Deprecated
  826. inline namespace _V2 {
  827. /**
  828. * @brief System clock.
  829. *
  830. * Time returned represents wall time from the system-wide clock.
  831. * @ingroup chrono
  832. */
  833. struct system_clock
  834. {
  835. typedef chrono::nanoseconds duration;
  836. typedef duration::rep rep;
  837. typedef duration::period period;
  838. typedef chrono::time_point<system_clock, duration> time_point;
  839. static_assert(system_clock::duration::min()
  840. < system_clock::duration::zero(),
  841. "a clock's minimum duration cannot be less than its epoch");
  842. static constexpr bool is_steady = false;
  843. static time_point
  844. now() noexcept;
  845. // Map to C API
  846. static std::time_t
  847. to_time_t(const time_point& __t) noexcept
  848. {
  849. return std::time_t(duration_cast<chrono::seconds>
  850. (__t.time_since_epoch()).count());
  851. }
  852. static time_point
  853. from_time_t(std::time_t __t) noexcept
  854. {
  855. typedef chrono::time_point<system_clock, seconds> __from;
  856. return time_point_cast<system_clock::duration>
  857. (__from(chrono::seconds(__t)));
  858. }
  859. };
  860. /**
  861. * @brief Monotonic clock
  862. *
  863. * Time returned has the property of only increasing at a uniform rate.
  864. * @ingroup chrono
  865. */
  866. struct steady_clock
  867. {
  868. typedef chrono::nanoseconds duration;
  869. typedef duration::rep rep;
  870. typedef duration::period period;
  871. typedef chrono::time_point<steady_clock, duration> time_point;
  872. static constexpr bool is_steady = true;
  873. static time_point
  874. now() noexcept;
  875. };
  876. /**
  877. * @brief Highest-resolution clock
  878. *
  879. * This is the clock "with the shortest tick period." Alias to
  880. * std::system_clock until higher-than-nanosecond definitions
  881. * become feasible.
  882. * @ingroup chrono
  883. */
  884. using high_resolution_clock = system_clock;
  885. } // end inline namespace _V2
  886. #if __cplusplus > 201703L
  887. template<typename _Duration>
  888. using sys_time = time_point<system_clock, _Duration>;
  889. using sys_seconds = sys_time<seconds>;
  890. using sys_days = sys_time<days>;
  891. using file_clock = ::std::filesystem::__file_clock;
  892. template<typename _Duration>
  893. using file_time = time_point<file_clock, _Duration>;
  894. template<> struct is_clock<system_clock> : true_type { };
  895. template<> struct is_clock<steady_clock> : true_type { };
  896. template<> struct is_clock<file_clock> : true_type { };
  897. template<> inline constexpr bool is_clock_v<system_clock> = true;
  898. template<> inline constexpr bool is_clock_v<steady_clock> = true;
  899. template<> inline constexpr bool is_clock_v<file_clock> = true;
  900. struct local_t { };
  901. template<typename _Duration>
  902. using local_time = time_point<local_t, _Duration>;
  903. using local_seconds = local_time<seconds>;
  904. using local_days = local_time<days>;
  905. #endif // C++20
  906. // @}
  907. } // namespace chrono
  908. #if __cplusplus > 201103L
  909. #define __cpp_lib_chrono_udls 201304
  910. inline namespace literals
  911. {
  912. /** ISO C++ 2014 namespace for suffixes for duration literals.
  913. *
  914. * These suffixes can be used to create `chrono::duration` values with
  915. * tick periods of hours, minutes, seconds, milliseconds, microseconds
  916. * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
  917. * as `5s` after making the suffix visible in the current scope.
  918. * The suffixes can be made visible by a using-directive or
  919. * using-declaration such as:
  920. * - `using namespace std::chrono_literals;`
  921. * - `using namespace std::literals;`
  922. * - `using namespace std::chrono;`
  923. * - `using namespace std;`
  924. * - `using std::chrono_literals::operator""s;`
  925. *
  926. * The result of these suffixes on an integer literal is one of the
  927. * standard typedefs such as `std::chrono::hours`.
  928. * The result on a floating-point literal is a duration type with the
  929. * specified tick period and an unspecified floating-point representation,
  930. * for example `1.5e2ms` might be equivalent to
  931. * `chrono::duration<long double, chrono::milli>(1.5e2)`.
  932. *
  933. * @ingroup chrono
  934. */
  935. inline namespace chrono_literals
  936. {
  937. #pragma GCC diagnostic push
  938. #pragma GCC diagnostic ignored "-Wliteral-suffix"
  939. /// @cond undocumented
  940. template<typename _Dur, char... _Digits>
  941. constexpr _Dur __check_overflow()
  942. {
  943. using _Val = __parse_int::_Parse_int<_Digits...>;
  944. constexpr typename _Dur::rep __repval = _Val::value;
  945. static_assert(__repval >= 0 && __repval == _Val::value,
  946. "literal value cannot be represented by duration type");
  947. return _Dur(__repval);
  948. }
  949. /// @endcond
  950. /// Literal suffix for durations representing non-integer hours
  951. constexpr chrono::duration<long double, ratio<3600,1>>
  952. operator""h(long double __hours)
  953. { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
  954. /// Literal suffix for durations of type `std::chrono::hours`
  955. template <char... _Digits>
  956. constexpr chrono::hours
  957. operator""h()
  958. { return __check_overflow<chrono::hours, _Digits...>(); }
  959. /// Literal suffix for durations representing non-integer minutes
  960. constexpr chrono::duration<long double, ratio<60,1>>
  961. operator""min(long double __mins)
  962. { return chrono::duration<long double, ratio<60,1>>{__mins}; }
  963. /// Literal suffix for durations of type `std::chrono::minutes`
  964. template <char... _Digits>
  965. constexpr chrono::minutes
  966. operator""min()
  967. { return __check_overflow<chrono::minutes, _Digits...>(); }
  968. /// Literal suffix for durations representing non-integer seconds
  969. constexpr chrono::duration<long double>
  970. operator""s(long double __secs)
  971. { return chrono::duration<long double>{__secs}; }
  972. /// Literal suffix for durations of type `std::chrono::seconds`
  973. template <char... _Digits>
  974. constexpr chrono::seconds
  975. operator""s()
  976. { return __check_overflow<chrono::seconds, _Digits...>(); }
  977. /// Literal suffix for durations representing non-integer milliseconds
  978. constexpr chrono::duration<long double, milli>
  979. operator""ms(long double __msecs)
  980. { return chrono::duration<long double, milli>{__msecs}; }
  981. /// Literal suffix for durations of type `std::chrono::milliseconds`
  982. template <char... _Digits>
  983. constexpr chrono::milliseconds
  984. operator""ms()
  985. { return __check_overflow<chrono::milliseconds, _Digits...>(); }
  986. /// Literal suffix for durations representing non-integer microseconds
  987. constexpr chrono::duration<long double, micro>
  988. operator""us(long double __usecs)
  989. { return chrono::duration<long double, micro>{__usecs}; }
  990. /// Literal suffix for durations of type `std::chrono::microseconds`
  991. template <char... _Digits>
  992. constexpr chrono::microseconds
  993. operator""us()
  994. { return __check_overflow<chrono::microseconds, _Digits...>(); }
  995. /// Literal suffix for durations representing non-integer nanoseconds
  996. constexpr chrono::duration<long double, nano>
  997. operator""ns(long double __nsecs)
  998. { return chrono::duration<long double, nano>{__nsecs}; }
  999. /// Literal suffix for durations of type `std::chrono::nanoseconds`
  1000. template <char... _Digits>
  1001. constexpr chrono::nanoseconds
  1002. operator""ns()
  1003. { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
  1004. #pragma GCC diagnostic pop
  1005. } // inline namespace chrono_literals
  1006. } // inline namespace literals
  1007. namespace chrono
  1008. {
  1009. using namespace literals::chrono_literals;
  1010. } // namespace chrono
  1011. #if __cplusplus >= 201703L
  1012. namespace filesystem
  1013. {
  1014. struct __file_clock
  1015. {
  1016. using duration = chrono::nanoseconds;
  1017. using rep = duration::rep;
  1018. using period = duration::period;
  1019. using time_point = chrono::time_point<__file_clock>;
  1020. static constexpr bool is_steady = false;
  1021. static time_point
  1022. now() noexcept
  1023. { return _S_from_sys(chrono::system_clock::now()); }
  1024. #if __cplusplus > 201703L
  1025. template<typename _Dur>
  1026. static
  1027. chrono::file_time<_Dur>
  1028. from_sys(const chrono::sys_time<_Dur>& __t) noexcept
  1029. { return _S_from_sys(__t); }
  1030. // For internal use only
  1031. template<typename _Dur>
  1032. static
  1033. chrono::sys_time<_Dur>
  1034. to_sys(const chrono::file_time<_Dur>& __t) noexcept
  1035. { return _S_to_sys(__t); }
  1036. #endif // C++20
  1037. private:
  1038. using __sys_clock = chrono::system_clock;
  1039. // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
  1040. // A signed 64-bit duration with nanosecond resolution gives roughly
  1041. // +/- 292 years, which covers the 1901-2446 date range for ext4.
  1042. static constexpr chrono::seconds _S_epoch_diff{6437664000};
  1043. protected:
  1044. // For internal use only
  1045. template<typename _Dur>
  1046. static
  1047. chrono::time_point<__file_clock, _Dur>
  1048. _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
  1049. {
  1050. using __file_time = chrono::time_point<__file_clock, _Dur>;
  1051. return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
  1052. }
  1053. // For internal use only
  1054. template<typename _Dur>
  1055. static
  1056. chrono::time_point<__sys_clock, _Dur>
  1057. _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
  1058. {
  1059. using __sys_time = chrono::time_point<__sys_clock, _Dur>;
  1060. return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
  1061. }
  1062. };
  1063. } // namespace filesystem
  1064. #endif // C++17
  1065. #endif // C++14
  1066. _GLIBCXX_END_NAMESPACE_VERSION
  1067. } // namespace std
  1068. #endif // C++11
  1069. #endif //_GLIBCXX_CHRONO