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, __r>;
100 template<typename _Period1, typename _Period2>
101 struct __duration_common_type<__failure_type, _Period1, _Period2>
102 { typedef __failure_type type; };
106 /// Specialization of common_type for chrono::duration types.
107 /// @relates duration
108 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
109 struct common_type<chrono::duration<_Rep1, _Period1>,
110 chrono::duration<_Rep2, _Period2>>
111 : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
114 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
116 /// @cond undocumented
118 template<typename _CT, typename _Clock, typename = void>
119 struct __timepoint_common_type
122 template<typename _CT, typename _Clock>
123 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
125 using type = chrono::time_point<_Clock, typename _CT::type>;
130 /// Specialization of common_type for chrono::time_point types.
131 /// @relates time_point
132 template<typename _Clock, typename _Duration1, typename _Duration2>
133 struct common_type<chrono::time_point<_Clock, _Duration1>,
134 chrono::time_point<_Clock, _Duration2>>
135 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
142 /// @addtogroup chrono
145 /// @cond undocumented
147 // Primary template for duration_cast impl.
148 template<typename _ToDur, typename _CF, typename _CR,
149 bool _NumIsOne = false, bool _DenIsOne = false>
150 struct __duration_cast_impl
152 template<typename _Rep, typename _Period>
153 static constexpr _ToDur
154 __cast(const duration<_Rep, _Period>& __d)
156 typedef typename _ToDur::rep __to_rep;
157 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
158 * static_cast<_CR>(_CF::num)
159 / static_cast<_CR>(_CF::den)));
163 template<typename _ToDur, typename _CF, typename _CR>
164 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
166 template<typename _Rep, typename _Period>
167 static constexpr _ToDur
168 __cast(const duration<_Rep, _Period>& __d)
170 typedef typename _ToDur::rep __to_rep;
171 return _ToDur(static_cast<__to_rep>(__d.count()));
175 template<typename _ToDur, typename _CF, typename _CR>
176 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
178 template<typename _Rep, typename _Period>
179 static constexpr _ToDur
180 __cast(const duration<_Rep, _Period>& __d)
182 typedef typename _ToDur::rep __to_rep;
183 return _ToDur(static_cast<__to_rep>(
184 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
188 template<typename _ToDur, typename _CF, typename _CR>
189 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
191 template<typename _Rep, typename _Period>
192 static constexpr _ToDur
193 __cast(const duration<_Rep, _Period>& __d)
195 typedef typename _ToDur::rep __to_rep;
196 return _ToDur(static_cast<__to_rep>(
197 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
201 template<typename _Tp>
206 template<typename _Rep, typename _Period>
207 struct __is_duration<duration<_Rep, _Period>>
211 template<typename _Tp>
212 using __enable_if_is_duration
213 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
215 template<typename _Tp>
216 using __disable_if_is_duration
217 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
222 template<typename _ToDur, typename _Rep, typename _Period>
223 constexpr __enable_if_is_duration<_ToDur>
224 duration_cast(const duration<_Rep, _Period>& __d)
226 typedef typename _ToDur::period __to_period;
227 typedef typename _ToDur::rep __to_rep;
228 typedef ratio_divide<_Period, __to_period> __cf;
229 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
231 typedef __duration_cast_impl<_ToDur, __cf, __cr,
232 __cf::num == 1, __cf::den == 1> __dc;
233 return __dc::__cast(__d);
236 /// treat_as_floating_point
237 template<typename _Rep>
238 struct treat_as_floating_point
239 : is_floating_point<_Rep>
242 #if __cplusplus > 201402L
243 template <typename _Rep>
244 inline constexpr bool treat_as_floating_point_v =
245 treat_as_floating_point<_Rep>::value;
248 #if __cplusplus > 201703L
249 template<typename _Tp>
252 template<typename _Tp>
253 inline constexpr bool is_clock_v = is_clock<_Tp>::value;
255 #if __cpp_lib_concepts
256 template<typename _Tp>
257 struct is_clock : false_type
260 template<typename _Tp>
263 typename _Tp::period;
264 typename _Tp::duration;
265 typename _Tp::time_point::clock;
266 typename _Tp::time_point::duration;
267 { &_Tp::is_steady } -> same_as<const bool*>;
268 { _Tp::now() } -> same_as<typename _Tp::time_point>;
269 requires same_as<typename _Tp::duration,
270 duration<typename _Tp::rep, typename _Tp::period>>;
271 requires same_as<typename _Tp::time_point::duration,
272 typename _Tp::duration>;
274 struct is_clock<_Tp> : true_type
277 template<typename _Tp, typename = void>
278 struct __is_clock_impl : false_type
281 template<typename _Tp>
282 struct __is_clock_impl<_Tp,
283 void_t<typename _Tp::rep, typename _Tp::period,
284 typename _Tp::duration,
285 typename _Tp::time_point::duration,
286 decltype(_Tp::is_steady),
287 decltype(_Tp::now())>>
288 : __and_<is_same<typename _Tp::duration,
289 duration<typename _Tp::rep, typename _Tp::period>>,
290 is_same<typename _Tp::time_point::duration,
291 typename _Tp::duration>,
292 is_same<decltype(&_Tp::is_steady), const bool*>,
293 is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
296 template<typename _Tp>
297 struct is_clock : __is_clock_impl<_Tp>::type
302 #if __cplusplus >= 201703L
303 # define __cpp_lib_chrono 201611
305 template<typename _ToDur, typename _Rep, typename _Period>
306 constexpr __enable_if_is_duration<_ToDur>
307 floor(const duration<_Rep, _Period>& __d)
309 auto __to = chrono::duration_cast<_ToDur>(__d);
311 return __to - _ToDur{1};
315 template<typename _ToDur, typename _Rep, typename _Period>
316 constexpr __enable_if_is_duration<_ToDur>
317 ceil(const duration<_Rep, _Period>& __d)
319 auto __to = chrono::duration_cast<_ToDur>(__d);
321 return __to + _ToDur{1};
325 template <typename _ToDur, typename _Rep, typename _Period>
326 constexpr enable_if_t<
327 __and_<__is_duration<_ToDur>,
328 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
330 round(const duration<_Rep, _Period>& __d)
332 _ToDur __t0 = chrono::floor<_ToDur>(__d);
333 _ToDur __t1 = __t0 + _ToDur{1};
334 auto __diff0 = __d - __t0;
335 auto __diff1 = __t1 - __d;
336 if (__diff0 == __diff1)
338 if (__t0.count() & 1)
342 else if (__diff0 < __diff1)
347 template<typename _Rep, typename _Period>
349 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
350 abs(duration<_Rep, _Period> __d)
352 if (__d >= __d.zero())
359 template<typename _Rep>
360 struct duration_values
362 static constexpr _Rep
366 static constexpr _Rep
368 { return numeric_limits<_Rep>::max(); }
370 static constexpr _Rep
372 { return numeric_limits<_Rep>::lowest(); }
375 /// @cond undocumented
377 template<typename _Tp>
382 template<intmax_t _Num, intmax_t _Den>
383 struct __is_ratio<ratio<_Num, _Den>>
390 template<typename _Rep, typename _Period>
394 template<typename _Rep2>
395 using __is_float = treat_as_floating_point<_Rep2>;
397 // _Period2 is an exact multiple of _Period
398 template<typename _Period2>
400 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
405 typedef _Period period;
407 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
408 static_assert(__is_ratio<_Period>::value,
409 "period must be a specialization of ratio");
410 static_assert(_Period::num > 0, "period must be positive");
412 // 20.11.5.1 construction / copy / destroy
413 constexpr duration() = default;
415 duration(const duration&) = default;
417 // _GLIBCXX_RESOLVE_LIB_DEFECTS
418 // 3050. Conversion specification problem in chrono::duration
419 template<typename _Rep2, typename = _Require<
420 is_convertible<const _Rep2&, rep>,
421 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
422 constexpr explicit duration(const _Rep2& __rep)
423 : __r(static_cast<rep>(__rep)) { }
425 template<typename _Rep2, typename _Period2, typename = _Require<
426 __or_<__is_float<rep>,
427 __and_<__is_harmonic<_Period2>,
428 __not_<__is_float<_Rep2>>>>>>
429 constexpr duration(const duration<_Rep2, _Period2>& __d)
430 : __r(duration_cast<duration>(__d).count()) { }
432 ~duration() = default;
433 duration& operator=(const duration&) = default;
435 // 20.11.5.2 observer
440 // 20.11.5.3 arithmetic
447 { return duration(-__r); }
449 _GLIBCXX17_CONSTEXPR duration&
456 _GLIBCXX17_CONSTEXPR duration
458 { return duration(__r++); }
460 _GLIBCXX17_CONSTEXPR duration&
467 _GLIBCXX17_CONSTEXPR duration
469 { return duration(__r--); }
471 _GLIBCXX17_CONSTEXPR duration&
472 operator+=(const duration& __d)
478 _GLIBCXX17_CONSTEXPR duration&
479 operator-=(const duration& __d)
485 _GLIBCXX17_CONSTEXPR duration&
486 operator*=(const rep& __rhs)
492 _GLIBCXX17_CONSTEXPR duration&
493 operator/=(const rep& __rhs)
500 template<typename _Rep2 = rep>
502 typename enable_if<!treat_as_floating_point<_Rep2>::value,
504 operator%=(const rep& __rhs)
510 template<typename _Rep2 = rep>
512 typename enable_if<!treat_as_floating_point<_Rep2>::value,
514 operator%=(const duration& __d)
520 // 20.11.5.4 special values
521 static constexpr duration
523 { return duration(duration_values<rep>::zero()); }
525 static constexpr duration
527 { return duration(duration_values<rep>::min()); }
529 static constexpr duration
531 { return duration(duration_values<rep>::max()); }
537 /// @relates duration @{
539 /// The sum of two durations.
540 template<typename _Rep1, typename _Period1,
541 typename _Rep2, typename _Period2>
542 constexpr typename common_type<duration<_Rep1, _Period1>,
543 duration<_Rep2, _Period2>>::type
544 operator+(const duration<_Rep1, _Period1>& __lhs,
545 const duration<_Rep2, _Period2>& __rhs)
547 typedef duration<_Rep1, _Period1> __dur1;
548 typedef duration<_Rep2, _Period2> __dur2;
549 typedef typename common_type<__dur1,__dur2>::type __cd;
550 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
553 /// The difference between two durations.
554 template<typename _Rep1, typename _Period1,
555 typename _Rep2, typename _Period2>
556 constexpr typename common_type<duration<_Rep1, _Period1>,
557 duration<_Rep2, _Period2>>::type
558 operator-(const duration<_Rep1, _Period1>& __lhs,
559 const duration<_Rep2, _Period2>& __rhs)
561 typedef duration<_Rep1, _Period1> __dur1;
562 typedef duration<_Rep2, _Period2> __dur2;
563 typedef typename common_type<__dur1,__dur2>::type __cd;
564 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
569 /// @cond undocumented
571 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
572 // is implicitly convertible to it.
573 // _GLIBCXX_RESOLVE_LIB_DEFECTS
574 // 3050. Conversion specification problem in chrono::duration constructor
575 template<typename _Rep1, typename _Rep2,
576 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
577 using __common_rep_t = typename
578 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
582 /// @relates duration @{
584 /// Multiply a duration by a scalar value.
585 template<typename _Rep1, typename _Period, typename _Rep2>
586 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
587 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
589 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
591 return __cd(__cd(__d).count() * __s);
594 /// Multiply a duration by a scalar value.
595 template<typename _Rep1, typename _Rep2, typename _Period>
596 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
597 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
598 { return __d * __s; }
600 template<typename _Rep1, typename _Period, typename _Rep2>
602 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
603 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
605 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
607 return __cd(__cd(__d).count() / __s);
610 template<typename _Rep1, typename _Period1,
611 typename _Rep2, typename _Period2>
612 constexpr typename common_type<_Rep1, _Rep2>::type
613 operator/(const duration<_Rep1, _Period1>& __lhs,
614 const duration<_Rep2, _Period2>& __rhs)
616 typedef duration<_Rep1, _Period1> __dur1;
617 typedef duration<_Rep2, _Period2> __dur2;
618 typedef typename common_type<__dur1,__dur2>::type __cd;
619 return __cd(__lhs).count() / __cd(__rhs).count();
623 template<typename _Rep1, typename _Period, typename _Rep2>
625 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
626 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
628 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
630 return __cd(__cd(__d).count() % __s);
633 template<typename _Rep1, typename _Period1,
634 typename _Rep2, typename _Period2>
635 constexpr typename common_type<duration<_Rep1, _Period1>,
636 duration<_Rep2, _Period2>>::type
637 operator%(const duration<_Rep1, _Period1>& __lhs,
638 const duration<_Rep2, _Period2>& __rhs)
640 typedef duration<_Rep1, _Period1> __dur1;
641 typedef duration<_Rep2, _Period2> __dur2;
642 typedef typename common_type<__dur1,__dur2>::type __cd;
643 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
648 template<typename _Rep1, typename _Period1,
649 typename _Rep2, typename _Period2>
651 operator==(const duration<_Rep1, _Period1>& __lhs,
652 const duration<_Rep2, _Period2>& __rhs)
654 typedef duration<_Rep1, _Period1> __dur1;
655 typedef duration<_Rep2, _Period2> __dur2;
656 typedef typename common_type<__dur1,__dur2>::type __ct;
657 return __ct(__lhs).count() == __ct(__rhs).count();
660 template<typename _Rep1, typename _Period1,
661 typename _Rep2, typename _Period2>
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 __ct;
669 return __ct(__lhs).count() < __ct(__rhs).count();
672 #if __cpp_lib_three_way_comparison
673 template<typename _Rep1, typename _Period1,
674 typename _Rep2, typename _Period2>
675 requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
677 operator<=>(const duration<_Rep1, _Period1>& __lhs,
678 const duration<_Rep2, _Period2>& __rhs)
680 using __ct = common_type_t<duration<_Rep1, _Period1>,
681 duration<_Rep2, _Period2>>;
682 return __ct(__lhs).count() <=> __ct(__rhs).count();
685 template<typename _Rep1, typename _Period1,
686 typename _Rep2, typename _Period2>
688 operator!=(const duration<_Rep1, _Period1>& __lhs,
689 const duration<_Rep2, _Period2>& __rhs)
690 { return !(__lhs == __rhs); }
693 template<typename _Rep1, typename _Period1,
694 typename _Rep2, typename _Period2>
696 operator<=(const duration<_Rep1, _Period1>& __lhs,
697 const duration<_Rep2, _Period2>& __rhs)
698 { return !(__rhs < __lhs); }
700 template<typename _Rep1, typename _Period1,
701 typename _Rep2, typename _Period2>
703 operator>(const duration<_Rep1, _Period1>& __lhs,
704 const duration<_Rep2, _Period2>& __rhs)
705 { return __rhs < __lhs; }
707 template<typename _Rep1, typename _Period1,
708 typename _Rep2, typename _Period2>
710 operator>=(const duration<_Rep1, _Period1>& __lhs,
711 const duration<_Rep2, _Period2>& __rhs)
712 { return !(__lhs < __rhs); }
716 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
717 # define _GLIBCXX_CHRONO_INT64_T int64_t
718 #elif defined __INT64_TYPE__
719 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
721 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
722 "Representation type for nanoseconds must have at least 64 bits");
723 # define _GLIBCXX_CHRONO_INT64_T long long
727 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
730 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
733 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
736 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
739 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
742 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
744 #if __cplusplus > 201703L
746 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
749 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
752 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
755 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
758 #undef _GLIBCXX_CHRONO_INT64_T
761 template<typename _Clock, typename _Dur>
764 static_assert(__is_duration<_Dur>::value,
765 "duration must be a specialization of std::chrono::duration");
767 typedef _Clock clock;
768 typedef _Dur duration;
769 typedef typename duration::rep rep;
770 typedef typename duration::period period;
772 constexpr time_point() : __d(duration::zero())
775 constexpr explicit time_point(const duration& __dur)
780 template<typename _Dur2,
781 typename = _Require<is_convertible<_Dur2, _Dur>>>
782 constexpr time_point(const time_point<clock, _Dur2>& __t)
783 : __d(__t.time_since_epoch())
788 time_since_epoch() const
792 _GLIBCXX17_CONSTEXPR time_point&
793 operator+=(const duration& __dur)
799 _GLIBCXX17_CONSTEXPR time_point&
800 operator-=(const duration& __dur)
807 static constexpr time_point
809 { return time_point(duration::min()); }
811 static constexpr time_point
813 { return time_point(duration::max()); }
820 template<typename _ToDur, typename _Clock, typename _Dur>
821 constexpr typename enable_if<__is_duration<_ToDur>::value,
822 time_point<_Clock, _ToDur>>::type
823 time_point_cast(const time_point<_Clock, _Dur>& __t)
825 typedef time_point<_Clock, _ToDur> __time_point;
826 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
829 #if __cplusplus > 201402L
830 template<typename _ToDur, typename _Clock, typename _Dur>
832 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
833 floor(const time_point<_Clock, _Dur>& __tp)
835 return time_point<_Clock, _ToDur>{
836 chrono::floor<_ToDur>(__tp.time_since_epoch())};
839 template<typename _ToDur, typename _Clock, typename _Dur>
841 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
842 ceil(const time_point<_Clock, _Dur>& __tp)
844 return time_point<_Clock, _ToDur>{
845 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
848 template<typename _ToDur, typename _Clock, typename _Dur>
849 constexpr enable_if_t<
850 __and_<__is_duration<_ToDur>,
851 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
852 time_point<_Clock, _ToDur>>
853 round(const time_point<_Clock, _Dur>& __tp)
855 return time_point<_Clock, _ToDur>{
856 chrono::round<_ToDur>(__tp.time_since_epoch())};
860 /// @relates time_point @{
862 /// Adjust a time point forwards by the given duration.
863 template<typename _Clock, typename _Dur1,
864 typename _Rep2, typename _Period2>
865 constexpr time_point<_Clock,
866 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
867 operator+(const time_point<_Clock, _Dur1>& __lhs,
868 const duration<_Rep2, _Period2>& __rhs)
870 typedef duration<_Rep2, _Period2> __dur2;
871 typedef typename common_type<_Dur1,__dur2>::type __ct;
872 typedef time_point<_Clock, __ct> __time_point;
873 return __time_point(__lhs.time_since_epoch() + __rhs);
876 /// Adjust a time point forwards by the given duration.
877 template<typename _Rep1, typename _Period1,
878 typename _Clock, typename _Dur2>
879 constexpr time_point<_Clock,
880 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
881 operator+(const duration<_Rep1, _Period1>& __lhs,
882 const time_point<_Clock, _Dur2>& __rhs)
884 typedef duration<_Rep1, _Period1> __dur1;
885 typedef typename common_type<__dur1,_Dur2>::type __ct;
886 typedef time_point<_Clock, __ct> __time_point;
887 return __time_point(__rhs.time_since_epoch() + __lhs);
890 /// Adjust a time point backwards by the given duration.
891 template<typename _Clock, typename _Dur1,
892 typename _Rep2, typename _Period2>
893 constexpr time_point<_Clock,
894 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
895 operator-(const time_point<_Clock, _Dur1>& __lhs,
896 const duration<_Rep2, _Period2>& __rhs)
898 typedef duration<_Rep2, _Period2> __dur2;
899 typedef typename common_type<_Dur1,__dur2>::type __ct;
900 typedef time_point<_Clock, __ct> __time_point;
901 return __time_point(__lhs.time_since_epoch() -__rhs);
906 /// @relates time_point @{
908 /// The difference between two time points (as a duration)
909 template<typename _Clock, typename _Dur1, typename _Dur2>
910 constexpr typename common_type<_Dur1, _Dur2>::type
911 operator-(const time_point<_Clock, _Dur1>& __lhs,
912 const time_point<_Clock, _Dur2>& __rhs)
913 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
915 template<typename _Clock, typename _Dur1, typename _Dur2>
917 operator==(const time_point<_Clock, _Dur1>& __lhs,
918 const time_point<_Clock, _Dur2>& __rhs)
919 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
921 #if __cpp_lib_three_way_comparison
922 template<typename _Clock, typename _Dur1,
923 three_way_comparable_with<_Dur1> _Dur2>
925 operator<=>(const time_point<_Clock, _Dur1>& __lhs,
926 const time_point<_Clock, _Dur2>& __rhs)
927 { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
929 template<typename _Clock, typename _Dur1, typename _Dur2>
931 operator!=(const time_point<_Clock, _Dur1>& __lhs,
932 const time_point<_Clock, _Dur2>& __rhs)
933 { return !(__lhs == __rhs); }
936 template<typename _Clock, typename _Dur1, typename _Dur2>
938 operator<(const time_point<_Clock, _Dur1>& __lhs,
939 const time_point<_Clock, _Dur2>& __rhs)
940 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
942 template<typename _Clock, typename _Dur1, typename _Dur2>
944 operator<=(const time_point<_Clock, _Dur1>& __lhs,
945 const time_point<_Clock, _Dur2>& __rhs)
946 { return !(__rhs < __lhs); }
948 template<typename _Clock, typename _Dur1, typename _Dur2>
950 operator>(const time_point<_Clock, _Dur1>& __lhs,
951 const time_point<_Clock, _Dur2>& __rhs)
952 { return __rhs < __lhs; }
954 template<typename _Clock, typename _Dur1, typename _Dur2>
956 operator>=(const time_point<_Clock, _Dur1>& __lhs,
957 const time_point<_Clock, _Dur2>& __rhs)
958 { return !(__lhs < __rhs); }
964 // Why nanosecond resolution as the default?
965 // Why have std::system_clock always count in the highest
966 // resolution (ie nanoseconds), even if on some OSes the low 3
967 // or 9 decimal digits will be always zero? This allows later
968 // implementations to change the system_clock::now()
969 // implementation any time to provide better resolution without
970 // changing function signature or units.
972 // To support the (forward) evolution of the library's defined
973 // clocks, wrap inside inline namespace so that the current
974 // defintions of system_clock, steady_clock, and
975 // high_resolution_clock types are uniquely mangled. This way, new
976 // code can use the latests clocks, while the library can contain
977 // compatibility definitions for previous versions. At some
978 // point, when these clocks settle down, the inlined namespaces
979 // can be removed. XXX GLIBCXX_ABI Deprecated
980 inline namespace _V2 {
983 * @brief System clock.
985 * Time returned represents wall time from the system-wide clock.
990 typedef chrono::nanoseconds duration;
991 typedef duration::rep rep;
992 typedef duration::period period;
993 typedef chrono::time_point<system_clock, duration> time_point;
995 static_assert(system_clock::duration::min()
996 < system_clock::duration::zero(),
997 "a clock's minimum duration cannot be less than its epoch");
999 static constexpr bool is_steady = false;
1006 to_time_t(const time_point& __t) noexcept
1008 return std::time_t(duration_cast<chrono::seconds>
1009 (__t.time_since_epoch()).count());
1013 from_time_t(std::time_t __t) noexcept
1015 typedef chrono::time_point<system_clock, seconds> __from;
1016 return time_point_cast<system_clock::duration>
1017 (__from(chrono::seconds(__t)));
1023 * @brief Monotonic clock
1025 * Time returned has the property of only increasing at a uniform rate.
1030 typedef chrono::nanoseconds duration;
1031 typedef duration::rep rep;
1032 typedef duration::period period;
1033 typedef chrono::time_point<steady_clock, duration> time_point;
1035 static constexpr bool is_steady = true;
1043 * @brief Highest-resolution clock
1045 * This is the clock "with the shortest tick period." Alias to
1046 * std::system_clock until higher-than-nanosecond definitions
1050 using high_resolution_clock = system_clock;
1052 } // end inline namespace _V2
1054 #if __cplusplus > 201703L
1055 template<typename _Duration>
1056 using sys_time = time_point<system_clock, _Duration>;
1057 using sys_seconds = sys_time<seconds>;
1058 using sys_days = sys_time<days>;
1060 using file_clock = ::std::filesystem::__file_clock;
1062 template<typename _Duration>
1063 using file_time = time_point<file_clock, _Duration>;
1065 template<> struct is_clock<system_clock> : true_type { };
1066 template<> struct is_clock<steady_clock> : true_type { };
1067 template<> struct is_clock<file_clock> : true_type { };
1069 template<> inline constexpr bool is_clock_v<system_clock> = true;
1070 template<> inline constexpr bool is_clock_v<steady_clock> = true;
1071 template<> inline constexpr bool is_clock_v<file_clock> = true;
1074 template<typename _Duration>
1075 using local_time = time_point<local_t, _Duration>;
1076 using local_seconds = local_time<seconds>;
1077 using local_days = local_time<days>;
1081 } // namespace chrono
1083 #if __cplusplus > 201103L
1085 #define __cpp_lib_chrono_udls 201304
1087 inline namespace literals
1089 /** ISO C++ 2014 namespace for suffixes for duration literals.
1091 * These suffixes can be used to create `chrono::duration` values with
1092 * tick periods of hours, minutes, seconds, milliseconds, microseconds
1093 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
1094 * as `5s` after making the suffix visible in the current scope.
1095 * The suffixes can be made visible by a using-directive or
1096 * using-declaration such as:
1097 * - `using namespace std::chrono_literals;`
1098 * - `using namespace std::literals;`
1099 * - `using namespace std::chrono;`
1100 * - `using namespace std;`
1101 * - `using std::chrono_literals::operator""s;`
1103 * The result of these suffixes on an integer literal is one of the
1104 * standard typedefs such as `std::chrono::hours`.
1105 * The result on a floating-point literal is a duration type with the
1106 * specified tick period and an unspecified floating-point representation,
1107 * for example `1.5e2ms` might be equivalent to
1108 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
1112 inline namespace chrono_literals
1114 #pragma GCC diagnostic push
1115 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1116 /// @cond undocumented
1117 template<typename _Dur, char... _Digits>
1118 constexpr _Dur __check_overflow()
1120 using _Val = __parse_int::_Parse_int<_Digits...>;
1121 constexpr typename _Dur::rep __repval = _Val::value;
1122 static_assert(__repval >= 0 && __repval == _Val::value,
1123 "literal value cannot be represented by duration type");
1124 return _Dur(__repval);
1128 /// Literal suffix for durations representing non-integer hours
1129 constexpr chrono::duration<long double, ratio<3600,1>>
1130 operator""h(long double __hours)
1131 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
1133 /// Literal suffix for durations of type `std::chrono::hours`
1134 template <char... _Digits>
1135 constexpr chrono::hours
1137 { return __check_overflow<chrono::hours, _Digits...>(); }
1139 /// Literal suffix for durations representing non-integer minutes
1140 constexpr chrono::duration<long double, ratio<60,1>>
1141 operator""min(long double __mins)
1142 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
1144 /// Literal suffix for durations of type `std::chrono::minutes`
1145 template <char... _Digits>
1146 constexpr chrono::minutes
1148 { return __check_overflow<chrono::minutes, _Digits...>(); }
1150 /// Literal suffix for durations representing non-integer seconds
1151 constexpr chrono::duration<long double>
1152 operator""s(long double __secs)
1153 { return chrono::duration<long double>{__secs}; }
1155 /// Literal suffix for durations of type `std::chrono::seconds`
1156 template <char... _Digits>
1157 constexpr chrono::seconds
1159 { return __check_overflow<chrono::seconds, _Digits...>(); }
1161 /// Literal suffix for durations representing non-integer milliseconds
1162 constexpr chrono::duration<long double, milli>
1163 operator""ms(long double __msecs)
1164 { return chrono::duration<long double, milli>{__msecs}; }
1166 /// Literal suffix for durations of type `std::chrono::milliseconds`
1167 template <char... _Digits>
1168 constexpr chrono::milliseconds
1170 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1172 /// Literal suffix for durations representing non-integer microseconds
1173 constexpr chrono::duration<long double, micro>
1174 operator""us(long double __usecs)
1175 { return chrono::duration<long double, micro>{__usecs}; }
1177 /// Literal suffix for durations of type `std::chrono::microseconds`
1178 template <char... _Digits>
1179 constexpr chrono::microseconds
1181 { return __check_overflow<chrono::microseconds, _Digits...>(); }
1183 /// Literal suffix for durations representing non-integer nanoseconds
1184 constexpr chrono::duration<long double, nano>
1185 operator""ns(long double __nsecs)
1186 { return chrono::duration<long double, nano>{__nsecs}; }
1188 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1189 template <char... _Digits>
1190 constexpr chrono::nanoseconds
1192 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1194 #pragma GCC diagnostic pop
1195 } // inline namespace chrono_literals
1196 } // inline namespace literals
1200 using namespace literals::chrono_literals;
1201 } // namespace chrono
1203 #if __cplusplus >= 201703L
1204 namespace filesystem
1208 using duration = chrono::nanoseconds;
1209 using rep = duration::rep;
1210 using period = duration::period;
1211 using time_point = chrono::time_point<__file_clock>;
1212 static constexpr bool is_steady = false;
1216 { return _S_from_sys(chrono::system_clock::now()); }
1218 #if __cplusplus > 201703L
1219 template<typename _Dur>
1221 chrono::file_time<_Dur>
1222 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1223 { return _S_from_sys(__t); }
1225 // For internal use only
1226 template<typename _Dur>
1228 chrono::sys_time<_Dur>
1229 to_sys(const chrono::file_time<_Dur>& __t) noexcept
1230 { return _S_to_sys(__t); }
1234 using __sys_clock = chrono::system_clock;
1236 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
1237 // A signed 64-bit duration with nanosecond resolution gives roughly
1238 // +/- 292 years, which covers the 1901-2446 date range for ext4.
1239 static constexpr chrono::seconds _S_epoch_diff{6437664000};
1242 // For internal use only
1243 template<typename _Dur>
1245 chrono::time_point<__file_clock, _Dur>
1246 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1248 using __file_time = chrono::time_point<__file_clock, _Dur>;
1249 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1252 // For internal use only
1253 template<typename _Dur>
1255 chrono::time_point<__sys_clock, _Dur>
1256 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1258 using __sys_time = chrono::time_point<__sys_clock, _Dur>;
1259 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1262 } // namespace filesystem
1266 _GLIBCXX_END_NAMESPACE_VERSION
1271 #endif //_GLIBCXX_CHRONO