29 #ifndef _GLIBCXX_CHRONO
30 #define _GLIBCXX_CHRONO 1
32 #pragma GCC system_header
34 #if __cplusplus < 201103L
44 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
46 namespace std _GLIBCXX_VISIBILITY(default)
61 _GLIBCXX_BEGIN_NAMESPACE_VERSION
63 template<
typename _Rep,
typename _Period = ratio<1>>
66 template<
typename _Clock,
typename _Dur =
typename _Clock::duration>
69 _GLIBCXX_END_NAMESPACE_VERSION
72 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76 template<
typename _CT,
typename _Period1,
typename _Period2>
77 struct __duration_common_type_wrapper
80 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
81 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
82 typedef typename _CT::type __cr;
83 typedef ratio<__gcd_num::value,
84 (_Period1::den / __gcd_den::value) * _Period2::den> __r;
86 typedef __success_type<chrono::duration<__cr, __r>> type;
89 template<
typename _Period1,
typename _Period2>
90 struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
91 {
typedef __failure_type type; };
93 template<
typename _Rep1,
typename _Period1,
typename _Rep2,
typename _Period2>
94 struct common_type<chrono::duration<_Rep1, _Period1>,
95 chrono::duration<_Rep2, _Period2>>
96 :
public __duration_common_type_wrapper<typename __member_type_wrapper<
97 common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
102 template<
typename _CT,
typename _Clock>
103 struct __timepoint_common_type_wrapper
105 typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
109 template<
typename _Clock>
110 struct __timepoint_common_type_wrapper<__failure_type, _Clock>
111 {
typedef __failure_type type; };
113 template<
typename _Clock,
typename _Duration1,
typename _Duration2>
114 struct common_type<chrono::time_point<_Clock, _Duration1>,
115 chrono::time_point<_Clock, _Duration2>>
116 :
public __timepoint_common_type_wrapper<typename __member_type_wrapper<
117 common_type<_Duration1, _Duration2>>::type, _Clock>::type
120 _GLIBCXX_END_NAMESPACE_VERSION
124 _GLIBCXX_BEGIN_NAMESPACE_VERSION
127 template<
typename _ToDur,
typename _CF,
typename _CR,
128 bool _NumIsOne =
false,
bool _DenIsOne =
false>
129 struct __duration_cast_impl
131 template<
typename _Rep,
typename _Period>
132 static constexpr _ToDur
133 __cast(
const duration<_Rep, _Period>& __d)
135 typedef typename _ToDur::rep __to_rep;
136 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
137 * static_cast<_CR>(_CF::num)
138 /
static_cast<_CR
>(_CF::den)));
142 template<
typename _ToDur,
typename _CF,
typename _CR>
143 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
145 template<
typename _Rep,
typename _Period>
146 static constexpr _ToDur
147 __cast(
const duration<_Rep, _Period>& __d)
149 typedef typename _ToDur::rep __to_rep;
150 return _ToDur(static_cast<__to_rep>(__d.count()));
154 template<
typename _ToDur,
typename _CF,
typename _CR>
155 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
157 template<
typename _Rep,
typename _Period>
158 static constexpr _ToDur
159 __cast(
const duration<_Rep, _Period>& __d)
161 typedef typename _ToDur::rep __to_rep;
162 return _ToDur(static_cast<__to_rep>(
163 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
167 template<
typename _ToDur,
typename _CF,
typename _CR>
168 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
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::num)));
180 template<
typename _Tp>
185 template<
typename _Rep,
typename _Period>
186 struct __is_duration<duration<_Rep, _Period>>
191 template<
typename _ToDur,
typename _Rep,
typename _Period>
192 constexpr
typename enable_if<__is_duration<_ToDur>::value,
196 typedef typename _ToDur::period __to_period;
197 typedef typename _ToDur::rep __to_rep;
199 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
201 typedef __duration_cast_impl<_ToDur, __cf, __cr,
202 __cf::num == 1, __cf::den == 1> __dc;
203 return __dc::__cast(__d);
207 template<
typename _Rep>
213 template<
typename _Rep>
216 static constexpr _Rep
220 static constexpr _Rep
224 static constexpr _Rep
229 template<
typename _Tp>
234 template<
intmax_t _Num,
intmax_t _Den>
235 struct __is_ratio<
ratio<_Num, _Den>>
240 template<
typename _Rep,
typename _Period>
244 typedef _Period period;
246 static_assert(!__is_duration<_Rep>::value,
"rep cannot be a duration");
247 static_assert(__is_ratio<_Period>::value,
248 "period must be a specialization of ratio");
249 static_assert(_Period::num > 0,
"period must be positive");
252 constexpr duration() =
default;
257 duration(
const duration&) =
default;
259 template<
typename _Rep2,
typename =
typename
260 enable_if<is_convertible<_Rep2, rep>::value
261 && (treat_as_floating_point<rep>::value
262 || !treat_as_floating_point<_Rep2>::value)>::type>
263 constexpr
explicit duration(
const _Rep2& __rep)
264 : __r(static_cast<rep>(__rep)) { }
266 template<
typename _Rep2,
typename _Period2,
typename =
typename
267 enable_if<treat_as_floating_point<rep>::value
268 || (ratio_divide<_Period2, period>::den == 1
269 && !treat_as_floating_point<_Rep2>::value)>::type>
270 constexpr duration(
const duration<_Rep2, _Period2>& __d)
271 : __r(duration_cast<duration>(__d).
count()) { }
273 ~duration() =
default;
274 duration& operator=(
const duration&) =
default;
288 {
return duration(-__r); }
299 {
return duration(__r++); }
310 {
return duration(__r--); }
313 operator+=(
const duration& __d)
320 operator-=(
const duration& __d)
327 operator*=(
const rep& __rhs)
334 operator/=(
const rep& __rhs)
341 template<
typename _Rep2 = rep>
342 typename enable_if<!treat_as_floating_point<_Rep2>::value,
344 operator%=(
const rep& __rhs)
350 template<
typename _Rep2 = rep>
351 typename enable_if<!treat_as_floating_point<_Rep2>::value,
353 operator%=(
const duration& __d)
360 static constexpr duration
362 {
return duration(duration_values<rep>::zero()); }
364 static constexpr duration
368 static constexpr duration
376 template<
typename _Rep1,
typename _Period1,
377 typename _Rep2,
typename _Period2>
378 constexpr
typename common_type<duration<_Rep1, _Period1>,
379 duration<_Rep2, _Period2>>::type
380 operator+(
const duration<_Rep1, _Period1>& __lhs,
381 const duration<_Rep2, _Period2>& __rhs)
383 typedef duration<_Rep1, _Period1> __dur1;
384 typedef duration<_Rep2, _Period2> __dur2;
385 typedef typename common_type<__dur1,__dur2>::type __cd;
386 return __cd(__cd(__lhs).
count() + __cd(__rhs).
count());
389 template<
typename _Rep1,
typename _Period1,
390 typename _Rep2,
typename _Period2>
391 constexpr
typename common_type<duration<_Rep1, _Period1>,
392 duration<_Rep2, _Period2>>::type
393 operator-(
const duration<_Rep1, _Period1>& __lhs,
394 const duration<_Rep2, _Period2>& __rhs)
396 typedef duration<_Rep1, _Period1> __dur1;
397 typedef duration<_Rep2, _Period2> __dur2;
398 typedef typename common_type<__dur1,__dur2>::type __cd;
399 return __cd(__cd(__lhs).
count() - __cd(__rhs).
count());
402 template<
typename _Rep1,
typename _Rep2,
bool =
403 is_convertible<_Rep2,
404 typename common_type<_Rep1, _Rep2>::type>::value>
405 struct __common_rep_type { };
407 template<
typename _Rep1,
typename _Rep2>
408 struct __common_rep_type<_Rep1, _Rep2, true>
409 {
typedef typename common_type<_Rep1, _Rep2>::type type; };
411 template<
typename _Rep1,
typename _Period,
typename _Rep2>
413 duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
414 operator*(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
416 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
418 return __cd(__cd(__d).
count() * __s);
421 template<
typename _Rep1,
typename _Rep2,
typename _Period>
423 duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
424 operator*(
const _Rep1& __s,
const duration<_Rep2, _Period>& __d)
425 {
return __d * __s; }
427 template<
typename _Rep1,
typename _Period,
typename _Rep2>
428 constexpr duration<
typename __common_rep_type<_Rep1,
typename
429 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
430 operator/(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
432 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
434 return __cd(__cd(__d).
count() / __s);
437 template<
typename _Rep1,
typename _Period1,
438 typename _Rep2,
typename _Period2>
439 constexpr
typename common_type<_Rep1, _Rep2>::type
440 operator/(
const duration<_Rep1, _Period1>& __lhs,
441 const duration<_Rep2, _Period2>& __rhs)
443 typedef duration<_Rep1, _Period1> __dur1;
444 typedef duration<_Rep2, _Period2> __dur2;
445 typedef typename common_type<__dur1,__dur2>::type __cd;
446 return __cd(__lhs).count() / __cd(__rhs).count();
450 template<
typename _Rep1,
typename _Period,
typename _Rep2>
451 constexpr duration<
typename __common_rep_type<_Rep1,
typename
452 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
453 operator%(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
455 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
457 return __cd(__cd(__d).
count() % __s);
460 template<
typename _Rep1,
typename _Period1,
461 typename _Rep2,
typename _Period2>
462 constexpr
typename common_type<duration<_Rep1, _Period1>,
463 duration<_Rep2, _Period2>>::type
464 operator%(
const duration<_Rep1, _Period1>& __lhs,
465 const duration<_Rep2, _Period2>& __rhs)
467 typedef duration<_Rep1, _Period1> __dur1;
468 typedef duration<_Rep2, _Period2> __dur2;
469 typedef typename common_type<__dur1,__dur2>::type __cd;
470 return __cd(__cd(__lhs).
count() % __cd(__rhs).
count());
474 template<
typename _Rep1,
typename _Period1,
475 typename _Rep2,
typename _Period2>
477 operator==(
const duration<_Rep1, _Period1>& __lhs,
478 const duration<_Rep2, _Period2>& __rhs)
480 typedef duration<_Rep1, _Period1> __dur1;
481 typedef duration<_Rep2, _Period2> __dur2;
482 typedef typename common_type<__dur1,__dur2>::type __ct;
483 return __ct(__lhs).count() == __ct(__rhs).count();
486 template<
typename _Rep1,
typename _Period1,
487 typename _Rep2,
typename _Period2>
489 operator<(const duration<_Rep1, _Period1>& __lhs,
490 const duration<_Rep2, _Period2>& __rhs)
492 typedef duration<_Rep1, _Period1> __dur1;
493 typedef duration<_Rep2, _Period2> __dur2;
494 typedef typename common_type<__dur1,__dur2>::type __ct;
495 return __ct(__lhs).count() < __ct(__rhs).count();
498 template<
typename _Rep1,
typename _Period1,
499 typename _Rep2,
typename _Period2>
501 operator!=(
const duration<_Rep1, _Period1>& __lhs,
502 const duration<_Rep2, _Period2>& __rhs)
503 {
return !(__lhs == __rhs); }
505 template<
typename _Rep1,
typename _Period1,
506 typename _Rep2,
typename _Period2>
508 operator<=(const duration<_Rep1, _Period1>& __lhs,
509 const duration<_Rep2, _Period2>& __rhs)
510 {
return !(__rhs < __lhs); }
512 template<
typename _Rep1,
typename _Period1,
513 typename _Rep2,
typename _Period2>
515 operator>(
const duration<_Rep1, _Period1>& __lhs,
516 const duration<_Rep2, _Period2>& __rhs)
517 {
return __rhs < __lhs; }
519 template<
typename _Rep1,
typename _Period1,
520 typename _Rep2,
typename _Period2>
522 operator>=(
const duration<_Rep1, _Period1>& __lhs,
523 const duration<_Rep2, _Period2>& __rhs)
524 {
return !(__lhs < __rhs); }
545 template<
typename _Clock,
typename _Dur>
548 typedef _Clock clock;
550 typedef typename duration::rep rep;
551 typedef typename duration::period period;
553 constexpr
time_point() : __d(duration::zero())
556 constexpr
explicit time_point(
const duration& __dur)
561 template<
typename _Dur2>
563 : __d(__t.time_since_epoch())
568 time_since_epoch()
const
587 static constexpr time_point
591 static constexpr time_point
600 template<
typename _ToDur,
typename _Clock,
typename _Dur>
601 constexpr
typename enable_if<__is_duration<_ToDur>::value,
602 time_point<_Clock, _ToDur>>::type
606 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
609 template<
typename _Clock,
typename _Dur1,
610 typename _Rep2,
typename _Period2>
611 constexpr time_point<_Clock,
612 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
613 operator+(
const time_point<_Clock, _Dur1>& __lhs,
614 const duration<_Rep2, _Period2>& __rhs)
616 typedef duration<_Rep2, _Period2> __dur2;
617 typedef typename common_type<_Dur1,__dur2>::type __ct;
618 typedef time_point<_Clock, __ct> __time_point;
619 return __time_point(__lhs.time_since_epoch() + __rhs);
622 template<
typename _Rep1,
typename _Period1,
623 typename _Clock,
typename _Dur2>
624 constexpr time_point<_Clock,
625 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
626 operator+(
const duration<_Rep1, _Period1>& __lhs,
627 const time_point<_Clock, _Dur2>& __rhs)
629 typedef duration<_Rep1, _Period1> __dur1;
630 typedef typename common_type<__dur1,_Dur2>::type __ct;
631 typedef time_point<_Clock, __ct> __time_point;
632 return __time_point(__rhs.time_since_epoch() + __lhs);
635 template<
typename _Clock,
typename _Dur1,
636 typename _Rep2,
typename _Period2>
637 constexpr time_point<_Clock,
638 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
639 operator-(
const time_point<_Clock, _Dur1>& __lhs,
640 const duration<_Rep2, _Period2>& __rhs)
642 typedef duration<_Rep2, _Period2> __dur2;
643 typedef typename common_type<_Dur1,__dur2>::type __ct;
644 typedef time_point<_Clock, __ct> __time_point;
645 return __time_point(__lhs.time_since_epoch() -__rhs);
648 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
649 constexpr
typename common_type<_Dur1, _Dur2>::type
650 operator-(
const time_point<_Clock, _Dur1>& __lhs,
651 const time_point<_Clock, _Dur2>& __rhs)
652 {
return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
654 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
656 operator==(
const time_point<_Clock, _Dur1>& __lhs,
657 const time_point<_Clock, _Dur2>& __rhs)
658 {
return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
660 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
662 operator!=(
const time_point<_Clock, _Dur1>& __lhs,
663 const time_point<_Clock, _Dur2>& __rhs)
664 {
return !(__lhs == __rhs); }
666 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
668 operator<(const time_point<_Clock, _Dur1>& __lhs,
669 const time_point<_Clock, _Dur2>& __rhs)
670 {
return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
672 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
674 operator<=(const time_point<_Clock, _Dur1>& __lhs,
675 const time_point<_Clock, _Dur2>& __rhs)
676 {
return !(__rhs < __lhs); }
678 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
680 operator>(
const time_point<_Clock, _Dur1>& __lhs,
681 const time_point<_Clock, _Dur2>& __rhs)
682 {
return __rhs < __lhs; }
684 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
686 operator>=(
const time_point<_Clock, _Dur1>& __lhs,
687 const time_point<_Clock, _Dur2>& __rhs)
688 {
return !(__lhs < __rhs); }
709 inline namespace _V2 {
719 typedef duration::rep rep;
720 typedef duration::period period;
724 < system_clock::duration::zero(),
725 "a clock's minimum duration cannot be less than its epoch");
727 static constexpr
bool is_steady =
false;
736 return std::time_t(duration_cast<chrono::seconds>
737 (__t.time_since_epoch()).
count());
741 from_time_t(std::time_t __t) noexcept
758 typedef duration::rep rep;
759 typedef duration::period period;
762 static constexpr
bool is_steady =
true;
780 _GLIBCXX_END_NAMESPACE_VERSION
783 #if __cplusplus > 201103L
785 inline namespace literals
787 inline namespace chrono_literals
790 namespace __select_type
793 using namespace __parse_int;
795 template<
unsigned long long _Val,
typename _Dur>
798 _Val <= static_cast<unsigned long long>
799 (numeric_limits<typename _Dur::rep>::max()),
802 static constexpr typename _Select_type::type
803 value{static_cast<typename _Select_type::type>(_Val)};
806 template<unsigned long long _Val, typename _Dur>
807 constexpr typename _Select_type<_Val, _Dur>::type
808 _Select_type<_Val, _Dur>::value;
812 constexpr chrono::duration<long double, ratio<3600,1>>
813 operator""h(long double __hours)
814 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
816 template <char... _Digits>
818 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
822 return __select_type::_Select_type<
823 __select_int::_Select_int<_Digits...>::value,
824 chrono::hours>::value;
827 constexpr chrono::duration<long double, ratio<60,1>>
828 operator""min(long double __mins)
829 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
831 template <char... _Digits>
833 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
834 chrono::minutes>::type
837 return __select_type::_Select_type<
838 __select_int::_Select_int<_Digits...>::value,
839 chrono::minutes>::value;
842 constexpr chrono::duration<long double>
843 operator""s(long double __secs)
844 { return chrono::duration<long double>{__secs}; }
846 template <char... _Digits>
848 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
849 chrono::seconds>::type
852 return __select_type::_Select_type<
853 __select_int::_Select_int<_Digits...>::value,
854 chrono::seconds>::value;
857 constexpr chrono::duration<long double, milli>
858 operator""ms(long double __msecs)
859 { return chrono::duration<long double, milli>{__msecs}; }
861 template <char... _Digits>
863 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
864 chrono::milliseconds>::type
867 return __select_type::_Select_type<
868 __select_int::_Select_int<_Digits...>::value,
869 chrono::milliseconds>::value;
872 constexpr chrono::duration<long double, micro>
873 operator""us(long double __usecs)
874 { return chrono::duration<long double, micro>{__usecs}; }
876 template <char... _Digits>
878 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
879 chrono::microseconds>::type
882 return __select_type::_Select_type<
883 __select_int::_Select_int<_Digits...>::value,
884 chrono::microseconds>::value;
887 constexpr chrono::duration<long double, nano>
888 operator""ns(long double __nsecs)
889 { return chrono::duration<long double, nano>{__nsecs}; }
891 template <char... _Digits>
893 __select_type::_Select_type<__select_int::_Select_int<_Digits...>::value,
894 chrono::nanoseconds>::type
897 return __select_type::_Select_type<
898 __select_int::_Select_int<_Digits...>::value,
899 chrono::nanoseconds>::value;
complex< _Tp > operator+(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x plus y.
duration< int64_t, ratio< 60 > > minutes
minutes
duration< int64_t, nano > nanoseconds
nanoseconds
duration< int64_t > seconds
seconds
duration< int64_t, ratio< 3600 > > hours
hours
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
duration< int64_t, milli > milliseconds
milliseconds
static constexpr _Tp max() noexcept
const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
size_t count() const noexcept
Returns the number of bits which are set.
duration< int64_t, micro > microseconds
microseconds
const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
typename __ratio_divide< _R1, _R2 >::type ratio_divide
ratio_divide
static constexpr _Tp lowest() noexcept
complex< _Tp > operator-(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x minus y.
complex< _Tp > operator*(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x times y.
complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
constexpr enable_if< __is_duration< _ToDur >::value, _ToDur >::type duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Provides compile-time rational arithmetic.