30 #ifndef _GLIBCXX_CHRONO
31 #define _GLIBCXX_CHRONO 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 template<
typename _Rep,
typename _Period = ratio<1>>
65 template<
typename _Clock,
typename _Dur =
typename _Clock::duration>
73 template<
typename _CT,
typename _Period1,
typename _Period2,
typename =
void>
74 struct __duration_common_type
77 template<
typename _CT,
typename _Period1,
typename _Period2>
78 struct __duration_common_type<_CT, _Period1, _Period2,
79 __void_t<typename _CT::type>>
82 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
83 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
84 using __cr =
typename _CT::type;
85 using __r =
ratio<__gcd_num::value,
86 (_Period1::den / __gcd_den::value) * _Period2::den>;
92 template<
typename _Period1,
typename _Period2>
93 struct __duration_common_type<__failure_type, _Period1, _Period2>
94 {
typedef __failure_type type; };
100 template<
typename _Rep1,
typename _Period1,
typename _Rep2,
typename _Period2>
103 : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
110 template<
typename _CT,
typename _Clock,
typename =
void>
111 struct __timepoint_common_type
114 template<
typename _CT,
typename _Clock>
115 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
117 using type = chrono::time_point<_Clock, typename _CT::type>;
124 template<
typename _Clock,
typename _Duration1,
typename _Duration2>
127 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
140 template<
typename _ToDur,
typename _CF,
typename _CR,
141 bool _NumIsOne =
false,
bool _DenIsOne =
false>
142 struct __duration_cast_impl
144 template<
typename _Rep,
typename _Period>
145 static constexpr _ToDur
146 __cast(
const duration<_Rep, _Period>& __d)
148 typedef typename _ToDur::rep __to_rep;
149 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
150 * static_cast<_CR>(_CF::num)
151 / static_cast<_CR>(_CF::den)));
155 template<
typename _ToDur,
typename _CF,
typename _CR>
156 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
158 template<
typename _Rep,
typename _Period>
159 static constexpr _ToDur
160 __cast(
const duration<_Rep, _Period>& __d)
162 typedef typename _ToDur::rep __to_rep;
163 return _ToDur(static_cast<__to_rep>(__d.count()));
167 template<
typename _ToDur,
typename _CF,
typename _CR>
168 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
170 template<
typename _Rep,
typename _Period>
171 static constexpr _ToDur
172 __cast(
const duration<_Rep, _Period>& __d)
174 typedef typename _ToDur::rep __to_rep;
175 return _ToDur(static_cast<__to_rep>(
176 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
180 template<
typename _ToDur,
typename _CF,
typename _CR>
181 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
183 template<
typename _Rep,
typename _Period>
184 static constexpr _ToDur
185 __cast(
const duration<_Rep, _Period>& __d)
187 typedef typename _ToDur::rep __to_rep;
188 return _ToDur(static_cast<__to_rep>(
189 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
193 template<
typename _Tp>
198 template<
typename _Rep,
typename _Period>
199 struct __is_duration<duration<_Rep, _Period>>
203 template<
typename _Tp>
204 using __enable_if_is_duration
205 =
typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
207 template<
typename _Tp>
208 using __disable_if_is_duration
209 =
typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
214 template<
typename _ToDur,
typename _Rep,
typename _Period>
215 constexpr __enable_if_is_duration<_ToDur>
218 typedef typename _ToDur::period __to_period;
219 typedef typename _ToDur::rep __to_rep;
223 typedef __duration_cast_impl<_ToDur, __cf, __cr,
224 __cf::num == 1, __cf::den == 1> __dc;
225 return __dc::__cast(__d);
229 template<
typename _Rep>
234 #if __cplusplus > 201402L
235 template <
typename _Rep>
236 inline constexpr
bool treat_as_floating_point_v =
240 #if __cplusplus >= 201703L
241 # define __cpp_lib_chrono 201611
243 template<
typename _ToDur,
typename _Rep,
typename _Period>
244 constexpr __enable_if_is_duration<_ToDur>
247 auto __to = chrono::duration_cast<_ToDur>(__d);
249 return __to - _ToDur{1};
253 template<
typename _ToDur,
typename _Rep,
typename _Period>
254 constexpr __enable_if_is_duration<_ToDur>
255 ceil(
const duration<_Rep, _Period>& __d)
257 auto __to = chrono::duration_cast<_ToDur>(__d);
259 return __to + _ToDur{1};
263 template <
typename _ToDur,
typename _Rep,
typename _Period>
265 __and_<__is_duration<_ToDur>,
266 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
268 round(
const duration<_Rep, _Period>& __d)
270 _ToDur __t0 = chrono::floor<_ToDur>(__d);
271 _ToDur __t1 = __t0 + _ToDur{1};
272 auto __diff0 = __d - __t0;
273 auto __diff1 = __t1 - __d;
274 if (__diff0 == __diff1)
276 if (__t0.count() & 1)
280 else if (__diff0 < __diff1)
285 template<
typename _Rep,
typename _Period>
287 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
288 abs(duration<_Rep, _Period> __d)
290 if (__d >= __d.zero())
297 template<
typename _Rep>
300 static constexpr _Rep
304 static constexpr _Rep
308 static constexpr _Rep
315 template<
typename _Tp>
320 template<
intmax_t _Num,
intmax_t _Den>
321 struct __is_ratio<
ratio<_Num, _Den>>
328 template<
typename _Rep,
typename _Period>
332 template<
typename _Rep2>
333 using __is_float = treat_as_floating_point<_Rep2>;
336 template<
typename _Period2>
338 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
343 typedef _Period period;
345 static_assert(!__is_duration<_Rep>::value,
"rep cannot be a duration");
346 static_assert(__is_ratio<_Period>::value,
347 "period must be a specialization of ratio");
348 static_assert(_Period::num > 0,
"period must be positive");
351 constexpr duration() =
default;
353 duration(
const duration&) =
default;
357 template<
typename _Rep2,
typename = _Require<
358 is_convertible<const _Rep2&, rep>,
359 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
360 constexpr
explicit duration(
const _Rep2& __rep)
361 : __r(static_cast<rep>(__rep)) { }
363 template<
typename _Rep2,
typename _Period2,
typename = _Require<
364 __or_<__is_float<rep>,
365 __and_<__is_harmonic<_Period2>,
366 __not_<__is_float<_Rep2>>>>>>
367 constexpr duration(
const duration<_Rep2, _Period2>& __d)
370 ~duration() =
default;
371 duration& operator=(
const duration&) =
default;
385 {
return duration(-__r); }
387 _GLIBCXX17_CONSTEXPR duration&
394 _GLIBCXX17_CONSTEXPR duration
396 {
return duration(__r++); }
398 _GLIBCXX17_CONSTEXPR duration&
405 _GLIBCXX17_CONSTEXPR duration
407 {
return duration(__r--); }
409 _GLIBCXX17_CONSTEXPR duration&
410 operator+=(
const duration& __d)
416 _GLIBCXX17_CONSTEXPR duration&
417 operator-=(
const duration& __d)
423 _GLIBCXX17_CONSTEXPR duration&
424 operator*=(
const rep& __rhs)
430 _GLIBCXX17_CONSTEXPR duration&
431 operator/=(
const rep& __rhs)
438 template<
typename _Rep2 = rep>
440 typename enable_if<!treat_as_floating_point<_Rep2>::value,
442 operator%=(
const rep& __rhs)
448 template<
typename _Rep2 = rep>
450 typename enable_if<!treat_as_floating_point<_Rep2>::value,
452 operator%=(
const duration& __d)
459 static constexpr duration
461 {
return duration(duration_values<rep>::zero()); }
463 static constexpr duration
465 {
return duration(duration_values<rep>::min()); }
467 static constexpr duration
469 {
return duration(duration_values<rep>::max()); }
478 template<
typename _Rep1,
typename _Period1,
479 typename _Rep2,
typename _Period2>
480 constexpr
typename common_type<duration<_Rep1, _Period1>,
481 duration<_Rep2, _Period2>>::type
488 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
492 template<
typename _Rep1,
typename _Period1,
493 typename _Rep2,
typename _Period2>
502 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
513 template<
typename _Rep1,
typename _Rep2,
515 using __common_rep_t =
typename
523 template<
typename _Rep1,
typename _Period,
typename _Rep2>
529 return __cd(__cd(__d).count() * __s);
533 template<
typename _Rep1,
typename _Rep2,
typename _Period>
536 {
return __d * __s; }
538 template<
typename _Rep1,
typename _Period,
typename _Rep2>
545 return __cd(__cd(__d).count() / __s);
548 template<
typename _Rep1,
typename _Period1,
549 typename _Rep2,
typename _Period2>
551 operator/(
const duration<_Rep1, _Period1>& __lhs,
552 const duration<_Rep2, _Period2>& __rhs)
554 typedef duration<_Rep1, _Period1> __dur1;
555 typedef duration<_Rep2, _Period2> __dur2;
557 return __cd(__lhs).count() / __cd(__rhs).count();
561 template<
typename _Rep1,
typename _Period,
typename _Rep2>
563 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
564 operator%(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
566 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
568 return __cd(__cd(__d).count() % __s);
571 template<
typename _Rep1,
typename _Period1,
572 typename _Rep2,
typename _Period2>
573 constexpr
typename common_type<duration<_Rep1, _Period1>,
574 duration<_Rep2, _Period2>>::type
575 operator%(
const duration<_Rep1, _Period1>& __lhs,
576 const duration<_Rep2, _Period2>& __rhs)
578 typedef duration<_Rep1, _Period1> __dur1;
579 typedef duration<_Rep2, _Period2> __dur2;
580 typedef typename common_type<__dur1,__dur2>::type __cd;
581 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
586 template<
typename _Rep1,
typename _Period1,
587 typename _Rep2,
typename _Period2>
589 operator==(
const duration<_Rep1, _Period1>& __lhs,
590 const duration<_Rep2, _Period2>& __rhs)
592 typedef duration<_Rep1, _Period1> __dur1;
593 typedef duration<_Rep2, _Period2> __dur2;
594 typedef typename common_type<__dur1,__dur2>::type __ct;
595 return __ct(__lhs).count() == __ct(__rhs).count();
598 template<
typename _Rep1,
typename _Period1,
599 typename _Rep2,
typename _Period2>
601 operator<(
const duration<_Rep1, _Period1>& __lhs,
602 const duration<_Rep2, _Period2>& __rhs)
604 typedef duration<_Rep1, _Period1> __dur1;
605 typedef duration<_Rep2, _Period2> __dur2;
606 typedef typename common_type<__dur1,__dur2>::type __ct;
607 return __ct(__lhs).count() < __ct(__rhs).count();
610 template<
typename _Rep1,
typename _Period1,
611 typename _Rep2,
typename _Period2>
613 operator!=(
const duration<_Rep1, _Period1>& __lhs,
614 const duration<_Rep2, _Period2>& __rhs)
615 {
return !(__lhs == __rhs); }
617 template<
typename _Rep1,
typename _Period1,
618 typename _Rep2,
typename _Period2>
620 operator<=(
const duration<_Rep1, _Period1>& __lhs,
621 const duration<_Rep2, _Period2>& __rhs)
622 {
return !(__rhs < __lhs); }
624 template<
typename _Rep1,
typename _Period1,
625 typename _Rep2,
typename _Period2>
627 operator>(
const duration<_Rep1, _Period1>& __lhs,
628 const duration<_Rep2, _Period2>& __rhs)
629 {
return __rhs < __lhs; }
631 template<
typename _Rep1,
typename _Period1,
632 typename _Rep2,
typename _Period2>
634 operator>=(
const duration<_Rep1, _Period1>& __lhs,
635 const duration<_Rep2, _Period2>& __rhs)
636 {
return !(__lhs < __rhs); }
640 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
641 # define _GLIBCXX_CHRONO_INT64_T int64_t
642 #elif defined __INT64_TYPE__
643 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
646 "Representation type for nanoseconds must have at least 64 bits");
647 # define _GLIBCXX_CHRONO_INT64_T long long
668 #undef _GLIBCXX_CHRONO_INT64_T
671 template<
typename _Clock,
typename _Dur>
674 typedef _Clock clock;
676 typedef typename duration::rep rep;
677 typedef typename duration::period period;
679 constexpr
time_point() : __d(duration::zero())
687 template<
typename _Dur2,
688 typename = _Require<is_convertible<_Dur2, _Dur>>>
690 : __d(__t.time_since_epoch())
695 time_since_epoch()
const
727 template<
typename _ToDur,
typename _Clock,
typename _Dur>
729 time_point<_Clock, _ToDur>>::type
733 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
736 #if __cplusplus > 201402L
737 template<
typename _ToDur,
typename _Clock,
typename _Dur>
740 floor(
const time_point<_Clock, _Dur>& __tp)
742 return time_point<_Clock, _ToDur>{
743 chrono::floor<_ToDur>(__tp.time_since_epoch())};
746 template<
typename _ToDur,
typename _Clock,
typename _Dur>
748 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
749 ceil(
const time_point<_Clock, _Dur>& __tp)
751 return time_point<_Clock, _ToDur>{
752 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
755 template<
typename _ToDur,
typename _Clock,
typename _Dur>
757 __and_<__is_duration<_ToDur>,
758 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
759 time_point<_Clock, _ToDur>>
760 round(
const time_point<_Clock, _Dur>& __tp)
762 return time_point<_Clock, _ToDur>{
763 chrono::round<_ToDur>(__tp.time_since_epoch())};
770 template<
typename _Clock,
typename _Dur1,
771 typename _Rep2,
typename _Period2>
772 constexpr time_point<_Clock,
773 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
775 const duration<_Rep2, _Period2>& __rhs)
777 typedef duration<_Rep2, _Period2> __dur2;
780 return __time_point(__lhs.time_since_epoch() + __rhs);
784 template<
typename _Rep1,
typename _Period1,
785 typename _Clock,
typename _Dur2>
791 typedef duration<_Rep1, _Period1> __dur1;
794 return __time_point(__rhs.time_since_epoch() + __lhs);
798 template<
typename _Clock,
typename _Dur1,
799 typename _Rep2,
typename _Period2>
803 const duration<_Rep2, _Period2>& __rhs)
805 typedef duration<_Rep2, _Period2> __dur2;
808 return __time_point(__lhs.time_since_epoch() -__rhs);
816 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
820 {
return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
822 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
826 {
return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
828 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
830 operator!=(
const time_point<_Clock, _Dur1>& __lhs,
831 const time_point<_Clock, _Dur2>& __rhs)
832 {
return !(__lhs == __rhs); }
834 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
836 operator<(
const time_point<_Clock, _Dur1>& __lhs,
837 const time_point<_Clock, _Dur2>& __rhs)
838 {
return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
840 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
842 operator<=(
const time_point<_Clock, _Dur1>& __lhs,
843 const time_point<_Clock, _Dur2>& __rhs)
844 {
return !(__rhs < __lhs); }
846 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
848 operator>(
const time_point<_Clock, _Dur1>& __lhs,
849 const time_point<_Clock, _Dur2>& __rhs)
850 {
return __rhs < __lhs; }
852 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
854 operator>=(
const time_point<_Clock, _Dur1>& __lhs,
855 const time_point<_Clock, _Dur2>& __rhs)
856 {
return !(__lhs < __rhs); }
878 inline namespace _V2 {
889 typedef duration::rep rep;
890 typedef duration::period period;
893 static_assert(system_clock::duration::min()
894 < system_clock::duration::zero(),
895 "a clock's minimum duration cannot be less than its epoch");
897 static constexpr
bool is_steady =
false;
906 return std::time_t(duration_cast<chrono::seconds>
907 (__t.time_since_epoch()).count());
911 from_time_t(std::time_t __t) noexcept
914 return time_point_cast<system_clock::duration>
929 typedef duration::rep rep;
930 typedef duration::period period;
933 static constexpr
bool is_steady =
true;
954 #if __cplusplus > 201103L
956 #define __cpp_lib_chrono_udls 201304
958 inline namespace literals
983 inline namespace chrono_literals
985 #pragma GCC diagnostic push
986 #pragma GCC diagnostic ignored "-Wliteral-suffix"
988 template<
typename _Dur,
char... _Digits>
989 constexpr _Dur __check_overflow()
991 using _Val = __parse_int::_Parse_int<_Digits...>;
992 constexpr
typename _Dur::rep __repval = _Val::value;
993 static_assert(__repval >= 0 && __repval == _Val::value,
994 "literal value cannot be represented by duration type");
995 return _Dur(__repval);
1001 operator""h(
long double __hours)
1005 template <
char... _Digits>
1012 operator""min(
long double __mins)
1016 template <
char... _Digits>
1023 operator""s(
long double __secs)
1027 template <
char... _Digits>
1034 operator""ms(
long double __msecs)
1038 template <
char... _Digits>
1045 operator""us(
long double __usecs)
1049 template <
char... _Digits>
1056 operator""ns(
long double __nsecs)
1060 template <
char... _Digits>
1065 #pragma GCC diagnostic pop
1071 using namespace literals::chrono_literals;
1076 _GLIBCXX_END_NAMESPACE_VERSION
1081 #endif //_GLIBCXX_CHRONO