1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008-2020 Free Software Foundation, Inc.
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)
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.
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.
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/>.
25 /** @file include/chrono
26 * This is a Standard C++ Library header.
30 #ifndef _GLIBCXX_CHRONO
31 #define _GLIBCXX_CHRONO 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
40 #include <type_traits>
43 #include <bits/parse_numbers.h> // for literals support.
44 #if __cplusplus > 201703L
49 namespace std _GLIBCXX_VISIBILITY(default)
51 _GLIBCXX_BEGIN_NAMESPACE_VERSION
53 #if __cplusplus >= 201703L
54 namespace filesystem { struct __file_clock; };
58 * @defgroup chrono Time
61 * Classes and functions for time.
65 /** @namespace std::chrono
66 * @brief ISO C++ 2011 namespace for date and time utilities
70 template<typename _Rep, typename _Period = ratio<1>>
73 template<typename _Clock, typename _Dur = typename _Clock::duration>
77 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
79 /// @cond undocumented
81 template<typename _CT, typename _Period1, typename _Period2, typename = void>
82 struct __duration_common_type
85 template<typename _CT, typename _Period1, typename _Period2>
86 struct __duration_common_type<_CT, _Period1, _Period2,
87 __void_t<typename _CT::type>>
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>;
97 using type = chrono::duration<__cr, typename __r::type>;
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>
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>>
118 using type = chrono::duration<typename common_type<_Rep>::type,
119 typename _Period::type>;
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>>
127 using type = chrono::duration<typename common_type<_Rep>::type,
128 typename _Period::type>;
131 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
133 /// @cond undocumented
135 template<typename _CT, typename _Clock, typename = void>
136 struct __timepoint_common_type
139 template<typename _CT, typename _Clock>
140 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
142 using type = chrono::time_point<_Clock, typename _CT::type>;
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>
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>; };
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>; };
172 /// @addtogroup chrono
175 /// @cond undocumented
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
182 template<typename _Rep, typename _Period>
183 static constexpr _ToDur
184 __cast(const duration<_Rep, _Period>& __d)
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)));
193 template<typename _ToDur, typename _CF, typename _CR>
194 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
196 template<typename _Rep, typename _Period>
197 static constexpr _ToDur
198 __cast(const duration<_Rep, _Period>& __d)
200 typedef typename _ToDur::rep __to_rep;
201 return _ToDur(static_cast<__to_rep>(__d.count()));
205 template<typename _ToDur, typename _CF, typename _CR>
206 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
208 template<typename _Rep, typename _Period>
209 static constexpr _ToDur
210 __cast(const duration<_Rep, _Period>& __d)
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)));
218 template<typename _ToDur, typename _CF, typename _CR>
219 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
221 template<typename _Rep, typename _Period>
222 static constexpr _ToDur
223 __cast(const duration<_Rep, _Period>& __d)
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)));
231 template<typename _Tp>
236 template<typename _Rep, typename _Period>
237 struct __is_duration<duration<_Rep, _Period>>
241 template<typename _Tp>
242 using __enable_if_is_duration
243 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
245 template<typename _Tp>
246 using __disable_if_is_duration
247 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
252 template<typename _ToDur, typename _Rep, typename _Period>
253 constexpr __enable_if_is_duration<_ToDur>
254 duration_cast(const duration<_Rep, _Period>& __d)
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
261 typedef __duration_cast_impl<_ToDur, __cf, __cr,
262 __cf::num == 1, __cf::den == 1> __dc;
263 return __dc::__cast(__d);
266 /// treat_as_floating_point
267 template<typename _Rep>
268 struct treat_as_floating_point
269 : is_floating_point<_Rep>
272 #if __cplusplus > 201402L
273 template <typename _Rep>
274 inline constexpr bool treat_as_floating_point_v =
275 treat_as_floating_point<_Rep>::value;
278 #if __cplusplus > 201703L
279 template<typename _Tp>
282 template<typename _Tp>
283 inline constexpr bool is_clock_v = is_clock<_Tp>::value;
285 #if __cpp_lib_concepts
286 template<typename _Tp>
287 struct is_clock : false_type
290 template<typename _Tp>
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>;
304 struct is_clock<_Tp> : true_type
307 template<typename _Tp, typename = void>
308 struct __is_clock_impl : false_type
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
326 template<typename _Tp>
327 struct is_clock : __is_clock_impl<_Tp>::type
332 #if __cplusplus >= 201703L
333 # define __cpp_lib_chrono 201611
335 template<typename _ToDur, typename _Rep, typename _Period>
336 constexpr __enable_if_is_duration<_ToDur>
337 floor(const duration<_Rep, _Period>& __d)
339 auto __to = chrono::duration_cast<_ToDur>(__d);
341 return __to - _ToDur{1};
345 template<typename _ToDur, typename _Rep, typename _Period>
346 constexpr __enable_if_is_duration<_ToDur>
347 ceil(const duration<_Rep, _Period>& __d)
349 auto __to = chrono::duration_cast<_ToDur>(__d);
351 return __to + _ToDur{1};
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,
360 round(const duration<_Rep, _Period>& __d)
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)
368 if (__t0.count() & 1)
372 else if (__diff0 < __diff1)
377 template<typename _Rep, typename _Period>
379 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
380 abs(duration<_Rep, _Period> __d)
382 if (__d >= __d.zero())
389 template<typename _Rep>
390 struct duration_values
392 static constexpr _Rep
396 static constexpr _Rep
398 { return numeric_limits<_Rep>::max(); }
400 static constexpr _Rep
402 { return numeric_limits<_Rep>::lowest(); }
405 /// @cond undocumented
407 template<typename _Tp>
412 template<intmax_t _Num, intmax_t _Den>
413 struct __is_ratio<ratio<_Num, _Den>>
420 template<typename _Rep, typename _Period>
424 template<typename _Rep2>
425 using __is_float = treat_as_floating_point<_Rep2>;
427 static constexpr intmax_t
428 _S_gcd(intmax_t __m, intmax_t __n) noexcept
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);
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)>;
445 // _Period2 is an exact multiple of _Period
446 template<typename _Period2>
448 = __bool_constant<__divide<_Period2, _Period>::den == 1>;
453 using period = typename _Period::type;
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");
460 // 20.11.5.1 construction / copy / destroy
461 constexpr duration() = default;
463 duration(const duration&) = default;
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)) { }
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()) { }
481 ~duration() = default;
482 duration& operator=(const duration&) = default;
484 // 20.11.5.2 observer
489 // 20.11.5.3 arithmetic
491 constexpr duration<typename common_type<rep>::type, period>
493 { return duration<typename common_type<rep>::type, period>(__r); }
495 constexpr duration<typename common_type<rep>::type, period>
497 { return duration<typename common_type<rep>::type, period>(-__r); }
499 _GLIBCXX17_CONSTEXPR duration&
506 _GLIBCXX17_CONSTEXPR duration
508 { return duration(__r++); }
510 _GLIBCXX17_CONSTEXPR duration&
517 _GLIBCXX17_CONSTEXPR duration
519 { return duration(__r--); }
521 _GLIBCXX17_CONSTEXPR duration&
522 operator+=(const duration& __d)
528 _GLIBCXX17_CONSTEXPR duration&
529 operator-=(const duration& __d)
535 _GLIBCXX17_CONSTEXPR duration&
536 operator*=(const rep& __rhs)
542 _GLIBCXX17_CONSTEXPR duration&
543 operator/=(const rep& __rhs)
550 template<typename _Rep2 = rep>
552 typename enable_if<!treat_as_floating_point<_Rep2>::value,
554 operator%=(const rep& __rhs)
560 template<typename _Rep2 = rep>
562 typename enable_if<!treat_as_floating_point<_Rep2>::value,
564 operator%=(const duration& __d)
570 // 20.11.5.4 special values
571 static constexpr duration
573 { return duration(duration_values<rep>::zero()); }
575 static constexpr duration
577 { return duration(duration_values<rep>::min()); }
579 static constexpr duration
581 { return duration(duration_values<rep>::max()); }
587 /// @relates duration @{
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)
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());
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)
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());
619 /// @cond undocumented
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;
632 /// @relates duration @{
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)
639 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
641 return __cd(__cd(__d).count() * __s);
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; }
650 template<typename _Rep1, typename _Period, typename _Rep2>
652 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
653 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
655 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
657 return __cd(__cd(__d).count() / __s);
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)
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();
673 template<typename _Rep1, typename _Period, typename _Rep2>
675 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
676 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
678 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
680 return __cd(__cd(__d).count() % __s);
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)
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());
698 template<typename _Rep1, typename _Period1,
699 typename _Rep2, typename _Period2>
701 operator==(const duration<_Rep1, _Period1>& __lhs,
702 const duration<_Rep2, _Period2>& __rhs)
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();
710 template<typename _Rep1, typename _Period1,
711 typename _Rep2, typename _Period2>
713 operator<(const duration<_Rep1, _Period1>& __lhs,
714 const duration<_Rep2, _Period2>& __rhs)
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();
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>>
727 operator<=>(const duration<_Rep1, _Period1>& __lhs,
728 const duration<_Rep2, _Period2>& __rhs)
730 using __ct = common_type_t<duration<_Rep1, _Period1>,
731 duration<_Rep2, _Period2>>;
732 return __ct(__lhs).count() <=> __ct(__rhs).count();
735 template<typename _Rep1, typename _Period1,
736 typename _Rep2, typename _Period2>
738 operator!=(const duration<_Rep1, _Period1>& __lhs,
739 const duration<_Rep2, _Period2>& __rhs)
740 { return !(__lhs == __rhs); }
743 template<typename _Rep1, typename _Period1,
744 typename _Rep2, typename _Period2>
746 operator<=(const duration<_Rep1, _Period1>& __lhs,
747 const duration<_Rep2, _Period2>& __rhs)
748 { return !(__rhs < __lhs); }
750 template<typename _Rep1, typename _Period1,
751 typename _Rep2, typename _Period2>
753 operator>(const duration<_Rep1, _Period1>& __lhs,
754 const duration<_Rep2, _Period2>& __rhs)
755 { return __rhs < __lhs; }
757 template<typename _Rep1, typename _Period1,
758 typename _Rep2, typename _Period2>
760 operator>=(const duration<_Rep1, _Period1>& __lhs,
761 const duration<_Rep2, _Period2>& __rhs)
762 { return !(__lhs < __rhs); }
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__
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
777 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
780 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
783 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
786 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
789 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
792 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
794 #if __cplusplus > 201703L
796 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
799 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
802 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
805 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
808 #undef _GLIBCXX_CHRONO_INT64_T
811 template<typename _Clock, typename _Dur>
814 static_assert(__is_duration<_Dur>::value,
815 "duration must be a specialization of std::chrono::duration");
817 typedef _Clock clock;
818 typedef _Dur duration;
819 typedef typename duration::rep rep;
820 typedef typename duration::period period;
822 constexpr time_point() : __d(duration::zero())
825 constexpr explicit time_point(const duration& __dur)
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())
838 time_since_epoch() const
842 _GLIBCXX17_CONSTEXPR time_point&
843 operator+=(const duration& __dur)
849 _GLIBCXX17_CONSTEXPR time_point&
850 operator-=(const duration& __dur)
857 static constexpr time_point
859 { return time_point(duration::min()); }
861 static constexpr time_point
863 { return time_point(duration::max()); }
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)
875 typedef time_point<_Clock, _ToDur> __time_point;
876 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
879 #if __cplusplus > 201402L
880 template<typename _ToDur, typename _Clock, typename _Dur>
882 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
883 floor(const time_point<_Clock, _Dur>& __tp)
885 return time_point<_Clock, _ToDur>{
886 chrono::floor<_ToDur>(__tp.time_since_epoch())};
889 template<typename _ToDur, typename _Clock, typename _Dur>
891 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
892 ceil(const time_point<_Clock, _Dur>& __tp)
894 return time_point<_Clock, _ToDur>{
895 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
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)
905 return time_point<_Clock, _ToDur>{
906 chrono::round<_ToDur>(__tp.time_since_epoch())};
910 /// @relates time_point @{
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)
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);
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)
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);
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)
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);
956 /// @relates time_point @{
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(); }
965 template<typename _Clock, typename _Dur1, typename _Dur2>
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(); }
971 #if __cpp_lib_three_way_comparison
972 template<typename _Clock, typename _Dur1,
973 three_way_comparable_with<_Dur1> _Dur2>
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(); }
979 template<typename _Clock, typename _Dur1, typename _Dur2>
981 operator!=(const time_point<_Clock, _Dur1>& __lhs,
982 const time_point<_Clock, _Dur2>& __rhs)
983 { return !(__lhs == __rhs); }
986 template<typename _Clock, typename _Dur1, typename _Dur2>
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(); }
992 template<typename _Clock, typename _Dur1, typename _Dur2>
994 operator<=(const time_point<_Clock, _Dur1>& __lhs,
995 const time_point<_Clock, _Dur2>& __rhs)
996 { return !(__rhs < __lhs); }
998 template<typename _Clock, typename _Dur1, typename _Dur2>
1000 operator>(const time_point<_Clock, _Dur1>& __lhs,
1001 const time_point<_Clock, _Dur2>& __rhs)
1002 { return __rhs < __lhs; }
1004 template<typename _Clock, typename _Dur1, typename _Dur2>
1006 operator>=(const time_point<_Clock, _Dur1>& __lhs,
1007 const time_point<_Clock, _Dur2>& __rhs)
1008 { return !(__lhs < __rhs); }
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.
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 {
1033 * @brief System clock.
1035 * Time returned represents wall time from the system-wide clock.
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;
1045 static_assert(system_clock::duration::min()
1046 < system_clock::duration::zero(),
1047 "a clock's minimum duration cannot be less than its epoch");
1049 static constexpr bool is_steady = false;
1056 to_time_t(const time_point& __t) noexcept
1058 return std::time_t(duration_cast<chrono::seconds>
1059 (__t.time_since_epoch()).count());
1063 from_time_t(std::time_t __t) noexcept
1065 typedef chrono::time_point<system_clock, seconds> __from;
1066 return time_point_cast<system_clock::duration>
1067 (__from(chrono::seconds(__t)));
1073 * @brief Monotonic clock
1075 * Time returned has the property of only increasing at a uniform rate.
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;
1085 static constexpr bool is_steady = true;
1093 * @brief Highest-resolution clock
1095 * This is the clock "with the shortest tick period." Alias to
1096 * std::system_clock until higher-than-nanosecond definitions
1100 using high_resolution_clock = system_clock;
1102 } // end inline namespace _V2
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>;
1110 using file_clock = ::std::filesystem::__file_clock;
1112 template<typename _Duration>
1113 using file_time = time_point<file_clock, _Duration>;
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 { };
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;
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>;
1131 } // namespace chrono
1133 #if __cplusplus > 201103L
1135 #define __cpp_lib_chrono_udls 201304
1137 inline namespace literals
1139 /** ISO C++ 2014 namespace for suffixes for duration literals.
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;`
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)`.
1162 inline namespace chrono_literals
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()
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);
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}; }
1183 /// Literal suffix for durations of type `std::chrono::hours`
1184 template <char... _Digits>
1185 constexpr chrono::hours
1187 { return __check_overflow<chrono::hours, _Digits...>(); }
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}; }
1194 /// Literal suffix for durations of type `std::chrono::minutes`
1195 template <char... _Digits>
1196 constexpr chrono::minutes
1198 { return __check_overflow<chrono::minutes, _Digits...>(); }
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}; }
1205 /// Literal suffix for durations of type `std::chrono::seconds`
1206 template <char... _Digits>
1207 constexpr chrono::seconds
1209 { return __check_overflow<chrono::seconds, _Digits...>(); }
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}; }
1216 /// Literal suffix for durations of type `std::chrono::milliseconds`
1217 template <char... _Digits>
1218 constexpr chrono::milliseconds
1220 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
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}; }
1227 /// Literal suffix for durations of type `std::chrono::microseconds`
1228 template <char... _Digits>
1229 constexpr chrono::microseconds
1231 { return __check_overflow<chrono::microseconds, _Digits...>(); }
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}; }
1238 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1239 template <char... _Digits>
1240 constexpr chrono::nanoseconds
1242 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1244 #pragma GCC diagnostic pop
1245 } // inline namespace chrono_literals
1246 } // inline namespace literals
1250 using namespace literals::chrono_literals;
1251 } // namespace chrono
1253 #if __cplusplus >= 201703L
1254 namespace filesystem
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;
1266 { return _S_from_sys(chrono::system_clock::now()); }
1268 #if __cplusplus > 201703L
1269 template<typename _Dur>
1271 chrono::file_time<_Dur>
1272 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1273 { return _S_from_sys(__t); }
1275 // For internal use only
1276 template<typename _Dur>
1278 chrono::sys_time<_Dur>
1279 to_sys(const chrono::file_time<_Dur>& __t) noexcept
1280 { return _S_to_sys(__t); }
1284 using __sys_clock = chrono::system_clock;
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};
1292 // For internal use only
1293 template<typename _Dur>
1295 chrono::time_point<__file_clock, _Dur>
1296 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1298 using __file_time = chrono::time_point<__file_clock, _Dur>;
1299 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1302 // For internal use only
1303 template<typename _Dur>
1305 chrono::time_point<__sys_clock, _Dur>
1306 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1308 using __sys_time = chrono::time_point<__sys_clock, _Dur>;
1309 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1312 } // namespace filesystem
1316 _GLIBCXX_END_NAMESPACE_VERSION
1321 #endif //_GLIBCXX_CHRONO