1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008, 2009, 2010 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 _GLIBCXX_BEGIN_NAMESPACE(std)
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 template<typename _Rep, typename _Period = ratio<1>>
63 template<typename _Clock, typename _Duration = typename _Clock::duration>
67 // 20.8.2.3 specialization of common_type (for duration)
68 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
69 struct common_type<chrono::duration<_Rep1, _Period1>,
70 chrono::duration<_Rep2, _Period2>>
72 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
73 ratio<__static_gcd<_Period1::num, _Period2::num>::value,
74 (_Period1::den / __static_gcd<_Period1::den, _Period2::den>::value)
75 * _Period2::den>> type;
78 // 20.8.2.3 specialization of common_type (for time_point)
79 template<typename _Clock, typename _Duration1, typename _Duration2>
80 struct common_type<chrono::time_point<_Clock, _Duration1>,
81 chrono::time_point<_Clock, _Duration2>>
83 typedef chrono::time_point<_Clock,
84 typename common_type<_Duration1, _Duration2>::type> type;
89 // Primary template for duration_cast impl.
90 template<typename _ToDuration, typename _CF, typename _CR,
91 bool _NumIsOne = false, bool _DenIsOne = false>
92 struct __duration_cast_impl
94 template<typename _Rep, typename _Period>
95 static constexpr _ToDuration
96 __cast(const duration<_Rep, _Period>& __d)
98 return _ToDuration(static_cast<
99 typename _ToDuration::rep>(static_cast<_CR>(__d.count())
100 * static_cast<_CR>(_CF::num)
101 / static_cast<_CR>(_CF::den)));
105 template<typename _ToDuration, typename _CF, typename _CR>
106 struct __duration_cast_impl<_ToDuration, _CF, _CR, true, true>
108 template<typename _Rep, typename _Period>
109 static constexpr _ToDuration
110 __cast(const duration<_Rep, _Period>& __d)
113 static_cast<typename _ToDuration::rep>(__d.count()));
117 template<typename _ToDuration, typename _CF, typename _CR>
118 struct __duration_cast_impl<_ToDuration, _CF, _CR, true, false>
120 template<typename _Rep, typename _Period>
121 static constexpr _ToDuration
122 __cast(const duration<_Rep, _Period>& __d)
124 return _ToDuration(static_cast<typename _ToDuration::rep>(
125 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
129 template<typename _ToDuration, typename _CF, typename _CR>
130 struct __duration_cast_impl<_ToDuration, _CF, _CR, false, true>
132 template<typename _Rep, typename _Period>
133 static constexpr _ToDuration
134 __cast(const duration<_Rep, _Period>& __d)
136 return _ToDuration(static_cast<typename _ToDuration::rep>(
137 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
141 template<typename _Tp>
146 template<typename _Rep, typename _Period>
147 struct __is_duration<duration<_Rep, _Period>>
152 template<typename _ToDuration, typename _Rep, typename _Period>
153 inline constexpr typename enable_if<__is_duration<_ToDuration>::value,
155 duration_cast(const duration<_Rep, _Period>& __d)
158 ratio_divide<_Period, typename _ToDuration::period>::type __cf;
160 common_type<typename _ToDuration::rep, _Rep, intmax_t>::type __cr;
162 return __duration_cast_impl<_ToDuration, __cf, __cr,
163 __cf::num == 1, __cf::den == 1>::__cast(__d);
166 /// treat_as_floating_point
167 template<typename _Rep>
168 struct treat_as_floating_point
169 : is_floating_point<_Rep>
173 template<typename _Rep>
174 struct duration_values
176 static constexpr _Rep
180 static constexpr _Rep
182 { return numeric_limits<_Rep>::max(); }
184 static constexpr _Rep
186 { return numeric_limits<_Rep>::min(); }
194 template<intmax_t _Num, intmax_t _Den>
195 struct __is_ratio<ratio<_Num, _Den>>
200 template<typename _Rep, typename _Period>
204 typedef _Period period;
206 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
207 static_assert(__is_ratio<_Period>::value,
208 "period must be a specialization of ratio");
209 static_assert(_Period::num > 0, "period must be positive");
211 // 20.8.3.1 construction / copy / destroy
212 constexpr duration() : __r() { }
214 template<typename _Rep2, typename = typename
215 enable_if<is_convertible<_Rep2, rep>::value
216 && (treat_as_floating_point<rep>::value
217 || !treat_as_floating_point<_Rep2>::value)>::type>
218 constexpr explicit duration(const _Rep2& __rep)
219 : __r(static_cast<rep>(__rep)) { }
221 template<typename _Rep2, typename _Period2, typename = typename
222 enable_if<treat_as_floating_point<rep>::value
223 || (ratio_divide<_Period2, period>::type::den == 1
224 && !treat_as_floating_point<_Rep2>::value)>::type>
225 constexpr duration(const duration<_Rep2, _Period2>& __d)
226 : __r(duration_cast<duration>(__d).count()) { }
228 ~duration() = default;
229 duration(const duration&) = default;
230 duration& operator=(const duration&) = default;
237 // 20.8.3.3 arithmetic
244 { return duration(-__r); }
255 { return duration(__r++); }
266 { return duration(__r--); }
269 operator+=(const duration& __d)
276 operator-=(const duration& __d)
283 operator*=(const rep& __rhs)
290 operator/=(const rep& __rhs)
297 template<typename _Rep2 = rep>
298 typename enable_if<!treat_as_floating_point<_Rep2>::value,
300 operator%=(const rep& __rhs)
306 template<typename _Rep2 = rep>
307 typename enable_if<!treat_as_floating_point<_Rep2>::value,
309 operator%=(const duration& __d)
315 // 20.8.3.4 special values
316 static constexpr duration
318 { return duration(duration_values<rep>::zero()); }
320 static constexpr duration
322 { return duration(duration_values<rep>::min()); }
324 static constexpr duration
326 { return duration(duration_values<rep>::max()); }
332 template<typename _Rep1, typename _Period1,
333 typename _Rep2, typename _Period2>
334 inline typename common_type<duration<_Rep1, _Period1>,
335 duration<_Rep2, _Period2>>::type
336 operator+(const duration<_Rep1, _Period1>& __lhs,
337 const duration<_Rep2, _Period2>& __rhs)
339 typedef typename common_type<duration<_Rep1, _Period1>,
340 duration<_Rep2, _Period2>>::type __ct;
341 return __ct(__lhs) += __rhs;
344 template<typename _Rep1, typename _Period1,
345 typename _Rep2, typename _Period2>
346 inline typename common_type<duration<_Rep1, _Period1>,
347 duration<_Rep2, _Period2>>::type
348 operator-(const duration<_Rep1, _Period1>& __lhs,
349 const duration<_Rep2, _Period2>& __rhs)
351 typedef typename common_type<duration<_Rep1, _Period1>,
352 duration<_Rep2, _Period2>>::type __ct;
353 return __ct(__lhs) -= __rhs;
356 template<typename _Rep1, typename _Rep2, bool =
357 is_convertible<_Rep2,
358 typename common_type<_Rep1, _Rep2>::type>::value>
359 struct __common_rep_type { };
361 template<typename _Rep1, typename _Rep2>
362 struct __common_rep_type<_Rep1, _Rep2, true>
363 { typedef typename common_type<_Rep1, _Rep2>::type type; };
365 template<typename _Rep1, typename _Period, typename _Rep2>
366 inline duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period>
367 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
369 typedef typename common_type<_Rep1, _Rep2>::type __cr;
370 return duration<__cr, _Period>(__d) *= __s;
373 template<typename _Rep1, typename _Period, typename _Rep2>
374 inline duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period>
375 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
376 { return __d * __s; }
378 template<typename _Rep1, typename _Period, typename _Rep2>
379 inline duration<typename __common_rep_type<_Rep1, typename
380 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
381 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
383 typedef typename common_type<_Rep1, _Rep2>::type __cr;
384 return duration<__cr, _Period>(__d) /= __s;
387 template<typename _Rep1, typename _Period1,
388 typename _Rep2, typename _Period2>
389 inline typename common_type<_Rep1, _Rep2>::type
390 operator/(const duration<_Rep1, _Period1>& __lhs,
391 const duration<_Rep2, _Period2>& __rhs)
393 typedef typename common_type<duration<_Rep1, _Period1>,
394 duration<_Rep2, _Period2>>::type __ct;
395 return __ct(__lhs).count() / __ct(__rhs).count();
399 template<typename _Rep1, typename _Period, typename _Rep2>
400 inline duration<typename __common_rep_type<_Rep1, typename
401 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period>
402 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
404 typedef typename common_type<_Rep1, _Rep2>::type __cr;
405 return duration<__cr, _Period>(__d) %= __s;
408 template<typename _Rep1, typename _Period1,
409 typename _Rep2, typename _Period2>
410 inline typename common_type<duration<_Rep1, _Period1>,
411 duration<_Rep2, _Period2>>::type
412 operator%(const duration<_Rep1, _Period1>& __lhs,
413 const duration<_Rep2, _Period2>& __rhs)
415 typedef typename common_type<duration<_Rep1, _Period1>,
416 duration<_Rep2, _Period2>>::type __ct;
417 return __ct(__lhs) %= __rhs;
421 template<typename _Rep1, typename _Period1,
422 typename _Rep2, typename _Period2>
423 inline constexpr bool
424 operator==(const duration<_Rep1, _Period1>& __lhs,
425 const duration<_Rep2, _Period2>& __rhs)
427 typedef typename common_type<duration<_Rep1, _Period1>,
428 duration<_Rep2, _Period2>>::type __ct;
429 return __ct(__lhs).count() == __ct(__rhs).count();
432 template<typename _Rep1, typename _Period1,
433 typename _Rep2, typename _Period2>
434 inline constexpr bool
435 operator<(const duration<_Rep1, _Period1>& __lhs,
436 const duration<_Rep2, _Period2>& __rhs)
438 typedef typename common_type<duration<_Rep1, _Period1>,
439 duration<_Rep2, _Period2>>::type __ct;
440 return __ct(__lhs).count() < __ct(__rhs).count();
443 template<typename _Rep1, typename _Period1,
444 typename _Rep2, typename _Period2>
445 inline constexpr bool
446 operator!=(const duration<_Rep1, _Period1>& __lhs,
447 const duration<_Rep2, _Period2>& __rhs)
448 { return !(__lhs == __rhs); }
450 template<typename _Rep1, typename _Period1,
451 typename _Rep2, typename _Period2>
452 inline constexpr bool
453 operator<=(const duration<_Rep1, _Period1>& __lhs,
454 const duration<_Rep2, _Period2>& __rhs)
455 { return !(__rhs < __lhs); }
457 template<typename _Rep1, typename _Period1,
458 typename _Rep2, typename _Period2>
459 inline constexpr bool
460 operator>(const duration<_Rep1, _Period1>& __lhs,
461 const duration<_Rep2, _Period2>& __rhs)
462 { return __rhs < __lhs; }
464 template<typename _Rep1, typename _Period1,
465 typename _Rep2, typename _Period2>
466 inline constexpr bool
467 operator>=(const duration<_Rep1, _Period1>& __lhs,
468 const duration<_Rep2, _Period2>& __rhs)
469 { return !(__lhs < __rhs); }
472 typedef duration<int64_t, nano> nanoseconds;
475 typedef duration<int64_t, micro> microseconds;
478 typedef duration<int64_t, milli> milliseconds;
481 typedef duration<int64_t> seconds;
484 typedef duration<int, ratio< 60>> minutes;
487 typedef duration<int, ratio<3600>> hours;
490 template<typename _Clock, typename _Duration>
493 typedef _Clock clock;
494 typedef _Duration duration;
495 typedef typename duration::rep rep;
496 typedef typename duration::period period;
498 constexpr time_point() : __d(duration::zero())
501 constexpr explicit time_point(const duration& __dur)
506 template<typename _Duration2>
507 constexpr time_point(const time_point<clock, _Duration2>& __t)
508 : __d(__t.time_since_epoch())
513 time_since_epoch() const
518 operator+=(const duration& __dur)
525 operator-=(const duration& __dur)
532 static constexpr time_point
534 { return time_point(duration::min()); }
536 static constexpr time_point
538 { return time_point(duration::max()); }
545 template<typename _ToDuration, typename _Clock, typename _Duration>
546 inline constexpr typename enable_if<__is_duration<_ToDuration>::value,
547 time_point<_Clock, _ToDuration>>::type
548 time_point_cast(const time_point<_Clock, _Duration>& __t)
550 return time_point<_Clock, _ToDuration>(
551 duration_cast<_ToDuration>(__t.time_since_epoch()));
554 template<typename _Clock, typename _Duration1,
555 typename _Rep2, typename _Period2>
556 inline time_point<_Clock,
557 typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
558 operator+(const time_point<_Clock, _Duration1>& __lhs,
559 const duration<_Rep2, _Period2>& __rhs)
561 typedef time_point<_Clock,
562 typename common_type<_Duration1,
563 duration<_Rep2, _Period2>>::type> __ct;
564 return __ct(__lhs) += __rhs;
567 template<typename _Rep1, typename _Period1,
568 typename _Clock, typename _Duration2>
569 inline time_point<_Clock,
570 typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
571 operator+(const duration<_Rep1, _Period1>& __lhs,
572 const time_point<_Clock, _Duration2>& __rhs)
573 { return __rhs + __lhs; }
575 template<typename _Clock, typename _Duration1,
576 typename _Rep2, typename _Period2>
577 inline time_point<_Clock,
578 typename common_type<_Duration1, duration<_Rep2, _Period2>>::type>
579 operator-(const time_point<_Clock, _Duration1>& __lhs,
580 const duration<_Rep2, _Period2>& __rhs)
581 { return __lhs + (-__rhs); }
583 template<typename _Clock, typename _Duration1, typename _Duration2>
584 inline typename common_type<_Duration1, _Duration2>::type
585 operator-(const time_point<_Clock, _Duration1>& __lhs,
586 const time_point<_Clock, _Duration2>& __rhs)
587 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
589 template<typename _Clock, typename _Duration1, typename _Duration2>
590 inline constexpr bool
591 operator==(const time_point<_Clock, _Duration1>& __lhs,
592 const time_point<_Clock, _Duration2>& __rhs)
593 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
595 template<typename _Clock, typename _Duration1, typename _Duration2>
596 inline constexpr bool
597 operator!=(const time_point<_Clock, _Duration1>& __lhs,
598 const time_point<_Clock, _Duration2>& __rhs)
599 { return !(__lhs == __rhs); }
601 template<typename _Clock, typename _Duration1, typename _Duration2>
602 inline constexpr bool
603 operator<(const time_point<_Clock, _Duration1>& __lhs,
604 const time_point<_Clock, _Duration2>& __rhs)
605 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
607 template<typename _Clock, typename _Duration1, typename _Duration2>
608 inline constexpr bool
609 operator<=(const time_point<_Clock, _Duration1>& __lhs,
610 const time_point<_Clock, _Duration2>& __rhs)
611 { return !(__rhs < __lhs); }
613 template<typename _Clock, typename _Duration1, typename _Duration2>
614 inline constexpr bool
615 operator>(const time_point<_Clock, _Duration1>& __lhs,
616 const time_point<_Clock, _Duration2>& __rhs)
617 { return __rhs < __lhs; }
619 template<typename _Clock, typename _Duration1, typename _Duration2>
620 inline constexpr bool
621 operator>=(const time_point<_Clock, _Duration1>& __lhs,
622 const time_point<_Clock, _Duration2>& __rhs)
623 { return !(__lhs < __rhs); }
628 #ifdef _GLIBCXX_USE_CLOCK_REALTIME
629 typedef chrono::nanoseconds duration;
630 #elif defined(_GLIBCXX_USE_GETTIMEOFDAY)
631 typedef chrono::microseconds duration;
633 typedef chrono::seconds duration;
636 typedef duration::rep rep;
637 typedef duration::period period;
638 typedef chrono::time_point<system_clock, duration> time_point;
640 static_assert(system_clock::duration::min()
641 < system_clock::duration::zero(),
642 "a clock's minimum duration cannot be less than its epoch");
644 static const bool is_monotonic = false;
651 to_time_t(const time_point& __t)
654 duration_cast<chrono::seconds>(__t.time_since_epoch()).count());
658 from_time_t(std::time_t __t)
660 return time_point_cast<system_clock::duration>(
661 chrono::time_point<system_clock, chrono::seconds>(
662 chrono::seconds(__t)));
666 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
668 struct monotonic_clock
670 typedef chrono::nanoseconds duration;
671 typedef duration::rep rep;
672 typedef duration::period period;
673 typedef chrono::time_point<monotonic_clock, duration> time_point;
675 static const bool is_monotonic = true;
681 typedef system_clock monotonic_clock;
684 typedef system_clock high_resolution_clock;
685 } // namespace chrono
688 _GLIBCXX_END_NAMESPACE
690 #endif //_GLIBCXX_USE_C99_STDINT_TR1
692 #endif //__GXX_EXPERIMENTAL_CXX0X__
694 #endif //_GLIBCXX_CHRONO