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