chrono.h 42 KB

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