chrono 40 KB

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