1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010, 2011 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.
29 #ifndef _GLIBCXX_CHRONO
30 #define _GLIBCXX_CHRONO 1
32 #pragma GCC system_header
34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
35 # include <bits/c++0x_warning.h>
39 #include <type_traits>
43 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
45 namespace std _GLIBCXX_VISIBILITY(default)
48 * @defgroup chrono Time
51 * Classes and functions for time.
55 /** @namespace std::chrono
56 * @brief ISO C++ 0x entities sub namespace for time and date.
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
72 // 20.8.2.3 specialization of common_type (for duration)
73 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
74 struct common_type<chrono::duration<_Rep1, _Period1>,
75 chrono::duration<_Rep2, _Period2>>
78 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
79 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
80 typedef typename common_type<_Rep1, _Rep2>::type __cr;
81 typedef ratio<__gcd_num::value,
82 (_Period1::den / __gcd_den::value) * _Period2::den> __r;
85 typedef chrono::duration<__cr, __r> type;
88 // 20.8.2.3 specialization of common_type (for time_point)
89 template<typename _Clock, typename _Dur1, typename _Dur2>
90 struct common_type<chrono::time_point<_Clock, _Dur1>,
91 chrono::time_point<_Clock, _Dur2>>
94 typedef typename common_type<_Dur1, _Dur2>::type __ct;
97 typedef chrono::time_point<_Clock, __ct> type;
99 _GLIBCXX_END_NAMESPACE_VERSION
103 _GLIBCXX_BEGIN_NAMESPACE_VERSION
105 // Primary template for duration_cast impl.
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 constexpr typename enable_if<__is_duration<_ToDur>::value,
173 duration_cast(const duration<_Rep, _Period>& __d)
175 typedef typename _ToDur::period __to_period;
176 typedef typename _ToDur::rep __to_rep;
177 typedef ratio_divide<_Period, __to_period> __r_div;
178 typedef typename __r_div::type __cf;
179 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
181 typedef __duration_cast_impl<_ToDur, __cf, __cr,
182 __cf::num == 1, __cf::den == 1> __dc;
183 return __dc::__cast(__d);
186 /// treat_as_floating_point
187 template<typename _Rep>
188 struct treat_as_floating_point
189 : is_floating_point<_Rep>
193 template<typename _Rep>
194 struct duration_values
196 static constexpr _Rep
200 static constexpr _Rep
202 { return numeric_limits<_Rep>::max(); }
204 static constexpr _Rep
206 { return numeric_limits<_Rep>::lowest(); }
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");
231 // 20.8.3.1 construction / copy / destroy
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;
258 // 20.8.3.3 arithmetic
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)
336 // 20.8.3.4 special values
337 static constexpr duration
339 { return duration(duration_values<rep>::zero()); }
341 static constexpr duration
343 { return duration(duration_values<rep>::min()); }
345 static constexpr duration
347 { return duration(duration_values<rep>::max()); }
353 template<typename _Rep1, typename _Period1,
354 typename _Rep2, typename _Period2>
355 constexpr 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 __cd;
363 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
366 template<typename _Rep1, typename _Period1,
367 typename _Rep2, typename _Period2>
368 constexpr 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 __cd;
376 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
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>
390 duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
391 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
393 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
395 return __cd(__cd(__d).count() * __s);
398 template<typename _Rep1, typename _Rep2, typename _Period>
400 duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
401 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
402 { return __d * __s; }
404 template<typename _Rep1, typename _Period, typename _Rep2>
405 constexpr duration<typename __common_rep_type<_Rep1, typename
406 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
407 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
409 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
411 return __cd(__cd(__d).count() / __s);
414 template<typename _Rep1, typename _Period1,
415 typename _Rep2, typename _Period2>
416 constexpr typename common_type<_Rep1, _Rep2>::type
417 operator/(const duration<_Rep1, _Period1>& __lhs,
418 const duration<_Rep2, _Period2>& __rhs)
420 typedef duration<_Rep1, _Period1> __dur1;
421 typedef duration<_Rep2, _Period2> __dur2;
422 typedef typename common_type<__dur1,__dur2>::type __cd;
423 return __cd(__lhs).count() / __cd(__rhs).count();
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<duration<_Rep1, _Period1>,
440 duration<_Rep2, _Period2>>::type
441 operator%(const duration<_Rep1, _Period1>& __lhs,
442 const duration<_Rep2, _Period2>& __rhs)
444 typedef duration<_Rep1, _Period1> __dur1;
445 typedef duration<_Rep2, _Period2> __dur2;
446 typedef typename common_type<__dur1,__dur2>::type __cd;
447 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
451 template<typename _Rep1, typename _Period1,
452 typename _Rep2, typename _Period2>
454 operator==(const duration<_Rep1, _Period1>& __lhs,
455 const duration<_Rep2, _Period2>& __rhs)
457 typedef duration<_Rep1, _Period1> __dur1;
458 typedef duration<_Rep2, _Period2> __dur2;
459 typedef typename common_type<__dur1,__dur2>::type __ct;
460 return __ct(__lhs).count() == __ct(__rhs).count();
463 template<typename _Rep1, typename _Period1,
464 typename _Rep2, typename _Period2>
466 operator<(const duration<_Rep1, _Period1>& __lhs,
467 const duration<_Rep2, _Period2>& __rhs)
469 typedef duration<_Rep1, _Period1> __dur1;
470 typedef duration<_Rep2, _Period2> __dur2;
471 typedef typename common_type<__dur1,__dur2>::type __ct;
472 return __ct(__lhs).count() < __ct(__rhs).count();
475 template<typename _Rep1, typename _Period1,
476 typename _Rep2, typename _Period2>
478 operator!=(const duration<_Rep1, _Period1>& __lhs,
479 const duration<_Rep2, _Period2>& __rhs)
480 { return !(__lhs == __rhs); }
482 template<typename _Rep1, typename _Period1,
483 typename _Rep2, typename _Period2>
485 operator<=(const duration<_Rep1, _Period1>& __lhs,
486 const duration<_Rep2, _Period2>& __rhs)
487 { return !(__rhs < __lhs); }
489 template<typename _Rep1, typename _Period1,
490 typename _Rep2, typename _Period2>
492 operator>(const duration<_Rep1, _Period1>& __lhs,
493 const duration<_Rep2, _Period2>& __rhs)
494 { return __rhs < __lhs; }
496 template<typename _Rep1, typename _Period1,
497 typename _Rep2, typename _Period2>
499 operator>=(const duration<_Rep1, _Period1>& __lhs,
500 const duration<_Rep2, _Period2>& __rhs)
501 { return !(__lhs < __rhs); }
504 typedef duration<int64_t, nano> nanoseconds;
507 typedef duration<int64_t, micro> microseconds;
510 typedef duration<int64_t, milli> milliseconds;
513 typedef duration<int64_t> seconds;
516 typedef duration<int, ratio< 60>> minutes;
519 typedef duration<int, ratio<3600>> hours;
522 template<typename _Clock, typename _Dur>
525 typedef _Clock clock;
526 typedef _Dur duration;
527 typedef typename duration::rep rep;
528 typedef typename duration::period period;
530 constexpr time_point() : __d(duration::zero())
533 constexpr explicit time_point(const duration& __dur)
538 template<typename _Dur2>
539 constexpr time_point(const time_point<clock, _Dur2>& __t)
540 : __d(__t.time_since_epoch())
545 time_since_epoch() const
550 operator+=(const duration& __dur)
557 operator-=(const duration& __dur)
564 static constexpr time_point
566 { return time_point(duration::min()); }
568 static constexpr time_point
570 { return time_point(duration::max()); }
577 template<typename _ToDur, typename _Clock, typename _Dur>
578 constexpr typename enable_if<__is_duration<_ToDur>::value,
579 time_point<_Clock, _ToDur>>::type
580 time_point_cast(const time_point<_Clock, _Dur>& __t)
582 typedef time_point<_Clock, _ToDur> __time_point;
583 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
586 template<typename _Clock, typename _Dur1,
587 typename _Rep2, typename _Period2>
588 constexpr time_point<_Clock,
589 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
590 operator+(const time_point<_Clock, _Dur1>& __lhs,
591 const duration<_Rep2, _Period2>& __rhs)
593 typedef duration<_Rep2, _Period2> __dur2;
594 typedef typename common_type<_Dur1,__dur2>::type __ct;
595 typedef time_point<_Clock, __ct> __time_point;
596 return __time_point(__lhs.time_since_epoch() + __rhs);
599 template<typename _Rep1, typename _Period1,
600 typename _Clock, typename _Dur2>
601 constexpr time_point<_Clock,
602 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
603 operator+(const duration<_Rep1, _Period1>& __lhs,
604 const time_point<_Clock, _Dur2>& __rhs)
606 typedef duration<_Rep1, _Period1> __dur1;
607 typedef typename common_type<__dur1,_Dur2>::type __ct;
608 typedef time_point<_Clock, __ct> __time_point;
609 return __time_point(__rhs.time_since_epoch() + __lhs);
612 template<typename _Clock, typename _Dur1,
613 typename _Rep2, typename _Period2>
614 constexpr time_point<_Clock,
615 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
616 operator-(const time_point<_Clock, _Dur1>& __lhs,
617 const duration<_Rep2, _Period2>& __rhs)
619 typedef duration<_Rep2, _Period2> __dur2;
620 typedef typename common_type<_Dur1,__dur2>::type __ct;
621 typedef time_point<_Clock, __ct> __time_point;
622 return __time_point(__lhs.time_since_epoch() -__rhs);
625 template<typename _Clock, typename _Dur1, typename _Dur2>
626 constexpr typename common_type<_Dur1, _Dur2>::type
627 operator-(const time_point<_Clock, _Dur1>& __lhs,
628 const time_point<_Clock, _Dur2>& __rhs)
629 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
631 template<typename _Clock, typename _Dur1, typename _Dur2>
633 operator==(const time_point<_Clock, _Dur1>& __lhs,
634 const time_point<_Clock, _Dur2>& __rhs)
635 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
637 template<typename _Clock, typename _Dur1, typename _Dur2>
639 operator!=(const time_point<_Clock, _Dur1>& __lhs,
640 const time_point<_Clock, _Dur2>& __rhs)
641 { return !(__lhs == __rhs); }
643 template<typename _Clock, typename _Dur1, typename _Dur2>
645 operator<(const time_point<_Clock, _Dur1>& __lhs,
646 const time_point<_Clock, _Dur2>& __rhs)
647 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
649 template<typename _Clock, typename _Dur1, typename _Dur2>
651 operator<=(const time_point<_Clock, _Dur1>& __lhs,
652 const time_point<_Clock, _Dur2>& __rhs)
653 { return !(__rhs < __lhs); }
655 template<typename _Clock, typename _Dur1, typename _Dur2>
657 operator>(const time_point<_Clock, _Dur1>& __lhs,
658 const time_point<_Clock, _Dur2>& __rhs)
659 { return __rhs < __lhs; }
661 template<typename _Clock, typename _Dur1, typename _Dur2>
663 operator>=(const time_point<_Clock, _Dur1>& __lhs,
664 const time_point<_Clock, _Dur2>& __rhs)
665 { return !(__lhs < __rhs); }
670 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
671 typedef chrono::nanoseconds duration;
672 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
673 typedef chrono::microseconds duration;
675 typedef chrono::seconds duration;
678 typedef duration::rep rep;
679 typedef duration::period period;
680 typedef chrono::time_point<system_clock, duration> time_point;
682 static_assert(system_clock::duration::min()
683 < system_clock::duration::zero(),
684 "a clock's minimum duration cannot be less than its epoch");
686 static constexpr bool is_steady = false;
693 to_time_t(const time_point& __t) noexcept
695 return std::time_t(duration_cast<chrono::seconds>
696 (__t.time_since_epoch()).count());
700 from_time_t(std::time_t __t) noexcept
702 typedef chrono::time_point<system_clock, seconds> __from;
703 return time_point_cast<system_clock::duration>
704 (__from(chrono::seconds(__t)));
708 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
712 typedef chrono::nanoseconds duration;
713 typedef duration::rep rep;
714 typedef duration::period period;
715 typedef chrono::time_point<steady_clock, duration> time_point;
717 static constexpr bool is_steady = true;
723 typedef system_clock steady_clock;
726 typedef system_clock high_resolution_clock;
728 _GLIBCXX_END_NAMESPACE_VERSION
729 } // namespace chrono
734 #endif //_GLIBCXX_USE_C99_STDINT_TR1
736 #endif //__GXX_EXPERIMENTAL_CXX0X__
738 #endif //_GLIBCXX_CHRONO