29 #ifndef _GLIBCXX_CHRONO
30 #define _GLIBCXX_CHRONO 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
43 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
45 namespace std _GLIBCXX_VISIBILITY(default)
60 _GLIBCXX_BEGIN_NAMESPACE_VERSION
62 template<
typename _Rep,
typename _Period = ratio<1>>
65 template<
typename _Clock,
typename _Dur =
typename _Clock::duration>
68 _GLIBCXX_END_NAMESPACE_VERSION
71 _GLIBCXX_BEGIN_NAMESPACE_VERSION
73 template<
typename _Rep1,
typename _Period1,
typename _Rep2,
typename _Period2>
78 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
79 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
81 typedef ratio<__gcd_num::value,
82 (_Period1::den / __gcd_den::value) * _Period2::den> __r;
89 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
90 struct common_type<chrono::time_point<_Clock, _Dur1>,
99 _GLIBCXX_END_NAMESPACE_VERSION
103 _GLIBCXX_BEGIN_NAMESPACE_VERSION
106 template<
typename _ToDur,
typename _CF,
typename _CR,
107 bool _NumIsOne =
false,
bool _DenIsOne =
false>
108 struct __duration_cast_impl
110 template<
typename _Rep,
typename _Period>
111 static constexpr _ToDur
112 __cast(
const duration<_Rep, _Period>& __d)
114 typedef typename _ToDur::rep __to_rep;
115 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
116 * static_cast<_CR>(_CF::num)
117 /
static_cast<_CR
>(_CF::den)));
121 template<
typename _ToDur,
typename _CF,
typename _CR>
122 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
124 template<
typename _Rep,
typename _Period>
125 static constexpr _ToDur
126 __cast(
const duration<_Rep, _Period>& __d)
128 typedef typename _ToDur::rep __to_rep;
129 return _ToDur(static_cast<__to_rep>(__d.count()));
133 template<
typename _ToDur,
typename _CF,
typename _CR>
134 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
136 template<
typename _Rep,
typename _Period>
137 static constexpr _ToDur
138 __cast(
const duration<_Rep, _Period>& __d)
140 typedef typename _ToDur::rep __to_rep;
141 return _ToDur(static_cast<__to_rep>(
142 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
146 template<
typename _ToDur,
typename _CF,
typename _CR>
147 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
149 template<
typename _Rep,
typename _Period>
150 static constexpr _ToDur
151 __cast(
const duration<_Rep, _Period>& __d)
153 typedef typename _ToDur::rep __to_rep;
154 return _ToDur(static_cast<__to_rep>(
155 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
159 template<
typename _Tp>
164 template<
typename _Rep,
typename _Period>
165 struct __is_duration<duration<_Rep, _Period>>
170 template<
typename _ToDur,
typename _Rep,
typename _Period>
171 inline constexpr
typename enable_if<__is_duration<_ToDur>::value,
175 typedef typename _ToDur::period __to_period;
176 typedef typename _ToDur::rep __to_rep;
178 typedef typename __r_div::type __cf;
181 typedef __duration_cast_impl<_ToDur, __cf, __cr,
182 __cf::num == 1, __cf::den == 1> __dc;
183 return __dc::__cast(__d);
187 template<
typename _Rep>
193 template<
typename _Rep>
196 static constexpr _Rep
200 static constexpr _Rep
204 static constexpr _Rep
214 template<
intmax_t _Num,
intmax_t _Den>
215 struct __is_ratio<
ratio<_Num, _Den>>
220 template<
typename _Rep,
typename _Period>
224 typedef _Period period;
226 static_assert(!__is_duration<_Rep>::value,
"rep cannot be a duration");
227 static_assert(__is_ratio<_Period>::value,
228 "period must be a specialization of ratio");
229 static_assert(_Period::num > 0,
"period must be positive");
232 constexpr duration() : __r() { }
234 constexpr duration(
const duration&) =
default;
236 template<
typename _Rep2,
typename =
typename
237 enable_if<is_convertible<_Rep2, rep>::value
238 && (treat_as_floating_point<rep>::value
239 || !treat_as_floating_point<_Rep2>::value)>::type>
240 constexpr
explicit duration(
const _Rep2& __rep)
241 : __r(static_cast<rep>(__rep)) { }
243 template<
typename _Rep2,
typename _Period2,
typename =
typename
244 enable_if<treat_as_floating_point<rep>::value
245 || (ratio_divide<_Period2, period>::type::den == 1
246 && !treat_as_floating_point<_Rep2>::value)>::type>
247 constexpr duration(
const duration<_Rep2, _Period2>& __d)
248 : __r(duration_cast<duration>(__d).
count()) { }
250 ~duration() =
default;
251 duration& operator=(
const duration&) =
default;
265 {
return duration(-__r); }
276 {
return duration(__r++); }
287 {
return duration(__r--); }
290 operator+=(
const duration& __d)
297 operator-=(
const duration& __d)
304 operator*=(
const rep& __rhs)
311 operator/=(
const rep& __rhs)
318 template<
typename _Rep2 = rep>
319 typename enable_if<!treat_as_floating_point<_Rep2>::value,
321 operator%=(
const rep& __rhs)
327 template<
typename _Rep2 = rep>
328 typename enable_if<!treat_as_floating_point<_Rep2>::value,
330 operator%=(
const duration& __d)
337 static constexpr duration
339 {
return duration(duration_values<rep>::zero()); }
341 static constexpr duration
345 static constexpr duration
353 template<
typename _Rep1,
typename _Period1,
354 typename _Rep2,
typename _Period2>
355 inline typename common_type<duration<_Rep1, _Period1>,
356 duration<_Rep2, _Period2>>::type
357 operator+(
const duration<_Rep1, _Period1>& __lhs,
358 const duration<_Rep2, _Period2>& __rhs)
360 typedef duration<_Rep1, _Period1> __dur1;
361 typedef duration<_Rep2, _Period2> __dur2;
362 typedef typename common_type<__dur1,__dur2>::type __ct;
363 return __ct(__lhs) += __rhs;
366 template<
typename _Rep1,
typename _Period1,
367 typename _Rep2,
typename _Period2>
368 inline typename common_type<duration<_Rep1, _Period1>,
369 duration<_Rep2, _Period2>>::type
370 operator-(
const duration<_Rep1, _Period1>& __lhs,
371 const duration<_Rep2, _Period2>& __rhs)
373 typedef duration<_Rep1, _Period1> __dur1;
374 typedef duration<_Rep2, _Period2> __dur2;
375 typedef typename common_type<__dur1,__dur2>::type __ct;
376 return __ct(__lhs) -= __rhs;
379 template<
typename _Rep1,
typename _Rep2,
bool =
380 is_convertible<_Rep2,
381 typename common_type<_Rep1, _Rep2>::type>::value>
382 struct __common_rep_type { };
384 template<
typename _Rep1,
typename _Rep2>
385 struct __common_rep_type<_Rep1, _Rep2, true>
386 {
typedef typename common_type<_Rep1, _Rep2>::type type; };
388 template<
typename _Rep1,
typename _Period,
typename _Rep2>
389 inline duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
390 operator*(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
392 typedef typename common_type<_Rep1, _Rep2>::type __cr;
393 return duration<__cr, _Period>(__d) *= __s;
396 template<
typename _Rep1,
typename _Period,
typename _Rep2>
397 inline duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
398 operator*(
const _Rep1& __s,
const duration<_Rep2, _Period>& __d)
399 {
return __d * __s; }
401 template<
typename _Rep1,
typename _Period,
typename _Rep2>
402 inline duration<
typename __common_rep_type<_Rep1,
typename
403 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
404 operator/(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
406 typedef typename common_type<_Rep1, _Rep2>::type __cr;
407 return duration<__cr, _Period>(__d) /= __s;
410 template<
typename _Rep1,
typename _Period1,
411 typename _Rep2,
typename _Period2>
412 inline typename common_type<_Rep1, _Rep2>::type
413 operator/(
const duration<_Rep1, _Period1>& __lhs,
414 const duration<_Rep2, _Period2>& __rhs)
416 typedef duration<_Rep1, _Period1> __dur1;
417 typedef duration<_Rep2, _Period2> __dur2;
418 typedef typename common_type<__dur1,__dur2>::type __ct;
419 return __ct(__lhs).count() / __ct(__rhs).count();
423 template<
typename _Rep1,
typename _Period,
typename _Rep2>
424 inline duration<
typename __common_rep_type<_Rep1,
typename
425 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
426 operator%(
const duration<_Rep1, _Period>& __d,
const _Rep2& __s)
428 typedef typename common_type<_Rep1, _Rep2>::type __cr;
429 return duration<__cr, _Period>(__d) %= __s;
432 template<
typename _Rep1,
typename _Period1,
433 typename _Rep2,
typename _Period2>
434 inline typename common_type<duration<_Rep1, _Period1>,
435 duration<_Rep2, _Period2>>::type
436 operator%(
const duration<_Rep1, _Period1>& __lhs,
437 const duration<_Rep2, _Period2>& __rhs)
439 typedef duration<_Rep1, _Period1> __dur1;
440 typedef duration<_Rep2, _Period2> __dur2;
441 typedef typename common_type<__dur1,__dur2>::type __ct;
442 return __ct(__lhs) %= __rhs;
446 template<
typename _Rep1,
typename _Period1,
447 typename _Rep2,
typename _Period2>
448 inline constexpr
bool
449 operator==(
const duration<_Rep1, _Period1>& __lhs,
450 const duration<_Rep2, _Period2>& __rhs)
452 typedef duration<_Rep1, _Period1> __dur1;
453 typedef duration<_Rep2, _Period2> __dur2;
454 typedef typename common_type<__dur1,__dur2>::type __ct;
455 return __ct(__lhs).count() == __ct(__rhs).count();
458 template<
typename _Rep1,
typename _Period1,
459 typename _Rep2,
typename _Period2>
460 inline constexpr
bool
461 operator<(const duration<_Rep1, _Period1>& __lhs,
462 const duration<_Rep2, _Period2>& __rhs)
464 typedef duration<_Rep1, _Period1> __dur1;
465 typedef duration<_Rep2, _Period2> __dur2;
466 typedef typename common_type<__dur1,__dur2>::type __ct;
467 return __ct(__lhs).count() < __ct(__rhs).count();
470 template<
typename _Rep1,
typename _Period1,
471 typename _Rep2,
typename _Period2>
472 inline constexpr
bool
473 operator!=(
const duration<_Rep1, _Period1>& __lhs,
474 const duration<_Rep2, _Period2>& __rhs)
475 {
return !(__lhs == __rhs); }
477 template<
typename _Rep1,
typename _Period1,
478 typename _Rep2,
typename _Period2>
479 inline constexpr
bool
480 operator<=(const duration<_Rep1, _Period1>& __lhs,
481 const duration<_Rep2, _Period2>& __rhs)
482 {
return !(__rhs < __lhs); }
484 template<
typename _Rep1,
typename _Period1,
485 typename _Rep2,
typename _Period2>
486 inline constexpr
bool
487 operator>(
const duration<_Rep1, _Period1>& __lhs,
488 const duration<_Rep2, _Period2>& __rhs)
489 {
return __rhs < __lhs; }
491 template<
typename _Rep1,
typename _Period1,
492 typename _Rep2,
typename _Period2>
493 inline constexpr
bool
494 operator>=(
const duration<_Rep1, _Period1>& __lhs,
495 const duration<_Rep2, _Period2>& __rhs)
496 {
return !(__lhs < __rhs); }
517 template<
typename _Clock,
typename _Dur>
520 typedef _Clock clock;
522 typedef typename duration::rep rep;
523 typedef typename duration::period period;
525 constexpr
time_point() : __d(duration::zero())
528 constexpr
explicit time_point(
const duration& __dur)
533 template<
typename _Dur2>
535 : __d(__t.time_since_epoch())
540 time_since_epoch()
const
559 static constexpr time_point
563 static constexpr time_point
572 template<
typename _ToDur,
typename _Clock,
typename _Dur>
574 time_point<_Clock, _ToDur>>::type
578 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
581 template<
typename _Clock,
typename _Dur1,
582 typename _Rep2,
typename _Period2>
583 inline time_point<_Clock,
585 operator+(
const time_point<_Clock, _Dur1>& __lhs,
586 const duration<_Rep2, _Period2>& __rhs)
588 typedef duration<_Rep2, _Period2> __dur2;
590 typedef time_point<_Clock, __ct> __time_point;
591 return __time_point(__lhs) += __rhs;
594 template<
typename _Rep1,
typename _Period1,
595 typename _Clock,
typename _Dur2>
596 inline time_point<_Clock,
597 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
598 operator+(
const duration<_Rep1, _Period1>& __lhs,
599 const time_point<_Clock, _Dur2>& __rhs)
600 {
return __rhs + __lhs; }
602 template<
typename _Clock,
typename _Dur1,
603 typename _Rep2,
typename _Period2>
604 inline time_point<_Clock,
605 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
606 operator-(
const time_point<_Clock, _Dur1>& __lhs,
607 const duration<_Rep2, _Period2>& __rhs)
608 {
return __lhs + (-__rhs); }
610 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
611 inline typename common_type<_Dur1, _Dur2>::type
612 operator-(
const time_point<_Clock, _Dur1>& __lhs,
613 const time_point<_Clock, _Dur2>& __rhs)
614 {
return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
616 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
617 inline constexpr
bool
618 operator==(
const time_point<_Clock, _Dur1>& __lhs,
619 const time_point<_Clock, _Dur2>& __rhs)
620 {
return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
622 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
623 inline constexpr
bool
624 operator!=(
const time_point<_Clock, _Dur1>& __lhs,
625 const time_point<_Clock, _Dur2>& __rhs)
626 {
return !(__lhs == __rhs); }
628 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
629 inline constexpr
bool
630 operator<(const time_point<_Clock, _Dur1>& __lhs,
631 const time_point<_Clock, _Dur2>& __rhs)
632 {
return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
634 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
635 inline constexpr
bool
636 operator<=(const time_point<_Clock, _Dur1>& __lhs,
637 const time_point<_Clock, _Dur2>& __rhs)
638 {
return !(__rhs < __lhs); }
640 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
641 inline constexpr
bool
642 operator>(
const time_point<_Clock, _Dur1>& __lhs,
643 const time_point<_Clock, _Dur2>& __rhs)
644 {
return __rhs < __lhs; }
646 template<
typename _Clock,
typename _Dur1,
typename _Dur2>
647 inline constexpr
bool
648 operator>=(
const time_point<_Clock, _Dur1>& __lhs,
649 const time_point<_Clock, _Dur2>& __rhs)
650 {
return !(__lhs < __rhs); }
655 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
657 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
663 typedef duration::rep rep;
664 typedef duration::period period;
668 < system_clock::duration::zero(),
669 "a clock's minimum duration cannot be less than its epoch");
671 static constexpr
bool is_monotonic =
false;
680 return std::time_t(duration_cast<chrono::seconds>
681 (__t.time_since_epoch()).
count());
685 from_time_t(std::time_t __t)
693 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
698 typedef duration::rep rep;
699 typedef duration::period period;
702 static constexpr
bool is_monotonic =
true;
708 typedef system_clock monotonic_clock;
711 typedef system_clock high_resolution_clock;
713 _GLIBCXX_END_NAMESPACE_VERSION
719 #endif //_GLIBCXX_USE_C99_STDINT_TR1
721 #endif //__GXX_EXPERIMENTAL_CXX0X__
723 #endif //_GLIBCXX_CHRONO