1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008-2013 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 #if __cplusplus < 201103L
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++ 2011 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
73 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
75 template<typename _CT, typename _Period1, typename _Period2>
76 struct __duration_common_type_wrapper
79 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
80 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
81 typedef typename _CT::type __cr;
82 typedef ratio<__gcd_num::value,
83 (_Period1::den / __gcd_den::value) * _Period2::den> __r;
85 typedef __success_type<chrono::duration<__cr, __r>> type;
88 template<typename _Period1, typename _Period2>
89 struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
90 { typedef __failure_type type; };
92 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
93 struct common_type<chrono::duration<_Rep1, _Period1>,
94 chrono::duration<_Rep2, _Period2>>
95 : public __duration_common_type_wrapper<typename __member_type_wrapper<
96 common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
99 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
101 template<typename _CT, typename _Clock>
102 struct __timepoint_common_type_wrapper
104 typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
108 template<typename _Clock>
109 struct __timepoint_common_type_wrapper<__failure_type, _Clock>
110 { typedef __failure_type type; };
112 template<typename _Clock, typename _Duration1, typename _Duration2>
113 struct common_type<chrono::time_point<_Clock, _Duration1>,
114 chrono::time_point<_Clock, _Duration2>>
115 : public __timepoint_common_type_wrapper<typename __member_type_wrapper<
116 common_type<_Duration1, _Duration2>>::type, _Clock>::type
119 _GLIBCXX_END_NAMESPACE_VERSION
123 _GLIBCXX_BEGIN_NAMESPACE_VERSION
125 // Primary template for duration_cast impl.
126 template<typename _ToDur, typename _CF, typename _CR,
127 bool _NumIsOne = false, bool _DenIsOne = false>
128 struct __duration_cast_impl
130 template<typename _Rep, typename _Period>
131 static constexpr _ToDur
132 __cast(const duration<_Rep, _Period>& __d)
134 typedef typename _ToDur::rep __to_rep;
135 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
136 * static_cast<_CR>(_CF::num)
137 / static_cast<_CR>(_CF::den)));
141 template<typename _ToDur, typename _CF, typename _CR>
142 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
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>(__d.count()));
153 template<typename _ToDur, typename _CF, typename _CR>
154 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
156 template<typename _Rep, typename _Period>
157 static constexpr _ToDur
158 __cast(const duration<_Rep, _Period>& __d)
160 typedef typename _ToDur::rep __to_rep;
161 return _ToDur(static_cast<__to_rep>(
162 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
166 template<typename _ToDur, typename _CF, typename _CR>
167 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
169 template<typename _Rep, typename _Period>
170 static constexpr _ToDur
171 __cast(const duration<_Rep, _Period>& __d)
173 typedef typename _ToDur::rep __to_rep;
174 return _ToDur(static_cast<__to_rep>(
175 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
179 template<typename _Tp>
184 template<typename _Rep, typename _Period>
185 struct __is_duration<duration<_Rep, _Period>>
190 template<typename _ToDur, typename _Rep, typename _Period>
191 constexpr typename enable_if<__is_duration<_ToDur>::value,
193 duration_cast(const duration<_Rep, _Period>& __d)
195 typedef typename _ToDur::period __to_period;
196 typedef typename _ToDur::rep __to_rep;
197 typedef ratio_divide<_Period, __to_period> __cf;
198 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
200 typedef __duration_cast_impl<_ToDur, __cf, __cr,
201 __cf::num == 1, __cf::den == 1> __dc;
202 return __dc::__cast(__d);
205 /// treat_as_floating_point
206 template<typename _Rep>
207 struct treat_as_floating_point
208 : is_floating_point<_Rep>
212 template<typename _Rep>
213 struct duration_values
215 static constexpr _Rep
219 static constexpr _Rep
221 { return numeric_limits<_Rep>::max(); }
223 static constexpr _Rep
225 { return numeric_limits<_Rep>::lowest(); }
228 template<typename _Tp>
233 template<intmax_t _Num, intmax_t _Den>
234 struct __is_ratio<ratio<_Num, _Den>>
239 template<typename _Rep, typename _Period>
243 typedef _Period period;
245 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
246 static_assert(__is_ratio<_Period>::value,
247 "period must be a specialization of ratio");
248 static_assert(_Period::num > 0, "period must be positive");
250 // 20.11.5.1 construction / copy / destroy
251 constexpr duration() = default;
253 // NB: Make constexpr implicit. This cannot be explicitly
254 // constexpr, as any UDT that is not a literal type with a
255 // constexpr copy constructor will be ill-formed.
256 duration(const duration&) = default;
258 template<typename _Rep2, typename = typename
259 enable_if<is_convertible<_Rep2, rep>::value
260 && (treat_as_floating_point<rep>::value
261 || !treat_as_floating_point<_Rep2>::value)>::type>
262 constexpr explicit duration(const _Rep2& __rep)
263 : __r(static_cast<rep>(__rep)) { }
265 template<typename _Rep2, typename _Period2, typename = typename
266 enable_if<treat_as_floating_point<rep>::value
267 || (ratio_divide<_Period2, period>::den == 1
268 && !treat_as_floating_point<_Rep2>::value)>::type>
269 constexpr duration(const duration<_Rep2, _Period2>& __d)
270 : __r(duration_cast<duration>(__d).count()) { }
272 ~duration() = default;
273 duration& operator=(const duration&) = default;
275 // 20.11.5.2 observer
280 // 20.11.5.3 arithmetic
287 { return duration(-__r); }
298 { return duration(__r++); }
309 { return duration(__r--); }
312 operator+=(const duration& __d)
319 operator-=(const duration& __d)
326 operator*=(const rep& __rhs)
333 operator/=(const rep& __rhs)
340 template<typename _Rep2 = rep>
341 typename enable_if<!treat_as_floating_point<_Rep2>::value,
343 operator%=(const rep& __rhs)
349 template<typename _Rep2 = rep>
350 typename enable_if<!treat_as_floating_point<_Rep2>::value,
352 operator%=(const duration& __d)
358 // 20.11.5.4 special values
359 static constexpr duration
361 { return duration(duration_values<rep>::zero()); }
363 static constexpr duration
365 { return duration(duration_values<rep>::min()); }
367 static constexpr duration
369 { return duration(duration_values<rep>::max()); }
375 template<typename _Rep1, typename _Period1,
376 typename _Rep2, typename _Period2>
377 constexpr typename common_type<duration<_Rep1, _Period1>,
378 duration<_Rep2, _Period2>>::type
379 operator+(const duration<_Rep1, _Period1>& __lhs,
380 const duration<_Rep2, _Period2>& __rhs)
382 typedef duration<_Rep1, _Period1> __dur1;
383 typedef duration<_Rep2, _Period2> __dur2;
384 typedef typename common_type<__dur1,__dur2>::type __cd;
385 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
388 template<typename _Rep1, typename _Period1,
389 typename _Rep2, typename _Period2>
390 constexpr typename common_type<duration<_Rep1, _Period1>,
391 duration<_Rep2, _Period2>>::type
392 operator-(const duration<_Rep1, _Period1>& __lhs,
393 const duration<_Rep2, _Period2>& __rhs)
395 typedef duration<_Rep1, _Period1> __dur1;
396 typedef duration<_Rep2, _Period2> __dur2;
397 typedef typename common_type<__dur1,__dur2>::type __cd;
398 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
401 template<typename _Rep1, typename _Rep2, bool =
402 is_convertible<_Rep2,
403 typename common_type<_Rep1, _Rep2>::type>::value>
404 struct __common_rep_type { };
406 template<typename _Rep1, typename _Rep2>
407 struct __common_rep_type<_Rep1, _Rep2, true>
408 { typedef typename common_type<_Rep1, _Rep2>::type type; };
410 template<typename _Rep1, typename _Period, typename _Rep2>
412 duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
413 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
415 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
417 return __cd(__cd(__d).count() * __s);
420 template<typename _Rep1, typename _Rep2, typename _Period>
422 duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
423 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
424 { return __d * __s; }
426 template<typename _Rep1, typename _Period, typename _Rep2>
427 constexpr duration<typename __common_rep_type<_Rep1, typename
428 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
429 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
431 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
433 return __cd(__cd(__d).count() / __s);
436 template<typename _Rep1, typename _Period1,
437 typename _Rep2, typename _Period2>
438 constexpr typename common_type<_Rep1, _Rep2>::type
439 operator/(const duration<_Rep1, _Period1>& __lhs,
440 const duration<_Rep2, _Period2>& __rhs)
442 typedef duration<_Rep1, _Period1> __dur1;
443 typedef duration<_Rep2, _Period2> __dur2;
444 typedef typename common_type<__dur1,__dur2>::type __cd;
445 return __cd(__lhs).count() / __cd(__rhs).count();
449 template<typename _Rep1, typename _Period, typename _Rep2>
450 constexpr duration<typename __common_rep_type<_Rep1, typename
451 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
452 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
454 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
456 return __cd(__cd(__d).count() % __s);
459 template<typename _Rep1, typename _Period1,
460 typename _Rep2, typename _Period2>
461 constexpr typename common_type<duration<_Rep1, _Period1>,
462 duration<_Rep2, _Period2>>::type
463 operator%(const duration<_Rep1, _Period1>& __lhs,
464 const duration<_Rep2, _Period2>& __rhs)
466 typedef duration<_Rep1, _Period1> __dur1;
467 typedef duration<_Rep2, _Period2> __dur2;
468 typedef typename common_type<__dur1,__dur2>::type __cd;
469 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
473 template<typename _Rep1, typename _Period1,
474 typename _Rep2, typename _Period2>
476 operator==(const duration<_Rep1, _Period1>& __lhs,
477 const duration<_Rep2, _Period2>& __rhs)
479 typedef duration<_Rep1, _Period1> __dur1;
480 typedef duration<_Rep2, _Period2> __dur2;
481 typedef typename common_type<__dur1,__dur2>::type __ct;
482 return __ct(__lhs).count() == __ct(__rhs).count();
485 template<typename _Rep1, typename _Period1,
486 typename _Rep2, typename _Period2>
488 operator<(const duration<_Rep1, _Period1>& __lhs,
489 const duration<_Rep2, _Period2>& __rhs)
491 typedef duration<_Rep1, _Period1> __dur1;
492 typedef duration<_Rep2, _Period2> __dur2;
493 typedef typename common_type<__dur1,__dur2>::type __ct;
494 return __ct(__lhs).count() < __ct(__rhs).count();
497 template<typename _Rep1, typename _Period1,
498 typename _Rep2, typename _Period2>
500 operator!=(const duration<_Rep1, _Period1>& __lhs,
501 const duration<_Rep2, _Period2>& __rhs)
502 { return !(__lhs == __rhs); }
504 template<typename _Rep1, typename _Period1,
505 typename _Rep2, typename _Period2>
507 operator<=(const duration<_Rep1, _Period1>& __lhs,
508 const duration<_Rep2, _Period2>& __rhs)
509 { return !(__rhs < __lhs); }
511 template<typename _Rep1, typename _Period1,
512 typename _Rep2, typename _Period2>
514 operator>(const duration<_Rep1, _Period1>& __lhs,
515 const duration<_Rep2, _Period2>& __rhs)
516 { return __rhs < __lhs; }
518 template<typename _Rep1, typename _Period1,
519 typename _Rep2, typename _Period2>
521 operator>=(const duration<_Rep1, _Period1>& __lhs,
522 const duration<_Rep2, _Period2>& __rhs)
523 { return !(__lhs < __rhs); }
526 typedef duration<int64_t, nano> nanoseconds;
529 typedef duration<int64_t, micro> microseconds;
532 typedef duration<int64_t, milli> milliseconds;
535 typedef duration<int64_t> seconds;
538 typedef duration<int, ratio< 60>> minutes;
541 typedef duration<int, ratio<3600>> hours;
544 template<typename _Clock, typename _Dur>
547 typedef _Clock clock;
548 typedef _Dur duration;
549 typedef typename duration::rep rep;
550 typedef typename duration::period period;
552 constexpr time_point() : __d(duration::zero())
555 constexpr explicit time_point(const duration& __dur)
560 template<typename _Dur2>
561 constexpr time_point(const time_point<clock, _Dur2>& __t)
562 : __d(__t.time_since_epoch())
567 time_since_epoch() const
572 operator+=(const duration& __dur)
579 operator-=(const duration& __dur)
586 static constexpr time_point
588 { return time_point(duration::min()); }
590 static constexpr time_point
592 { return time_point(duration::max()); }
599 template<typename _ToDur, typename _Clock, typename _Dur>
600 constexpr typename enable_if<__is_duration<_ToDur>::value,
601 time_point<_Clock, _ToDur>>::type
602 time_point_cast(const time_point<_Clock, _Dur>& __t)
604 typedef time_point<_Clock, _ToDur> __time_point;
605 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
608 template<typename _Clock, typename _Dur1,
609 typename _Rep2, typename _Period2>
610 constexpr time_point<_Clock,
611 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
612 operator+(const time_point<_Clock, _Dur1>& __lhs,
613 const duration<_Rep2, _Period2>& __rhs)
615 typedef duration<_Rep2, _Period2> __dur2;
616 typedef typename common_type<_Dur1,__dur2>::type __ct;
617 typedef time_point<_Clock, __ct> __time_point;
618 return __time_point(__lhs.time_since_epoch() + __rhs);
621 template<typename _Rep1, typename _Period1,
622 typename _Clock, typename _Dur2>
623 constexpr time_point<_Clock,
624 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
625 operator+(const duration<_Rep1, _Period1>& __lhs,
626 const time_point<_Clock, _Dur2>& __rhs)
628 typedef duration<_Rep1, _Period1> __dur1;
629 typedef typename common_type<__dur1,_Dur2>::type __ct;
630 typedef time_point<_Clock, __ct> __time_point;
631 return __time_point(__rhs.time_since_epoch() + __lhs);
634 template<typename _Clock, typename _Dur1,
635 typename _Rep2, typename _Period2>
636 constexpr time_point<_Clock,
637 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
638 operator-(const time_point<_Clock, _Dur1>& __lhs,
639 const duration<_Rep2, _Period2>& __rhs)
641 typedef duration<_Rep2, _Period2> __dur2;
642 typedef typename common_type<_Dur1,__dur2>::type __ct;
643 typedef time_point<_Clock, __ct> __time_point;
644 return __time_point(__lhs.time_since_epoch() -__rhs);
647 template<typename _Clock, typename _Dur1, typename _Dur2>
648 constexpr typename common_type<_Dur1, _Dur2>::type
649 operator-(const time_point<_Clock, _Dur1>& __lhs,
650 const time_point<_Clock, _Dur2>& __rhs)
651 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
653 template<typename _Clock, typename _Dur1, typename _Dur2>
655 operator==(const time_point<_Clock, _Dur1>& __lhs,
656 const time_point<_Clock, _Dur2>& __rhs)
657 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
659 template<typename _Clock, typename _Dur1, typename _Dur2>
661 operator!=(const time_point<_Clock, _Dur1>& __lhs,
662 const time_point<_Clock, _Dur2>& __rhs)
663 { return !(__lhs == __rhs); }
665 template<typename _Clock, typename _Dur1, typename _Dur2>
667 operator<(const time_point<_Clock, _Dur1>& __lhs,
668 const time_point<_Clock, _Dur2>& __rhs)
669 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
671 template<typename _Clock, typename _Dur1, typename _Dur2>
673 operator<=(const time_point<_Clock, _Dur1>& __lhs,
674 const time_point<_Clock, _Dur2>& __rhs)
675 { return !(__rhs < __lhs); }
677 template<typename _Clock, typename _Dur1, typename _Dur2>
679 operator>(const time_point<_Clock, _Dur1>& __lhs,
680 const time_point<_Clock, _Dur2>& __rhs)
681 { return __rhs < __lhs; }
683 template<typename _Clock, typename _Dur1, typename _Dur2>
685 operator>=(const time_point<_Clock, _Dur1>& __lhs,
686 const time_point<_Clock, _Dur2>& __rhs)
687 { return !(__lhs < __rhs); }
692 // Why nanosecond resolution as the default?
693 // Why have std::system_clock always count in the higest
694 // resolution (ie nanoseconds), even if on some OSes the low 3
695 // or 9 decimal digits will be always zero? This allows later
696 // implementations to change the system_clock::now()
697 // implementation any time to provide better resolution without
698 // changing function signature or units.
700 // To support the (forward) evolution of the library's defined
701 // clocks, wrap inside inline namespace so that the current
702 // defintions of system_clock, steady_clock, and
703 // high_resolution_clock types are uniquely mangled. This way, new
704 // code can use the latests clocks, while the library can contain
705 // compatibility definitions for previous versions. At some
706 // point, when these clocks settle down, the inlined namespaces
707 // can be removed. XXX GLIBCXX_ABI Deprecated
708 inline namespace _V2 {
711 * @brief System clock.
713 * Time returned represents wall time from the system-wide clock.
717 typedef chrono::nanoseconds duration;
718 typedef duration::rep rep;
719 typedef duration::period period;
720 typedef chrono::time_point<system_clock, duration> time_point;
722 static_assert(system_clock::duration::min()
723 < system_clock::duration::zero(),
724 "a clock's minimum duration cannot be less than its epoch");
726 static constexpr bool is_steady = false;
733 to_time_t(const time_point& __t) noexcept
735 return std::time_t(duration_cast<chrono::seconds>
736 (__t.time_since_epoch()).count());
740 from_time_t(std::time_t __t) noexcept
742 typedef chrono::time_point<system_clock, seconds> __from;
743 return time_point_cast<system_clock::duration>
744 (__from(chrono::seconds(__t)));
750 * @brief Monotonic clock
752 * Time returned has the property of only increasing at a uniform rate.
756 typedef chrono::nanoseconds duration;
757 typedef duration::rep rep;
758 typedef duration::period period;
759 typedef chrono::time_point<steady_clock, duration> time_point;
761 static constexpr bool is_steady = true;
769 * @brief Highest-resolution clock
771 * This is the clock "with the shortest tick period." Alias to
772 * std::system_clock until higher-than-nanosecond definitions
775 using high_resolution_clock = system_clock;
777 } // end inline namespace _V2
779 _GLIBCXX_END_NAMESPACE_VERSION
780 } // namespace chrono
785 #endif //_GLIBCXX_USE_C99_STDINT_TR1
789 #endif //_GLIBCXX_CHRONO