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
48 namespace std _GLIBCXX_VISIBILITY(default)
50 _GLIBCXX_BEGIN_NAMESPACE_VERSION
52 #if __cplusplus >= 201703L
53 namespace filesystem { struct __file_clock; };
57 * @defgroup chrono Time
60 * Classes and functions for time.
64 /** @namespace std::chrono
65 * @brief ISO C++ 2011 namespace for date and time utilities
69 template<typename _Rep, typename _Period = ratio<1>>
72 template<typename _Clock, typename _Dur = typename _Clock::duration>
76 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
78 /// @cond undocumented
80 template<typename _CT, typename _Period1, typename _Period2, typename = void>
81 struct __duration_common_type
84 template<typename _CT, typename _Period1, typename _Period2>
85 struct __duration_common_type<_CT, _Period1, _Period2,
86 __void_t<typename _CT::type>>
89 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
90 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
91 using __cr = typename _CT::type;
92 using __r = ratio<__gcd_num::value,
93 (_Period1::den / __gcd_den::value) * _Period2::den>;
96 using type = chrono::duration<__cr, __r>;
99 template<typename _Period1, typename _Period2>
100 struct __duration_common_type<__failure_type, _Period1, _Period2>
101 { typedef __failure_type type; };
105 /// Specialization of common_type for chrono::duration types.
106 /// @relates duration
107 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
108 struct common_type<chrono::duration<_Rep1, _Period1>,
109 chrono::duration<_Rep2, _Period2>>
110 : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
113 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
115 /// @cond undocumented
117 template<typename _CT, typename _Clock, typename = void>
118 struct __timepoint_common_type
121 template<typename _CT, typename _Clock>
122 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
124 using type = chrono::time_point<_Clock, typename _CT::type>;
129 /// Specialization of common_type for chrono::time_point types.
130 /// @relates time_point
131 template<typename _Clock, typename _Duration1, typename _Duration2>
132 struct common_type<chrono::time_point<_Clock, _Duration1>,
133 chrono::time_point<_Clock, _Duration2>>
134 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
141 /// @addtogroup chrono
144 /// @cond undocumented
146 // Primary template for duration_cast impl.
147 template<typename _ToDur, typename _CF, typename _CR,
148 bool _NumIsOne = false, bool _DenIsOne = false>
149 struct __duration_cast_impl
151 template<typename _Rep, typename _Period>
152 static constexpr _ToDur
153 __cast(const duration<_Rep, _Period>& __d)
155 typedef typename _ToDur::rep __to_rep;
156 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
157 * static_cast<_CR>(_CF::num)
158 / static_cast<_CR>(_CF::den)));
162 template<typename _ToDur, typename _CF, typename _CR>
163 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
165 template<typename _Rep, typename _Period>
166 static constexpr _ToDur
167 __cast(const duration<_Rep, _Period>& __d)
169 typedef typename _ToDur::rep __to_rep;
170 return _ToDur(static_cast<__to_rep>(__d.count()));
174 template<typename _ToDur, typename _CF, typename _CR>
175 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
177 template<typename _Rep, typename _Period>
178 static constexpr _ToDur
179 __cast(const duration<_Rep, _Period>& __d)
181 typedef typename _ToDur::rep __to_rep;
182 return _ToDur(static_cast<__to_rep>(
183 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
187 template<typename _ToDur, typename _CF, typename _CR>
188 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
190 template<typename _Rep, typename _Period>
191 static constexpr _ToDur
192 __cast(const duration<_Rep, _Period>& __d)
194 typedef typename _ToDur::rep __to_rep;
195 return _ToDur(static_cast<__to_rep>(
196 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
200 template<typename _Tp>
205 template<typename _Rep, typename _Period>
206 struct __is_duration<duration<_Rep, _Period>>
210 template<typename _Tp>
211 using __enable_if_is_duration
212 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
214 template<typename _Tp>
215 using __disable_if_is_duration
216 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
221 template<typename _ToDur, typename _Rep, typename _Period>
222 constexpr __enable_if_is_duration<_ToDur>
223 duration_cast(const duration<_Rep, _Period>& __d)
225 typedef typename _ToDur::period __to_period;
226 typedef typename _ToDur::rep __to_rep;
227 typedef ratio_divide<_Period, __to_period> __cf;
228 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
230 typedef __duration_cast_impl<_ToDur, __cf, __cr,
231 __cf::num == 1, __cf::den == 1> __dc;
232 return __dc::__cast(__d);
235 /// treat_as_floating_point
236 template<typename _Rep>
237 struct treat_as_floating_point
238 : is_floating_point<_Rep>
241 #if __cplusplus > 201402L
242 template <typename _Rep>
243 inline constexpr bool treat_as_floating_point_v =
244 treat_as_floating_point<_Rep>::value;
247 #if __cplusplus > 201703L
248 template<typename _Tp>
251 template<typename _Tp>
252 inline constexpr bool is_clock_v = is_clock<_Tp>::value;
254 #if __cpp_lib_concepts
255 template<typename _Tp>
256 struct is_clock : false_type
259 template<typename _Tp>
262 typename _Tp::period;
263 typename _Tp::duration;
264 typename _Tp::time_point::clock;
265 typename _Tp::time_point::duration;
266 { &_Tp::is_steady } -> same_as<const bool*>;
267 { _Tp::now() } -> same_as<typename _Tp::time_point>;
268 requires same_as<typename _Tp::duration,
269 duration<typename _Tp::rep, typename _Tp::period>>;
270 requires same_as<typename _Tp::time_point::duration,
271 typename _Tp::duration>;
273 struct is_clock<_Tp> : true_type
276 template<typename _Tp, typename = void>
277 struct __is_clock_impl : false_type
280 template<typename _Tp>
281 struct __is_clock_impl<_Tp,
282 void_t<typename _Tp::rep, typename _Tp::period,
283 typename _Tp::duration,
284 typename _Tp::time_point::duration,
285 decltype(_Tp::is_steady),
286 decltype(_Tp::now())>>
287 : __and_<is_same<typename _Tp::duration,
288 duration<typename _Tp::rep, typename _Tp::period>>,
289 is_same<typename _Tp::time_point::duration,
290 typename _Tp::duration>,
291 is_same<decltype(&_Tp::is_steady), const bool*>,
292 is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
295 template<typename _Tp>
296 struct is_clock : __is_clock_impl<_Tp>::type
301 #if __cplusplus >= 201703L
302 # define __cpp_lib_chrono 201611
304 template<typename _ToDur, typename _Rep, typename _Period>
305 constexpr __enable_if_is_duration<_ToDur>
306 floor(const duration<_Rep, _Period>& __d)
308 auto __to = chrono::duration_cast<_ToDur>(__d);
310 return __to - _ToDur{1};
314 template<typename _ToDur, typename _Rep, typename _Period>
315 constexpr __enable_if_is_duration<_ToDur>
316 ceil(const duration<_Rep, _Period>& __d)
318 auto __to = chrono::duration_cast<_ToDur>(__d);
320 return __to + _ToDur{1};
324 template <typename _ToDur, typename _Rep, typename _Period>
325 constexpr enable_if_t<
326 __and_<__is_duration<_ToDur>,
327 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
329 round(const duration<_Rep, _Period>& __d)
331 _ToDur __t0 = chrono::floor<_ToDur>(__d);
332 _ToDur __t1 = __t0 + _ToDur{1};
333 auto __diff0 = __d - __t0;
334 auto __diff1 = __t1 - __d;
335 if (__diff0 == __diff1)
337 if (__t0.count() & 1)
341 else if (__diff0 < __diff1)
346 template<typename _Rep, typename _Period>
348 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
349 abs(duration<_Rep, _Period> __d)
351 if (__d >= __d.zero())
358 template<typename _Rep>
359 struct duration_values
361 static constexpr _Rep
365 static constexpr _Rep
367 { return numeric_limits<_Rep>::max(); }
369 static constexpr _Rep
371 { return numeric_limits<_Rep>::lowest(); }
374 /// @cond undocumented
376 template<typename _Tp>
381 template<intmax_t _Num, intmax_t _Den>
382 struct __is_ratio<ratio<_Num, _Den>>
389 template<typename _Rep, typename _Period>
393 template<typename _Rep2>
394 using __is_float = treat_as_floating_point<_Rep2>;
396 // _Period2 is an exact multiple of _Period
397 template<typename _Period2>
399 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
404 typedef _Period period;
406 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
407 static_assert(__is_ratio<_Period>::value,
408 "period must be a specialization of ratio");
409 static_assert(_Period::num > 0, "period must be positive");
411 // 20.11.5.1 construction / copy / destroy
412 constexpr duration() = default;
414 duration(const duration&) = default;
416 // _GLIBCXX_RESOLVE_LIB_DEFECTS
417 // 3050. Conversion specification problem in chrono::duration
418 template<typename _Rep2, typename = _Require<
419 is_convertible<const _Rep2&, rep>,
420 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
421 constexpr explicit duration(const _Rep2& __rep)
422 : __r(static_cast<rep>(__rep)) { }
424 template<typename _Rep2, typename _Period2, typename = _Require<
425 __or_<__is_float<rep>,
426 __and_<__is_harmonic<_Period2>,
427 __not_<__is_float<_Rep2>>>>>>
428 constexpr duration(const duration<_Rep2, _Period2>& __d)
429 : __r(duration_cast<duration>(__d).count()) { }
431 ~duration() = default;
432 duration& operator=(const duration&) = default;
434 // 20.11.5.2 observer
439 // 20.11.5.3 arithmetic
446 { return duration(-__r); }
448 _GLIBCXX17_CONSTEXPR duration&
455 _GLIBCXX17_CONSTEXPR duration
457 { return duration(__r++); }
459 _GLIBCXX17_CONSTEXPR duration&
466 _GLIBCXX17_CONSTEXPR duration
468 { return duration(__r--); }
470 _GLIBCXX17_CONSTEXPR duration&
471 operator+=(const duration& __d)
477 _GLIBCXX17_CONSTEXPR duration&
478 operator-=(const duration& __d)
484 _GLIBCXX17_CONSTEXPR duration&
485 operator*=(const rep& __rhs)
491 _GLIBCXX17_CONSTEXPR duration&
492 operator/=(const rep& __rhs)
499 template<typename _Rep2 = rep>
501 typename enable_if<!treat_as_floating_point<_Rep2>::value,
503 operator%=(const rep& __rhs)
509 template<typename _Rep2 = rep>
511 typename enable_if<!treat_as_floating_point<_Rep2>::value,
513 operator%=(const duration& __d)
519 // 20.11.5.4 special values
520 static constexpr duration
522 { return duration(duration_values<rep>::zero()); }
524 static constexpr duration
526 { return duration(duration_values<rep>::min()); }
528 static constexpr duration
530 { return duration(duration_values<rep>::max()); }
536 /// @relates duration @{
538 /// The sum of two durations.
539 template<typename _Rep1, typename _Period1,
540 typename _Rep2, typename _Period2>
541 constexpr typename common_type<duration<_Rep1, _Period1>,
542 duration<_Rep2, _Period2>>::type
543 operator+(const duration<_Rep1, _Period1>& __lhs,
544 const duration<_Rep2, _Period2>& __rhs)
546 typedef duration<_Rep1, _Period1> __dur1;
547 typedef duration<_Rep2, _Period2> __dur2;
548 typedef typename common_type<__dur1,__dur2>::type __cd;
549 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
552 /// The difference between two durations.
553 template<typename _Rep1, typename _Period1,
554 typename _Rep2, typename _Period2>
555 constexpr typename common_type<duration<_Rep1, _Period1>,
556 duration<_Rep2, _Period2>>::type
557 operator-(const duration<_Rep1, _Period1>& __lhs,
558 const duration<_Rep2, _Period2>& __rhs)
560 typedef duration<_Rep1, _Period1> __dur1;
561 typedef duration<_Rep2, _Period2> __dur2;
562 typedef typename common_type<__dur1,__dur2>::type __cd;
563 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
568 /// @cond undocumented
570 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
571 // is implicitly convertible to it.
572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
573 // 3050. Conversion specification problem in chrono::duration constructor
574 template<typename _Rep1, typename _Rep2,
575 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
576 using __common_rep_t = typename
577 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
581 /// @relates duration @{
583 /// Multiply a duration by a scalar value.
584 template<typename _Rep1, typename _Period, typename _Rep2>
585 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
586 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
588 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
590 return __cd(__cd(__d).count() * __s);
593 /// Multiply a duration by a scalar value.
594 template<typename _Rep1, typename _Rep2, typename _Period>
595 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
596 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
597 { return __d * __s; }
599 template<typename _Rep1, typename _Period, typename _Rep2>
601 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
602 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
604 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
606 return __cd(__cd(__d).count() / __s);
609 template<typename _Rep1, typename _Period1,
610 typename _Rep2, typename _Period2>
611 constexpr typename common_type<_Rep1, _Rep2>::type
612 operator/(const duration<_Rep1, _Period1>& __lhs,
613 const duration<_Rep2, _Period2>& __rhs)
615 typedef duration<_Rep1, _Period1> __dur1;
616 typedef duration<_Rep2, _Period2> __dur2;
617 typedef typename common_type<__dur1,__dur2>::type __cd;
618 return __cd(__lhs).count() / __cd(__rhs).count();
622 template<typename _Rep1, typename _Period, typename _Rep2>
624 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
625 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
627 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
629 return __cd(__cd(__d).count() % __s);
632 template<typename _Rep1, typename _Period1,
633 typename _Rep2, typename _Period2>
634 constexpr typename common_type<duration<_Rep1, _Period1>,
635 duration<_Rep2, _Period2>>::type
636 operator%(const duration<_Rep1, _Period1>& __lhs,
637 const duration<_Rep2, _Period2>& __rhs)
639 typedef duration<_Rep1, _Period1> __dur1;
640 typedef duration<_Rep2, _Period2> __dur2;
641 typedef typename common_type<__dur1,__dur2>::type __cd;
642 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
647 template<typename _Rep1, typename _Period1,
648 typename _Rep2, typename _Period2>
650 operator==(const duration<_Rep1, _Period1>& __lhs,
651 const duration<_Rep2, _Period2>& __rhs)
653 typedef duration<_Rep1, _Period1> __dur1;
654 typedef duration<_Rep2, _Period2> __dur2;
655 typedef typename common_type<__dur1,__dur2>::type __ct;
656 return __ct(__lhs).count() == __ct(__rhs).count();
659 template<typename _Rep1, typename _Period1,
660 typename _Rep2, typename _Period2>
662 operator<(const duration<_Rep1, _Period1>& __lhs,
663 const duration<_Rep2, _Period2>& __rhs)
665 typedef duration<_Rep1, _Period1> __dur1;
666 typedef duration<_Rep2, _Period2> __dur2;
667 typedef typename common_type<__dur1,__dur2>::type __ct;
668 return __ct(__lhs).count() < __ct(__rhs).count();
671 template<typename _Rep1, typename _Period1,
672 typename _Rep2, typename _Period2>
674 operator!=(const duration<_Rep1, _Period1>& __lhs,
675 const duration<_Rep2, _Period2>& __rhs)
676 { return !(__lhs == __rhs); }
678 template<typename _Rep1, typename _Period1,
679 typename _Rep2, typename _Period2>
681 operator<=(const duration<_Rep1, _Period1>& __lhs,
682 const duration<_Rep2, _Period2>& __rhs)
683 { return !(__rhs < __lhs); }
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 __rhs < __lhs; }
692 template<typename _Rep1, typename _Period1,
693 typename _Rep2, typename _Period2>
695 operator>=(const duration<_Rep1, _Period1>& __lhs,
696 const duration<_Rep2, _Period2>& __rhs)
697 { return !(__lhs < __rhs); }
701 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
702 # define _GLIBCXX_CHRONO_INT64_T int64_t
703 #elif defined __INT64_TYPE__
704 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
706 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
707 "Representation type for nanoseconds must have at least 64 bits");
708 # define _GLIBCXX_CHRONO_INT64_T long long
712 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
715 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
718 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
721 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
724 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
727 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
729 #if __cplusplus > 201703L
731 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
734 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
737 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
740 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
743 #undef _GLIBCXX_CHRONO_INT64_T
746 template<typename _Clock, typename _Dur>
749 static_assert(__is_duration<_Dur>::value,
750 "duration must be a specialization of std::chrono::duration");
752 typedef _Clock clock;
753 typedef _Dur duration;
754 typedef typename duration::rep rep;
755 typedef typename duration::period period;
757 constexpr time_point() : __d(duration::zero())
760 constexpr explicit time_point(const duration& __dur)
765 template<typename _Dur2,
766 typename = _Require<is_convertible<_Dur2, _Dur>>>
767 constexpr time_point(const time_point<clock, _Dur2>& __t)
768 : __d(__t.time_since_epoch())
773 time_since_epoch() const
777 _GLIBCXX17_CONSTEXPR time_point&
778 operator+=(const duration& __dur)
784 _GLIBCXX17_CONSTEXPR time_point&
785 operator-=(const duration& __dur)
792 static constexpr time_point
794 { return time_point(duration::min()); }
796 static constexpr time_point
798 { return time_point(duration::max()); }
805 template<typename _ToDur, typename _Clock, typename _Dur>
806 constexpr typename enable_if<__is_duration<_ToDur>::value,
807 time_point<_Clock, _ToDur>>::type
808 time_point_cast(const time_point<_Clock, _Dur>& __t)
810 typedef time_point<_Clock, _ToDur> __time_point;
811 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
814 #if __cplusplus > 201402L
815 template<typename _ToDur, typename _Clock, typename _Dur>
817 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
818 floor(const time_point<_Clock, _Dur>& __tp)
820 return time_point<_Clock, _ToDur>{
821 chrono::floor<_ToDur>(__tp.time_since_epoch())};
824 template<typename _ToDur, typename _Clock, typename _Dur>
826 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
827 ceil(const time_point<_Clock, _Dur>& __tp)
829 return time_point<_Clock, _ToDur>{
830 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
833 template<typename _ToDur, typename _Clock, typename _Dur>
834 constexpr enable_if_t<
835 __and_<__is_duration<_ToDur>,
836 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
837 time_point<_Clock, _ToDur>>
838 round(const time_point<_Clock, _Dur>& __tp)
840 return time_point<_Clock, _ToDur>{
841 chrono::round<_ToDur>(__tp.time_since_epoch())};
845 /// @relates time_point @{
847 /// Adjust a time point forwards by the given duration.
848 template<typename _Clock, typename _Dur1,
849 typename _Rep2, typename _Period2>
850 constexpr time_point<_Clock,
851 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
852 operator+(const time_point<_Clock, _Dur1>& __lhs,
853 const duration<_Rep2, _Period2>& __rhs)
855 typedef duration<_Rep2, _Period2> __dur2;
856 typedef typename common_type<_Dur1,__dur2>::type __ct;
857 typedef time_point<_Clock, __ct> __time_point;
858 return __time_point(__lhs.time_since_epoch() + __rhs);
861 /// Adjust a time point forwards by the given duration.
862 template<typename _Rep1, typename _Period1,
863 typename _Clock, typename _Dur2>
864 constexpr time_point<_Clock,
865 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
866 operator+(const duration<_Rep1, _Period1>& __lhs,
867 const time_point<_Clock, _Dur2>& __rhs)
869 typedef duration<_Rep1, _Period1> __dur1;
870 typedef typename common_type<__dur1,_Dur2>::type __ct;
871 typedef time_point<_Clock, __ct> __time_point;
872 return __time_point(__rhs.time_since_epoch() + __lhs);
875 /// Adjust a time point backwards by the given duration.
876 template<typename _Clock, typename _Dur1,
877 typename _Rep2, typename _Period2>
878 constexpr time_point<_Clock,
879 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
880 operator-(const time_point<_Clock, _Dur1>& __lhs,
881 const duration<_Rep2, _Period2>& __rhs)
883 typedef duration<_Rep2, _Period2> __dur2;
884 typedef typename common_type<_Dur1,__dur2>::type __ct;
885 typedef time_point<_Clock, __ct> __time_point;
886 return __time_point(__lhs.time_since_epoch() -__rhs);
891 /// @relates time_point @{
893 /// The difference between two time points (as a duration)
894 template<typename _Clock, typename _Dur1, typename _Dur2>
895 constexpr typename common_type<_Dur1, _Dur2>::type
896 operator-(const time_point<_Clock, _Dur1>& __lhs,
897 const time_point<_Clock, _Dur2>& __rhs)
898 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
900 template<typename _Clock, typename _Dur1, typename _Dur2>
902 operator==(const time_point<_Clock, _Dur1>& __lhs,
903 const time_point<_Clock, _Dur2>& __rhs)
904 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
906 template<typename _Clock, typename _Dur1, typename _Dur2>
908 operator!=(const time_point<_Clock, _Dur1>& __lhs,
909 const time_point<_Clock, _Dur2>& __rhs)
910 { return !(__lhs == __rhs); }
912 template<typename _Clock, typename _Dur1, typename _Dur2>
914 operator<(const time_point<_Clock, _Dur1>& __lhs,
915 const time_point<_Clock, _Dur2>& __rhs)
916 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
918 template<typename _Clock, typename _Dur1, typename _Dur2>
920 operator<=(const time_point<_Clock, _Dur1>& __lhs,
921 const time_point<_Clock, _Dur2>& __rhs)
922 { return !(__rhs < __lhs); }
924 template<typename _Clock, typename _Dur1, typename _Dur2>
926 operator>(const time_point<_Clock, _Dur1>& __lhs,
927 const time_point<_Clock, _Dur2>& __rhs)
928 { return __rhs < __lhs; }
930 template<typename _Clock, typename _Dur1, typename _Dur2>
932 operator>=(const time_point<_Clock, _Dur1>& __lhs,
933 const time_point<_Clock, _Dur2>& __rhs)
934 { return !(__lhs < __rhs); }
940 // Why nanosecond resolution as the default?
941 // Why have std::system_clock always count in the highest
942 // resolution (ie nanoseconds), even if on some OSes the low 3
943 // or 9 decimal digits will be always zero? This allows later
944 // implementations to change the system_clock::now()
945 // implementation any time to provide better resolution without
946 // changing function signature or units.
948 // To support the (forward) evolution of the library's defined
949 // clocks, wrap inside inline namespace so that the current
950 // defintions of system_clock, steady_clock, and
951 // high_resolution_clock types are uniquely mangled. This way, new
952 // code can use the latests clocks, while the library can contain
953 // compatibility definitions for previous versions. At some
954 // point, when these clocks settle down, the inlined namespaces
955 // can be removed. XXX GLIBCXX_ABI Deprecated
956 inline namespace _V2 {
959 * @brief System clock.
961 * Time returned represents wall time from the system-wide clock.
966 typedef chrono::nanoseconds duration;
967 typedef duration::rep rep;
968 typedef duration::period period;
969 typedef chrono::time_point<system_clock, duration> time_point;
971 static_assert(system_clock::duration::min()
972 < system_clock::duration::zero(),
973 "a clock's minimum duration cannot be less than its epoch");
975 static constexpr bool is_steady = false;
982 to_time_t(const time_point& __t) noexcept
984 return std::time_t(duration_cast<chrono::seconds>
985 (__t.time_since_epoch()).count());
989 from_time_t(std::time_t __t) noexcept
991 typedef chrono::time_point<system_clock, seconds> __from;
992 return time_point_cast<system_clock::duration>
993 (__from(chrono::seconds(__t)));
999 * @brief Monotonic clock
1001 * Time returned has the property of only increasing at a uniform rate.
1006 typedef chrono::nanoseconds duration;
1007 typedef duration::rep rep;
1008 typedef duration::period period;
1009 typedef chrono::time_point<steady_clock, duration> time_point;
1011 static constexpr bool is_steady = true;
1019 * @brief Highest-resolution clock
1021 * This is the clock "with the shortest tick period." Alias to
1022 * std::system_clock until higher-than-nanosecond definitions
1026 using high_resolution_clock = system_clock;
1028 } // end inline namespace _V2
1030 #if __cplusplus > 201703L
1031 template<typename _Duration>
1032 using sys_time = time_point<system_clock, _Duration>;
1033 using sys_seconds = sys_time<seconds>;
1034 using sys_days = sys_time<days>;
1036 using file_clock = ::std::filesystem::__file_clock;
1038 template<typename _Duration>
1039 using file_time = time_point<file_clock, _Duration>;
1041 template<> struct is_clock<system_clock> : true_type { };
1042 template<> struct is_clock<steady_clock> : true_type { };
1043 template<> struct is_clock<file_clock> : true_type { };
1045 template<> inline constexpr bool is_clock_v<system_clock> = true;
1046 template<> inline constexpr bool is_clock_v<steady_clock> = true;
1047 template<> inline constexpr bool is_clock_v<file_clock> = true;
1050 template<typename _Duration>
1051 using local_time = time_point<local_t, _Duration>;
1052 using local_seconds = local_time<seconds>;
1053 using local_days = local_time<days>;
1057 } // namespace chrono
1059 #if __cplusplus > 201103L
1061 #define __cpp_lib_chrono_udls 201304
1063 inline namespace literals
1065 /** ISO C++ 2014 namespace for suffixes for duration literals.
1067 * These suffixes can be used to create `chrono::duration` values with
1068 * tick periods of hours, minutes, seconds, milliseconds, microseconds
1069 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
1070 * as `5s` after making the suffix visible in the current scope.
1071 * The suffixes can be made visible by a using-directive or
1072 * using-declaration such as:
1073 * - `using namespace std::chrono_literals;`
1074 * - `using namespace std::literals;`
1075 * - `using namespace std::chrono;`
1076 * - `using namespace std;`
1077 * - `using std::chrono_literals::operator""s;`
1079 * The result of these suffixes on an integer literal is one of the
1080 * standard typedefs such as `std::chrono::hours`.
1081 * The result on a floating-point literal is a duration type with the
1082 * specified tick period and an unspecified floating-point representation,
1083 * for example `1.5e2ms` might be equivalent to
1084 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
1088 inline namespace chrono_literals
1090 #pragma GCC diagnostic push
1091 #pragma GCC diagnostic ignored "-Wliteral-suffix"
1092 /// @cond undocumented
1093 template<typename _Dur, char... _Digits>
1094 constexpr _Dur __check_overflow()
1096 using _Val = __parse_int::_Parse_int<_Digits...>;
1097 constexpr typename _Dur::rep __repval = _Val::value;
1098 static_assert(__repval >= 0 && __repval == _Val::value,
1099 "literal value cannot be represented by duration type");
1100 return _Dur(__repval);
1104 /// Literal suffix for durations representing non-integer hours
1105 constexpr chrono::duration<long double, ratio<3600,1>>
1106 operator""h(long double __hours)
1107 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
1109 /// Literal suffix for durations of type `std::chrono::hours`
1110 template <char... _Digits>
1111 constexpr chrono::hours
1113 { return __check_overflow<chrono::hours, _Digits...>(); }
1115 /// Literal suffix for durations representing non-integer minutes
1116 constexpr chrono::duration<long double, ratio<60,1>>
1117 operator""min(long double __mins)
1118 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
1120 /// Literal suffix for durations of type `std::chrono::minutes`
1121 template <char... _Digits>
1122 constexpr chrono::minutes
1124 { return __check_overflow<chrono::minutes, _Digits...>(); }
1126 /// Literal suffix for durations representing non-integer seconds
1127 constexpr chrono::duration<long double>
1128 operator""s(long double __secs)
1129 { return chrono::duration<long double>{__secs}; }
1131 /// Literal suffix for durations of type `std::chrono::seconds`
1132 template <char... _Digits>
1133 constexpr chrono::seconds
1135 { return __check_overflow<chrono::seconds, _Digits...>(); }
1137 /// Literal suffix for durations representing non-integer milliseconds
1138 constexpr chrono::duration<long double, milli>
1139 operator""ms(long double __msecs)
1140 { return chrono::duration<long double, milli>{__msecs}; }
1142 /// Literal suffix for durations of type `std::chrono::milliseconds`
1143 template <char... _Digits>
1144 constexpr chrono::milliseconds
1146 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1148 /// Literal suffix for durations representing non-integer microseconds
1149 constexpr chrono::duration<long double, micro>
1150 operator""us(long double __usecs)
1151 { return chrono::duration<long double, micro>{__usecs}; }
1153 /// Literal suffix for durations of type `std::chrono::microseconds`
1154 template <char... _Digits>
1155 constexpr chrono::microseconds
1157 { return __check_overflow<chrono::microseconds, _Digits...>(); }
1159 /// Literal suffix for durations representing non-integer nanoseconds
1160 constexpr chrono::duration<long double, nano>
1161 operator""ns(long double __nsecs)
1162 { return chrono::duration<long double, nano>{__nsecs}; }
1164 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1165 template <char... _Digits>
1166 constexpr chrono::nanoseconds
1168 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1170 #pragma GCC diagnostic pop
1171 } // inline namespace chrono_literals
1172 } // inline namespace literals
1176 using namespace literals::chrono_literals;
1177 } // namespace chrono
1179 #if __cplusplus >= 201703L
1180 namespace filesystem
1184 using duration = chrono::nanoseconds;
1185 using rep = duration::rep;
1186 using period = duration::period;
1187 using time_point = chrono::time_point<__file_clock>;
1188 static constexpr bool is_steady = false;
1192 { return _S_from_sys(chrono::system_clock::now()); }
1194 #if __cplusplus > 201703L
1195 template<typename _Dur>
1197 chrono::file_time<_Dur>
1198 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1199 { return _S_from_sys(__t); }
1201 // For internal use only
1202 template<typename _Dur>
1204 chrono::sys_time<_Dur>
1205 to_sys(const chrono::file_time<_Dur>& __t) noexcept
1206 { return _S_to_sys(__t); }
1210 using __sys_clock = chrono::system_clock;
1212 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
1213 // A signed 64-bit duration with nanosecond resolution gives roughly
1214 // +/- 292 years, which covers the 1901-2446 date range for ext4.
1215 static constexpr chrono::seconds _S_epoch_diff{6437664000};
1218 // For internal use only
1219 template<typename _Dur>
1221 chrono::time_point<__file_clock, _Dur>
1222 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1224 using __file_time = chrono::time_point<__file_clock, _Dur>;
1225 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1228 // For internal use only
1229 template<typename _Dur>
1231 chrono::time_point<__sys_clock, _Dur>
1232 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1234 using __sys_time = chrono::time_point<__sys_clock, _Dur>;
1235 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1238 } // namespace filesystem
1242 _GLIBCXX_END_NAMESPACE_VERSION
1247 #endif //_GLIBCXX_CHRONO