1 // <chrono> -*- C++ -*-
3 // Copyright (C) 2008-2019 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>
42 #include <bits/parse_numbers.h> // for literals support.
44 namespace std _GLIBCXX_VISIBILITY(default)
46 _GLIBCXX_BEGIN_NAMESPACE_VERSION
49 * @defgroup chrono Time
52 * Classes and functions for time.
56 /** @namespace std::chrono
57 * @brief ISO C++ 2011 entities sub-namespace for time and date.
61 template<typename _Rep, typename _Period = ratio<1>>
64 template<typename _Clock, typename _Dur = typename _Clock::duration>
68 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
70 template<typename _CT, typename _Period1, typename _Period2>
71 struct __duration_common_type_wrapper
74 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num;
75 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den;
76 typedef typename _CT::type __cr;
77 typedef ratio<__gcd_num::value,
78 (_Period1::den / __gcd_den::value) * _Period2::den> __r;
80 typedef __success_type<chrono::duration<__cr, __r>> type;
83 template<typename _Period1, typename _Period2>
84 struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2>
85 { typedef __failure_type type; };
87 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
88 struct common_type<chrono::duration<_Rep1, _Period1>,
89 chrono::duration<_Rep2, _Period2>>
90 : public __duration_common_type_wrapper<typename __member_type_wrapper<
91 common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type
94 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
96 template<typename _CT, typename _Clock>
97 struct __timepoint_common_type_wrapper
99 typedef __success_type<chrono::time_point<_Clock, typename _CT::type>>
103 template<typename _Clock>
104 struct __timepoint_common_type_wrapper<__failure_type, _Clock>
105 { typedef __failure_type type; };
107 template<typename _Clock, typename _Duration1, typename _Duration2>
108 struct common_type<chrono::time_point<_Clock, _Duration1>,
109 chrono::time_point<_Clock, _Duration2>>
110 : public __timepoint_common_type_wrapper<typename __member_type_wrapper<
111 common_type<_Duration1, _Duration2>>::type, _Clock>::type
116 // Primary template for duration_cast impl.
117 template<typename _ToDur, typename _CF, typename _CR,
118 bool _NumIsOne = false, bool _DenIsOne = false>
119 struct __duration_cast_impl
121 template<typename _Rep, typename _Period>
122 static constexpr _ToDur
123 __cast(const duration<_Rep, _Period>& __d)
125 typedef typename _ToDur::rep __to_rep;
126 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
127 * static_cast<_CR>(_CF::num)
128 / static_cast<_CR>(_CF::den)));
132 template<typename _ToDur, typename _CF, typename _CR>
133 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
135 template<typename _Rep, typename _Period>
136 static constexpr _ToDur
137 __cast(const duration<_Rep, _Period>& __d)
139 typedef typename _ToDur::rep __to_rep;
140 return _ToDur(static_cast<__to_rep>(__d.count()));
144 template<typename _ToDur, typename _CF, typename _CR>
145 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
147 template<typename _Rep, typename _Period>
148 static constexpr _ToDur
149 __cast(const duration<_Rep, _Period>& __d)
151 typedef typename _ToDur::rep __to_rep;
152 return _ToDur(static_cast<__to_rep>(
153 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
157 template<typename _ToDur, typename _CF, typename _CR>
158 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
160 template<typename _Rep, typename _Period>
161 static constexpr _ToDur
162 __cast(const duration<_Rep, _Period>& __d)
164 typedef typename _ToDur::rep __to_rep;
165 return _ToDur(static_cast<__to_rep>(
166 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
170 template<typename _Tp>
175 template<typename _Rep, typename _Period>
176 struct __is_duration<duration<_Rep, _Period>>
180 template<typename _Tp>
181 using __enable_if_is_duration
182 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
184 template<typename _Tp>
185 using __disable_if_is_duration
186 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
189 template<typename _ToDur, typename _Rep, typename _Period>
190 constexpr __enable_if_is_duration<_ToDur>
191 duration_cast(const duration<_Rep, _Period>& __d)
193 typedef typename _ToDur::period __to_period;
194 typedef typename _ToDur::rep __to_rep;
195 typedef ratio_divide<_Period, __to_period> __cf;
196 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
198 typedef __duration_cast_impl<_ToDur, __cf, __cr,
199 __cf::num == 1, __cf::den == 1> __dc;
200 return __dc::__cast(__d);
203 /// treat_as_floating_point
204 template<typename _Rep>
205 struct treat_as_floating_point
206 : is_floating_point<_Rep>
209 #if __cplusplus > 201402L
210 template <typename _Rep>
211 inline constexpr bool treat_as_floating_point_v =
212 treat_as_floating_point<_Rep>::value;
215 #if __cplusplus >= 201703L
216 # define __cpp_lib_chrono 201611
218 template<typename _ToDur, typename _Rep, typename _Period>
219 constexpr __enable_if_is_duration<_ToDur>
220 floor(const duration<_Rep, _Period>& __d)
222 auto __to = chrono::duration_cast<_ToDur>(__d);
224 return __to - _ToDur{1};
228 template<typename _ToDur, typename _Rep, typename _Period>
229 constexpr __enable_if_is_duration<_ToDur>
230 ceil(const duration<_Rep, _Period>& __d)
232 auto __to = chrono::duration_cast<_ToDur>(__d);
234 return __to + _ToDur{1};
238 template <typename _ToDur, typename _Rep, typename _Period>
239 constexpr enable_if_t<
240 __and_<__is_duration<_ToDur>,
241 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
243 round(const duration<_Rep, _Period>& __d)
245 _ToDur __t0 = chrono::floor<_ToDur>(__d);
246 _ToDur __t1 = __t0 + _ToDur{1};
247 auto __diff0 = __d - __t0;
248 auto __diff1 = __t1 - __d;
249 if (__diff0 == __diff1)
251 if (__t0.count() & 1)
255 else if (__diff0 < __diff1)
260 template<typename _Rep, typename _Period>
262 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
263 abs(duration<_Rep, _Period> __d)
265 if (__d >= __d.zero())
272 template<typename _Rep>
273 struct duration_values
275 static constexpr _Rep
279 static constexpr _Rep
281 { return numeric_limits<_Rep>::max(); }
283 static constexpr _Rep
285 { return numeric_limits<_Rep>::lowest(); }
288 template<typename _Tp>
293 template<intmax_t _Num, intmax_t _Den>
294 struct __is_ratio<ratio<_Num, _Den>>
299 template<typename _Rep, typename _Period>
303 template<typename _Rep2>
304 using __is_float = treat_as_floating_point<_Rep2>;
306 // _Period2 is an exact multiple of _Period
307 template<typename _Period2>
309 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
314 typedef _Period period;
316 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
317 static_assert(__is_ratio<_Period>::value,
318 "period must be a specialization of ratio");
319 static_assert(_Period::num > 0, "period must be positive");
321 // 20.11.5.1 construction / copy / destroy
322 constexpr duration() = default;
324 duration(const duration&) = default;
326 // _GLIBCXX_RESOLVE_LIB_DEFECTS
327 // 3050. Conversion specification problem in chrono::duration
328 template<typename _Rep2, typename = _Require<
329 is_convertible<const _Rep2&, rep>,
330 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
331 constexpr explicit duration(const _Rep2& __rep)
332 : __r(static_cast<rep>(__rep)) { }
334 template<typename _Rep2, typename _Period2, typename = _Require<
335 __or_<__is_float<rep>,
336 __and_<__is_harmonic<_Period2>,
337 __not_<__is_float<_Rep2>>>>>>
338 constexpr duration(const duration<_Rep2, _Period2>& __d)
339 : __r(duration_cast<duration>(__d).count()) { }
341 ~duration() = default;
342 duration& operator=(const duration&) = default;
344 // 20.11.5.2 observer
349 // 20.11.5.3 arithmetic
356 { return duration(-__r); }
358 _GLIBCXX17_CONSTEXPR duration&
365 _GLIBCXX17_CONSTEXPR duration
367 { return duration(__r++); }
369 _GLIBCXX17_CONSTEXPR duration&
376 _GLIBCXX17_CONSTEXPR duration
378 { return duration(__r--); }
380 _GLIBCXX17_CONSTEXPR duration&
381 operator+=(const duration& __d)
387 _GLIBCXX17_CONSTEXPR duration&
388 operator-=(const duration& __d)
394 _GLIBCXX17_CONSTEXPR duration&
395 operator*=(const rep& __rhs)
401 _GLIBCXX17_CONSTEXPR duration&
402 operator/=(const rep& __rhs)
409 template<typename _Rep2 = rep>
411 typename enable_if<!treat_as_floating_point<_Rep2>::value,
413 operator%=(const rep& __rhs)
419 template<typename _Rep2 = rep>
421 typename enable_if<!treat_as_floating_point<_Rep2>::value,
423 operator%=(const duration& __d)
429 // 20.11.5.4 special values
430 static constexpr duration
432 { return duration(duration_values<rep>::zero()); }
434 static constexpr duration
436 { return duration(duration_values<rep>::min()); }
438 static constexpr duration
440 { return duration(duration_values<rep>::max()); }
446 template<typename _Rep1, typename _Period1,
447 typename _Rep2, typename _Period2>
448 constexpr typename common_type<duration<_Rep1, _Period1>,
449 duration<_Rep2, _Period2>>::type
450 operator+(const duration<_Rep1, _Period1>& __lhs,
451 const duration<_Rep2, _Period2>& __rhs)
453 typedef duration<_Rep1, _Period1> __dur1;
454 typedef duration<_Rep2, _Period2> __dur2;
455 typedef typename common_type<__dur1,__dur2>::type __cd;
456 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
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());
472 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
473 // is implicitly convertible to it.
474 // _GLIBCXX_RESOLVE_LIB_DEFECTS
475 // 3050. Conversion specification problem in chrono::duration constructor
476 template<typename _Rep1, typename _Rep2,
477 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
478 using __common_rep_t = typename
479 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
481 template<typename _Rep1, typename _Period, typename _Rep2>
482 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
483 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
485 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
487 return __cd(__cd(__d).count() * __s);
490 template<typename _Rep1, typename _Rep2, typename _Period>
491 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
492 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
493 { return __d * __s; }
495 template<typename _Rep1, typename _Period, typename _Rep2>
497 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
498 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
500 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
502 return __cd(__cd(__d).count() / __s);
505 template<typename _Rep1, typename _Period1,
506 typename _Rep2, typename _Period2>
507 constexpr typename common_type<_Rep1, _Rep2>::type
508 operator/(const duration<_Rep1, _Period1>& __lhs,
509 const duration<_Rep2, _Period2>& __rhs)
511 typedef duration<_Rep1, _Period1> __dur1;
512 typedef duration<_Rep2, _Period2> __dur2;
513 typedef typename common_type<__dur1,__dur2>::type __cd;
514 return __cd(__lhs).count() / __cd(__rhs).count();
518 template<typename _Rep1, typename _Period, typename _Rep2>
520 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
521 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
523 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
525 return __cd(__cd(__d).count() % __s);
528 template<typename _Rep1, typename _Period1,
529 typename _Rep2, typename _Period2>
530 constexpr typename common_type<duration<_Rep1, _Period1>,
531 duration<_Rep2, _Period2>>::type
532 operator%(const duration<_Rep1, _Period1>& __lhs,
533 const duration<_Rep2, _Period2>& __rhs)
535 typedef duration<_Rep1, _Period1> __dur1;
536 typedef duration<_Rep2, _Period2> __dur2;
537 typedef typename common_type<__dur1,__dur2>::type __cd;
538 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
542 template<typename _Rep1, typename _Period1,
543 typename _Rep2, typename _Period2>
545 operator==(const duration<_Rep1, _Period1>& __lhs,
546 const duration<_Rep2, _Period2>& __rhs)
548 typedef duration<_Rep1, _Period1> __dur1;
549 typedef duration<_Rep2, _Period2> __dur2;
550 typedef typename common_type<__dur1,__dur2>::type __ct;
551 return __ct(__lhs).count() == __ct(__rhs).count();
554 template<typename _Rep1, typename _Period1,
555 typename _Rep2, typename _Period2>
557 operator<(const duration<_Rep1, _Period1>& __lhs,
558 const duration<_Rep2, _Period2>& __rhs)
560 typedef duration<_Rep1, _Period1> __dur1;
561 typedef duration<_Rep2, _Period2> __dur2;
562 typedef typename common_type<__dur1,__dur2>::type __ct;
563 return __ct(__lhs).count() < __ct(__rhs).count();
566 template<typename _Rep1, typename _Period1,
567 typename _Rep2, typename _Period2>
569 operator!=(const duration<_Rep1, _Period1>& __lhs,
570 const duration<_Rep2, _Period2>& __rhs)
571 { return !(__lhs == __rhs); }
573 template<typename _Rep1, typename _Period1,
574 typename _Rep2, typename _Period2>
576 operator<=(const duration<_Rep1, _Period1>& __lhs,
577 const duration<_Rep2, _Period2>& __rhs)
578 { return !(__rhs < __lhs); }
580 template<typename _Rep1, typename _Period1,
581 typename _Rep2, typename _Period2>
583 operator>(const duration<_Rep1, _Period1>& __lhs,
584 const duration<_Rep2, _Period2>& __rhs)
585 { return __rhs < __lhs; }
587 template<typename _Rep1, typename _Period1,
588 typename _Rep2, typename _Period2>
590 operator>=(const duration<_Rep1, _Period1>& __lhs,
591 const duration<_Rep2, _Period2>& __rhs)
592 { return !(__lhs < __rhs); }
594 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
595 # define _GLIBCXX_CHRONO_INT64_T int64_t
596 #elif defined __INT64_TYPE__
597 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
599 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
600 "Representation type for nanoseconds must have at least 64 bits");
601 # define _GLIBCXX_CHRONO_INT64_T long long
605 typedef duration<_GLIBCXX_CHRONO_INT64_T, nano> nanoseconds;
608 typedef duration<_GLIBCXX_CHRONO_INT64_T, micro> microseconds;
611 typedef duration<_GLIBCXX_CHRONO_INT64_T, milli> milliseconds;
614 typedef duration<_GLIBCXX_CHRONO_INT64_T> seconds;
617 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>> minutes;
620 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>> hours;
622 #undef _GLIBCXX_CHRONO_INT64_T
625 template<typename _Clock, typename _Dur>
628 typedef _Clock clock;
629 typedef _Dur duration;
630 typedef typename duration::rep rep;
631 typedef typename duration::period period;
633 constexpr time_point() : __d(duration::zero())
636 constexpr explicit time_point(const duration& __dur)
641 template<typename _Dur2,
642 typename = _Require<is_convertible<_Dur2, _Dur>>>
643 constexpr time_point(const time_point<clock, _Dur2>& __t)
644 : __d(__t.time_since_epoch())
649 time_since_epoch() const
653 _GLIBCXX17_CONSTEXPR time_point&
654 operator+=(const duration& __dur)
660 _GLIBCXX17_CONSTEXPR time_point&
661 operator-=(const duration& __dur)
668 static constexpr time_point
670 { return time_point(duration::min()); }
672 static constexpr time_point
674 { return time_point(duration::max()); }
681 template<typename _ToDur, typename _Clock, typename _Dur>
682 constexpr typename enable_if<__is_duration<_ToDur>::value,
683 time_point<_Clock, _ToDur>>::type
684 time_point_cast(const time_point<_Clock, _Dur>& __t)
686 typedef time_point<_Clock, _ToDur> __time_point;
687 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
690 #if __cplusplus > 201402L
691 template<typename _ToDur, typename _Clock, typename _Dur>
693 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
694 floor(const time_point<_Clock, _Dur>& __tp)
696 return time_point<_Clock, _ToDur>{
697 chrono::floor<_ToDur>(__tp.time_since_epoch())};
700 template<typename _ToDur, typename _Clock, typename _Dur>
702 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
703 ceil(const time_point<_Clock, _Dur>& __tp)
705 return time_point<_Clock, _ToDur>{
706 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
709 template<typename _ToDur, typename _Clock, typename _Dur>
710 constexpr enable_if_t<
711 __and_<__is_duration<_ToDur>,
712 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
713 time_point<_Clock, _ToDur>>
714 round(const time_point<_Clock, _Dur>& __tp)
716 return time_point<_Clock, _ToDur>{
717 chrono::round<_ToDur>(__tp.time_since_epoch())};
721 template<typename _Clock, typename _Dur1,
722 typename _Rep2, typename _Period2>
723 constexpr time_point<_Clock,
724 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
725 operator+(const time_point<_Clock, _Dur1>& __lhs,
726 const duration<_Rep2, _Period2>& __rhs)
728 typedef duration<_Rep2, _Period2> __dur2;
729 typedef typename common_type<_Dur1,__dur2>::type __ct;
730 typedef time_point<_Clock, __ct> __time_point;
731 return __time_point(__lhs.time_since_epoch() + __rhs);
734 template<typename _Rep1, typename _Period1,
735 typename _Clock, typename _Dur2>
736 constexpr time_point<_Clock,
737 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
738 operator+(const duration<_Rep1, _Period1>& __lhs,
739 const time_point<_Clock, _Dur2>& __rhs)
741 typedef duration<_Rep1, _Period1> __dur1;
742 typedef typename common_type<__dur1,_Dur2>::type __ct;
743 typedef time_point<_Clock, __ct> __time_point;
744 return __time_point(__rhs.time_since_epoch() + __lhs);
747 template<typename _Clock, typename _Dur1,
748 typename _Rep2, typename _Period2>
749 constexpr time_point<_Clock,
750 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
751 operator-(const time_point<_Clock, _Dur1>& __lhs,
752 const duration<_Rep2, _Period2>& __rhs)
754 typedef duration<_Rep2, _Period2> __dur2;
755 typedef typename common_type<_Dur1,__dur2>::type __ct;
756 typedef time_point<_Clock, __ct> __time_point;
757 return __time_point(__lhs.time_since_epoch() -__rhs);
760 template<typename _Clock, typename _Dur1, typename _Dur2>
761 constexpr typename common_type<_Dur1, _Dur2>::type
762 operator-(const time_point<_Clock, _Dur1>& __lhs,
763 const time_point<_Clock, _Dur2>& __rhs)
764 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
766 template<typename _Clock, typename _Dur1, typename _Dur2>
768 operator==(const time_point<_Clock, _Dur1>& __lhs,
769 const time_point<_Clock, _Dur2>& __rhs)
770 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
772 template<typename _Clock, typename _Dur1, typename _Dur2>
774 operator!=(const time_point<_Clock, _Dur1>& __lhs,
775 const time_point<_Clock, _Dur2>& __rhs)
776 { return !(__lhs == __rhs); }
778 template<typename _Clock, typename _Dur1, typename _Dur2>
780 operator<(const time_point<_Clock, _Dur1>& __lhs,
781 const time_point<_Clock, _Dur2>& __rhs)
782 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
784 template<typename _Clock, typename _Dur1, typename _Dur2>
786 operator<=(const time_point<_Clock, _Dur1>& __lhs,
787 const time_point<_Clock, _Dur2>& __rhs)
788 { return !(__rhs < __lhs); }
790 template<typename _Clock, typename _Dur1, typename _Dur2>
792 operator>(const time_point<_Clock, _Dur1>& __lhs,
793 const time_point<_Clock, _Dur2>& __rhs)
794 { return __rhs < __lhs; }
796 template<typename _Clock, typename _Dur1, typename _Dur2>
798 operator>=(const time_point<_Clock, _Dur1>& __lhs,
799 const time_point<_Clock, _Dur2>& __rhs)
800 { return !(__lhs < __rhs); }
805 // Why nanosecond resolution as the default?
806 // Why have std::system_clock always count in the highest
807 // resolution (ie nanoseconds), even if on some OSes the low 3
808 // or 9 decimal digits will be always zero? This allows later
809 // implementations to change the system_clock::now()
810 // implementation any time to provide better resolution without
811 // changing function signature or units.
813 // To support the (forward) evolution of the library's defined
814 // clocks, wrap inside inline namespace so that the current
815 // defintions of system_clock, steady_clock, and
816 // high_resolution_clock types are uniquely mangled. This way, new
817 // code can use the latests clocks, while the library can contain
818 // compatibility definitions for previous versions. At some
819 // point, when these clocks settle down, the inlined namespaces
820 // can be removed. XXX GLIBCXX_ABI Deprecated
821 inline namespace _V2 {
824 * @brief System clock.
826 * Time returned represents wall time from the system-wide clock.
830 typedef chrono::nanoseconds duration;
831 typedef duration::rep rep;
832 typedef duration::period period;
833 typedef chrono::time_point<system_clock, duration> time_point;
835 static_assert(system_clock::duration::min()
836 < system_clock::duration::zero(),
837 "a clock's minimum duration cannot be less than its epoch");
839 static constexpr bool is_steady = false;
846 to_time_t(const time_point& __t) noexcept
848 return std::time_t(duration_cast<chrono::seconds>
849 (__t.time_since_epoch()).count());
853 from_time_t(std::time_t __t) noexcept
855 typedef chrono::time_point<system_clock, seconds> __from;
856 return time_point_cast<system_clock::duration>
857 (__from(chrono::seconds(__t)));
863 * @brief Monotonic clock
865 * Time returned has the property of only increasing at a uniform rate.
869 typedef chrono::nanoseconds duration;
870 typedef duration::rep rep;
871 typedef duration::period period;
872 typedef chrono::time_point<steady_clock, duration> time_point;
874 static constexpr bool is_steady = true;
882 * @brief Highest-resolution clock
884 * This is the clock "with the shortest tick period." Alias to
885 * std::system_clock until higher-than-nanosecond definitions
888 using high_resolution_clock = system_clock;
890 } // end inline namespace _V2
891 } // namespace chrono
893 #if __cplusplus > 201103L
895 #define __cpp_lib_chrono_udls 201304
897 inline namespace literals
899 inline namespace chrono_literals
901 #pragma GCC diagnostic push
902 #pragma GCC diagnostic ignored "-Wliteral-suffix"
903 template<typename _Dur, char... _Digits>
904 constexpr _Dur __check_overflow()
906 using _Val = __parse_int::_Parse_int<_Digits...>;
907 constexpr typename _Dur::rep __repval = _Val::value;
908 static_assert(__repval >= 0 && __repval == _Val::value,
909 "literal value cannot be represented by duration type");
910 return _Dur(__repval);
913 constexpr chrono::duration<long double, ratio<3600,1>>
914 operator""h(long double __hours)
915 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
917 template <char... _Digits>
918 constexpr chrono::hours
920 { return __check_overflow<chrono::hours, _Digits...>(); }
922 constexpr chrono::duration<long double, ratio<60,1>>
923 operator""min(long double __mins)
924 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
926 template <char... _Digits>
927 constexpr chrono::minutes
929 { return __check_overflow<chrono::minutes, _Digits...>(); }
931 constexpr chrono::duration<long double>
932 operator""s(long double __secs)
933 { return chrono::duration<long double>{__secs}; }
935 template <char... _Digits>
936 constexpr chrono::seconds
938 { return __check_overflow<chrono::seconds, _Digits...>(); }
940 constexpr chrono::duration<long double, milli>
941 operator""ms(long double __msecs)
942 { return chrono::duration<long double, milli>{__msecs}; }
944 template <char... _Digits>
945 constexpr chrono::milliseconds
947 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
949 constexpr chrono::duration<long double, micro>
950 operator""us(long double __usecs)
951 { return chrono::duration<long double, micro>{__usecs}; }
953 template <char... _Digits>
954 constexpr chrono::microseconds
956 { return __check_overflow<chrono::microseconds, _Digits...>(); }
958 constexpr chrono::duration<long double, nano>
959 operator""ns(long double __nsecs)
960 { return chrono::duration<long double, nano>{__nsecs}; }
962 template <char... _Digits>
963 constexpr chrono::nanoseconds
965 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
967 #pragma GCC diagnostic pop
968 } // inline namespace chrono_literals
969 } // inline namespace literals
973 using namespace literals::chrono_literals;
974 } // namespace chrono
980 _GLIBCXX_END_NAMESPACE_VERSION
985 #endif //_GLIBCXX_CHRONO