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.
30 #ifndef _GLIBCXX_CHRONO
31 #define _GLIBCXX_CHRONO 1
33 #pragma GCC system_header
35 #if __cplusplus < 201103L
36 # include <bits/c++0x_warning.h>
40 #include <type_traits>
43 #include <bits/parse_numbers.h> // for literals support.
45 namespace std _GLIBCXX_VISIBILITY(default)
47 _GLIBCXX_BEGIN_NAMESPACE_VERSION
50 * @defgroup chrono Time
53 * Classes and functions for time.
57 /** @namespace std::chrono
58 * @brief ISO C++ 2011 namespace for date and time utilities
62 template<typename _Rep, typename _Period = ratio<1>>
65 template<typename _Clock, typename _Dur = typename _Clock::duration>
69 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
71 /// @cond undocumented
73 template<typename _CT, typename _Period1, typename _Period2, typename = void>
74 struct __duration_common_type
77 template<typename _CT, typename _Period1, typename _Period2>
78 struct __duration_common_type<_CT, _Period1, _Period2,
79 __void_t<typename _CT::type>>
82 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
83 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
84 using __cr = typename _CT::type;
85 using __r = ratio<__gcd_num::value,
86 (_Period1::den / __gcd_den::value) * _Period2::den>;
89 using type = chrono::duration<__cr, __r>;
92 template<typename _Period1, typename _Period2>
93 struct __duration_common_type<__failure_type, _Period1, _Period2>
94 { typedef __failure_type type; };
98 /// Specialization of common_type for chrono::duration types.
100 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
101 struct common_type<chrono::duration<_Rep1, _Period1>,
102 chrono::duration<_Rep2, _Period2>>
103 : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
106 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
108 /// @cond undocumented
110 template<typename _CT, typename _Clock, typename = void>
111 struct __timepoint_common_type
114 template<typename _CT, typename _Clock>
115 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
117 using type = chrono::time_point<_Clock, typename _CT::type>;
122 /// Specialization of common_type for chrono::time_point types.
123 /// @relates time_point
124 template<typename _Clock, typename _Duration1, typename _Duration2>
125 struct common_type<chrono::time_point<_Clock, _Duration1>,
126 chrono::time_point<_Clock, _Duration2>>
127 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
134 /// @addtogroup chrono
137 /// @cond undocumented
139 // Primary template for duration_cast impl.
140 template<typename _ToDur, typename _CF, typename _CR,
141 bool _NumIsOne = false, bool _DenIsOne = false>
142 struct __duration_cast_impl
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>(static_cast<_CR>(__d.count())
150 * static_cast<_CR>(_CF::num)
151 / static_cast<_CR>(_CF::den)));
155 template<typename _ToDur, typename _CF, typename _CR>
156 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
158 template<typename _Rep, typename _Period>
159 static constexpr _ToDur
160 __cast(const duration<_Rep, _Period>& __d)
162 typedef typename _ToDur::rep __to_rep;
163 return _ToDur(static_cast<__to_rep>(__d.count()));
167 template<typename _ToDur, typename _CF, typename _CR>
168 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
170 template<typename _Rep, typename _Period>
171 static constexpr _ToDur
172 __cast(const duration<_Rep, _Period>& __d)
174 typedef typename _ToDur::rep __to_rep;
175 return _ToDur(static_cast<__to_rep>(
176 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
180 template<typename _ToDur, typename _CF, typename _CR>
181 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
183 template<typename _Rep, typename _Period>
184 static constexpr _ToDur
185 __cast(const duration<_Rep, _Period>& __d)
187 typedef typename _ToDur::rep __to_rep;
188 return _ToDur(static_cast<__to_rep>(
189 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
193 template<typename _Tp>
198 template<typename _Rep, typename _Period>
199 struct __is_duration<duration<_Rep, _Period>>
203 template<typename _Tp>
204 using __enable_if_is_duration
205 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
207 template<typename _Tp>
208 using __disable_if_is_duration
209 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
214 template<typename _ToDur, typename _Rep, typename _Period>
215 constexpr __enable_if_is_duration<_ToDur>
216 duration_cast(const duration<_Rep, _Period>& __d)
218 typedef typename _ToDur::period __to_period;
219 typedef typename _ToDur::rep __to_rep;
220 typedef ratio_divide<_Period, __to_period> __cf;
221 typedef typename common_type<__to_rep, _Rep, intmax_t>::type
223 typedef __duration_cast_impl<_ToDur, __cf, __cr,
224 __cf::num == 1, __cf::den == 1> __dc;
225 return __dc::__cast(__d);
228 /// treat_as_floating_point
229 template<typename _Rep>
230 struct treat_as_floating_point
231 : is_floating_point<_Rep>
234 #if __cplusplus > 201402L
235 template <typename _Rep>
236 inline constexpr bool treat_as_floating_point_v =
237 treat_as_floating_point<_Rep>::value;
240 #if __cplusplus >= 201703L
241 # define __cpp_lib_chrono 201611
243 template<typename _ToDur, typename _Rep, typename _Period>
244 constexpr __enable_if_is_duration<_ToDur>
245 floor(const duration<_Rep, _Period>& __d)
247 auto __to = chrono::duration_cast<_ToDur>(__d);
249 return __to - _ToDur{1};
253 template<typename _ToDur, typename _Rep, typename _Period>
254 constexpr __enable_if_is_duration<_ToDur>
255 ceil(const duration<_Rep, _Period>& __d)
257 auto __to = chrono::duration_cast<_ToDur>(__d);
259 return __to + _ToDur{1};
263 template <typename _ToDur, typename _Rep, typename _Period>
264 constexpr enable_if_t<
265 __and_<__is_duration<_ToDur>,
266 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
268 round(const duration<_Rep, _Period>& __d)
270 _ToDur __t0 = chrono::floor<_ToDur>(__d);
271 _ToDur __t1 = __t0 + _ToDur{1};
272 auto __diff0 = __d - __t0;
273 auto __diff1 = __t1 - __d;
274 if (__diff0 == __diff1)
276 if (__t0.count() & 1)
280 else if (__diff0 < __diff1)
285 template<typename _Rep, typename _Period>
287 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
288 abs(duration<_Rep, _Period> __d)
290 if (__d >= __d.zero())
297 template<typename _Rep>
298 struct duration_values
300 static constexpr _Rep
304 static constexpr _Rep
306 { return numeric_limits<_Rep>::max(); }
308 static constexpr _Rep
310 { return numeric_limits<_Rep>::lowest(); }
313 /// @cond undocumented
315 template<typename _Tp>
320 template<intmax_t _Num, intmax_t _Den>
321 struct __is_ratio<ratio<_Num, _Den>>
328 template<typename _Rep, typename _Period>
332 template<typename _Rep2>
333 using __is_float = treat_as_floating_point<_Rep2>;
335 // _Period2 is an exact multiple of _Period
336 template<typename _Period2>
338 = __bool_constant<ratio_divide<_Period2, _Period>::den == 1>;
343 typedef _Period period;
345 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
346 static_assert(__is_ratio<_Period>::value,
347 "period must be a specialization of ratio");
348 static_assert(_Period::num > 0, "period must be positive");
350 // 20.11.5.1 construction / copy / destroy
351 constexpr duration() = default;
353 duration(const duration&) = default;
355 // _GLIBCXX_RESOLVE_LIB_DEFECTS
356 // 3050. Conversion specification problem in chrono::duration
357 template<typename _Rep2, typename = _Require<
358 is_convertible<const _Rep2&, rep>,
359 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
360 constexpr explicit duration(const _Rep2& __rep)
361 : __r(static_cast<rep>(__rep)) { }
363 template<typename _Rep2, typename _Period2, typename = _Require<
364 __or_<__is_float<rep>,
365 __and_<__is_harmonic<_Period2>,
366 __not_<__is_float<_Rep2>>>>>>
367 constexpr duration(const duration<_Rep2, _Period2>& __d)
368 : __r(duration_cast<duration>(__d).count()) { }
370 ~duration() = default;
371 duration& operator=(const duration&) = default;
373 // 20.11.5.2 observer
378 // 20.11.5.3 arithmetic
385 { return duration(-__r); }
387 _GLIBCXX17_CONSTEXPR duration&
394 _GLIBCXX17_CONSTEXPR duration
396 { return duration(__r++); }
398 _GLIBCXX17_CONSTEXPR duration&
405 _GLIBCXX17_CONSTEXPR duration
407 { return duration(__r--); }
409 _GLIBCXX17_CONSTEXPR duration&
410 operator+=(const duration& __d)
416 _GLIBCXX17_CONSTEXPR duration&
417 operator-=(const duration& __d)
423 _GLIBCXX17_CONSTEXPR duration&
424 operator*=(const rep& __rhs)
430 _GLIBCXX17_CONSTEXPR duration&
431 operator/=(const rep& __rhs)
438 template<typename _Rep2 = rep>
440 typename enable_if<!treat_as_floating_point<_Rep2>::value,
442 operator%=(const rep& __rhs)
448 template<typename _Rep2 = rep>
450 typename enable_if<!treat_as_floating_point<_Rep2>::value,
452 operator%=(const duration& __d)
458 // 20.11.5.4 special values
459 static constexpr duration
461 { return duration(duration_values<rep>::zero()); }
463 static constexpr duration
465 { return duration(duration_values<rep>::min()); }
467 static constexpr duration
469 { return duration(duration_values<rep>::max()); }
475 /// @relates duration @{
477 /// The sum of two durations.
478 template<typename _Rep1, typename _Period1,
479 typename _Rep2, typename _Period2>
480 constexpr typename common_type<duration<_Rep1, _Period1>,
481 duration<_Rep2, _Period2>>::type
482 operator+(const duration<_Rep1, _Period1>& __lhs,
483 const duration<_Rep2, _Period2>& __rhs)
485 typedef duration<_Rep1, _Period1> __dur1;
486 typedef duration<_Rep2, _Period2> __dur2;
487 typedef typename common_type<__dur1,__dur2>::type __cd;
488 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
491 /// The difference between two durations.
492 template<typename _Rep1, typename _Period1,
493 typename _Rep2, typename _Period2>
494 constexpr typename common_type<duration<_Rep1, _Period1>,
495 duration<_Rep2, _Period2>>::type
496 operator-(const duration<_Rep1, _Period1>& __lhs,
497 const duration<_Rep2, _Period2>& __rhs)
499 typedef duration<_Rep1, _Period1> __dur1;
500 typedef duration<_Rep2, _Period2> __dur2;
501 typedef typename common_type<__dur1,__dur2>::type __cd;
502 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
507 /// @cond undocumented
509 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
510 // is implicitly convertible to it.
511 // _GLIBCXX_RESOLVE_LIB_DEFECTS
512 // 3050. Conversion specification problem in chrono::duration constructor
513 template<typename _Rep1, typename _Rep2,
514 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
515 using __common_rep_t = typename
516 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
520 /// @relates duration @{
522 /// Multiply a duration by a scalar value.
523 template<typename _Rep1, typename _Period, typename _Rep2>
524 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
525 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
527 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
529 return __cd(__cd(__d).count() * __s);
532 /// Multiply a duration by a scalar value.
533 template<typename _Rep1, typename _Rep2, typename _Period>
534 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
535 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
536 { return __d * __s; }
538 template<typename _Rep1, typename _Period, typename _Rep2>
540 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
541 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
543 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
545 return __cd(__cd(__d).count() / __s);
548 template<typename _Rep1, typename _Period1,
549 typename _Rep2, typename _Period2>
550 constexpr typename common_type<_Rep1, _Rep2>::type
551 operator/(const duration<_Rep1, _Period1>& __lhs,
552 const duration<_Rep2, _Period2>& __rhs)
554 typedef duration<_Rep1, _Period1> __dur1;
555 typedef duration<_Rep2, _Period2> __dur2;
556 typedef typename common_type<__dur1,__dur2>::type __cd;
557 return __cd(__lhs).count() / __cd(__rhs).count();
561 template<typename _Rep1, typename _Period, typename _Rep2>
563 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
564 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
566 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
568 return __cd(__cd(__d).count() % __s);
571 template<typename _Rep1, typename _Period1,
572 typename _Rep2, typename _Period2>
573 constexpr typename common_type<duration<_Rep1, _Period1>,
574 duration<_Rep2, _Period2>>::type
575 operator%(const duration<_Rep1, _Period1>& __lhs,
576 const duration<_Rep2, _Period2>& __rhs)
578 typedef duration<_Rep1, _Period1> __dur1;
579 typedef duration<_Rep2, _Period2> __dur2;
580 typedef typename common_type<__dur1,__dur2>::type __cd;
581 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
586 template<typename _Rep1, typename _Period1,
587 typename _Rep2, typename _Period2>
589 operator==(const duration<_Rep1, _Period1>& __lhs,
590 const duration<_Rep2, _Period2>& __rhs)
592 typedef duration<_Rep1, _Period1> __dur1;
593 typedef duration<_Rep2, _Period2> __dur2;
594 typedef typename common_type<__dur1,__dur2>::type __ct;
595 return __ct(__lhs).count() == __ct(__rhs).count();
598 template<typename _Rep1, typename _Period1,
599 typename _Rep2, typename _Period2>
601 operator<(const duration<_Rep1, _Period1>& __lhs,
602 const duration<_Rep2, _Period2>& __rhs)
604 typedef duration<_Rep1, _Period1> __dur1;
605 typedef duration<_Rep2, _Period2> __dur2;
606 typedef typename common_type<__dur1,__dur2>::type __ct;
607 return __ct(__lhs).count() < __ct(__rhs).count();
610 template<typename _Rep1, typename _Period1,
611 typename _Rep2, typename _Period2>
613 operator!=(const duration<_Rep1, _Period1>& __lhs,
614 const duration<_Rep2, _Period2>& __rhs)
615 { return !(__lhs == __rhs); }
617 template<typename _Rep1, typename _Period1,
618 typename _Rep2, typename _Period2>
620 operator<=(const duration<_Rep1, _Period1>& __lhs,
621 const duration<_Rep2, _Period2>& __rhs)
622 { return !(__rhs < __lhs); }
624 template<typename _Rep1, typename _Period1,
625 typename _Rep2, typename _Period2>
627 operator>(const duration<_Rep1, _Period1>& __lhs,
628 const duration<_Rep2, _Period2>& __rhs)
629 { return __rhs < __lhs; }
631 template<typename _Rep1, typename _Period1,
632 typename _Rep2, typename _Period2>
634 operator>=(const duration<_Rep1, _Period1>& __lhs,
635 const duration<_Rep2, _Period2>& __rhs)
636 { return !(__lhs < __rhs); }
640 #ifdef _GLIBCXX_USE_C99_STDINT_TR1
641 # define _GLIBCXX_CHRONO_INT64_T int64_t
642 #elif defined __INT64_TYPE__
643 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
645 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
646 "Representation type for nanoseconds must have at least 64 bits");
647 # define _GLIBCXX_CHRONO_INT64_T long long
651 typedef duration<_GLIBCXX_CHRONO_INT64_T, nano> nanoseconds;
654 typedef duration<_GLIBCXX_CHRONO_INT64_T, micro> microseconds;
657 typedef duration<_GLIBCXX_CHRONO_INT64_T, milli> milliseconds;
660 typedef duration<_GLIBCXX_CHRONO_INT64_T> seconds;
663 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>> minutes;
666 typedef duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>> hours;
668 #undef _GLIBCXX_CHRONO_INT64_T
671 template<typename _Clock, typename _Dur>
674 typedef _Clock clock;
675 typedef _Dur duration;
676 typedef typename duration::rep rep;
677 typedef typename duration::period period;
679 constexpr time_point() : __d(duration::zero())
682 constexpr explicit time_point(const duration& __dur)
687 template<typename _Dur2,
688 typename = _Require<is_convertible<_Dur2, _Dur>>>
689 constexpr time_point(const time_point<clock, _Dur2>& __t)
690 : __d(__t.time_since_epoch())
695 time_since_epoch() const
699 _GLIBCXX17_CONSTEXPR time_point&
700 operator+=(const duration& __dur)
706 _GLIBCXX17_CONSTEXPR time_point&
707 operator-=(const duration& __dur)
714 static constexpr time_point
716 { return time_point(duration::min()); }
718 static constexpr time_point
720 { return time_point(duration::max()); }
727 template<typename _ToDur, typename _Clock, typename _Dur>
728 constexpr typename enable_if<__is_duration<_ToDur>::value,
729 time_point<_Clock, _ToDur>>::type
730 time_point_cast(const time_point<_Clock, _Dur>& __t)
732 typedef time_point<_Clock, _ToDur> __time_point;
733 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
736 #if __cplusplus > 201402L
737 template<typename _ToDur, typename _Clock, typename _Dur>
739 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
740 floor(const time_point<_Clock, _Dur>& __tp)
742 return time_point<_Clock, _ToDur>{
743 chrono::floor<_ToDur>(__tp.time_since_epoch())};
746 template<typename _ToDur, typename _Clock, typename _Dur>
748 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
749 ceil(const time_point<_Clock, _Dur>& __tp)
751 return time_point<_Clock, _ToDur>{
752 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
755 template<typename _ToDur, typename _Clock, typename _Dur>
756 constexpr enable_if_t<
757 __and_<__is_duration<_ToDur>,
758 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
759 time_point<_Clock, _ToDur>>
760 round(const time_point<_Clock, _Dur>& __tp)
762 return time_point<_Clock, _ToDur>{
763 chrono::round<_ToDur>(__tp.time_since_epoch())};
767 /// @relates time_point @{
769 /// Adjust a time point forwards by the given duration.
770 template<typename _Clock, typename _Dur1,
771 typename _Rep2, typename _Period2>
772 constexpr time_point<_Clock,
773 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
774 operator+(const time_point<_Clock, _Dur1>& __lhs,
775 const duration<_Rep2, _Period2>& __rhs)
777 typedef duration<_Rep2, _Period2> __dur2;
778 typedef typename common_type<_Dur1,__dur2>::type __ct;
779 typedef time_point<_Clock, __ct> __time_point;
780 return __time_point(__lhs.time_since_epoch() + __rhs);
783 /// Adjust a time point forwards by the given duration.
784 template<typename _Rep1, typename _Period1,
785 typename _Clock, typename _Dur2>
786 constexpr time_point<_Clock,
787 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
788 operator+(const duration<_Rep1, _Period1>& __lhs,
789 const time_point<_Clock, _Dur2>& __rhs)
791 typedef duration<_Rep1, _Period1> __dur1;
792 typedef typename common_type<__dur1,_Dur2>::type __ct;
793 typedef time_point<_Clock, __ct> __time_point;
794 return __time_point(__rhs.time_since_epoch() + __lhs);
797 /// Adjust a time point backwards by the given duration.
798 template<typename _Clock, typename _Dur1,
799 typename _Rep2, typename _Period2>
800 constexpr time_point<_Clock,
801 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
802 operator-(const time_point<_Clock, _Dur1>& __lhs,
803 const duration<_Rep2, _Period2>& __rhs)
805 typedef duration<_Rep2, _Period2> __dur2;
806 typedef typename common_type<_Dur1,__dur2>::type __ct;
807 typedef time_point<_Clock, __ct> __time_point;
808 return __time_point(__lhs.time_since_epoch() -__rhs);
813 /// @relates time_point @{
815 /// The difference between two time points (as a duration)
816 template<typename _Clock, typename _Dur1, typename _Dur2>
817 constexpr typename common_type<_Dur1, _Dur2>::type
818 operator-(const time_point<_Clock, _Dur1>& __lhs,
819 const time_point<_Clock, _Dur2>& __rhs)
820 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
822 template<typename _Clock, typename _Dur1, typename _Dur2>
824 operator==(const time_point<_Clock, _Dur1>& __lhs,
825 const time_point<_Clock, _Dur2>& __rhs)
826 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
828 template<typename _Clock, typename _Dur1, typename _Dur2>
830 operator!=(const time_point<_Clock, _Dur1>& __lhs,
831 const time_point<_Clock, _Dur2>& __rhs)
832 { return !(__lhs == __rhs); }
834 template<typename _Clock, typename _Dur1, typename _Dur2>
836 operator<(const time_point<_Clock, _Dur1>& __lhs,
837 const time_point<_Clock, _Dur2>& __rhs)
838 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
840 template<typename _Clock, typename _Dur1, typename _Dur2>
842 operator<=(const time_point<_Clock, _Dur1>& __lhs,
843 const time_point<_Clock, _Dur2>& __rhs)
844 { return !(__rhs < __lhs); }
846 template<typename _Clock, typename _Dur1, typename _Dur2>
848 operator>(const time_point<_Clock, _Dur1>& __lhs,
849 const time_point<_Clock, _Dur2>& __rhs)
850 { return __rhs < __lhs; }
852 template<typename _Clock, typename _Dur1, typename _Dur2>
854 operator>=(const time_point<_Clock, _Dur1>& __lhs,
855 const time_point<_Clock, _Dur2>& __rhs)
856 { return !(__lhs < __rhs); }
862 // Why nanosecond resolution as the default?
863 // Why have std::system_clock always count in the highest
864 // resolution (ie nanoseconds), even if on some OSes the low 3
865 // or 9 decimal digits will be always zero? This allows later
866 // implementations to change the system_clock::now()
867 // implementation any time to provide better resolution without
868 // changing function signature or units.
870 // To support the (forward) evolution of the library's defined
871 // clocks, wrap inside inline namespace so that the current
872 // defintions of system_clock, steady_clock, and
873 // high_resolution_clock types are uniquely mangled. This way, new
874 // code can use the latests clocks, while the library can contain
875 // compatibility definitions for previous versions. At some
876 // point, when these clocks settle down, the inlined namespaces
877 // can be removed. XXX GLIBCXX_ABI Deprecated
878 inline namespace _V2 {
881 * @brief System clock.
883 * Time returned represents wall time from the system-wide clock.
888 typedef chrono::nanoseconds duration;
889 typedef duration::rep rep;
890 typedef duration::period period;
891 typedef chrono::time_point<system_clock, duration> time_point;
893 static_assert(system_clock::duration::min()
894 < system_clock::duration::zero(),
895 "a clock's minimum duration cannot be less than its epoch");
897 static constexpr bool is_steady = false;
904 to_time_t(const time_point& __t) noexcept
906 return std::time_t(duration_cast<chrono::seconds>
907 (__t.time_since_epoch()).count());
911 from_time_t(std::time_t __t) noexcept
913 typedef chrono::time_point<system_clock, seconds> __from;
914 return time_point_cast<system_clock::duration>
915 (__from(chrono::seconds(__t)));
921 * @brief Monotonic clock
923 * Time returned has the property of only increasing at a uniform rate.
928 typedef chrono::nanoseconds duration;
929 typedef duration::rep rep;
930 typedef duration::period period;
931 typedef chrono::time_point<steady_clock, duration> time_point;
933 static constexpr bool is_steady = true;
941 * @brief Highest-resolution clock
943 * This is the clock "with the shortest tick period." Alias to
944 * std::system_clock until higher-than-nanosecond definitions
948 using high_resolution_clock = system_clock;
950 } // end inline namespace _V2
952 } // namespace chrono
954 #if __cplusplus > 201103L
956 #define __cpp_lib_chrono_udls 201304
958 inline namespace literals
960 /** ISO C++ 2014 namespace for suffixes for duration literals.
962 * These suffixes can be used to create `chrono::duration` values with
963 * tick periods of hours, minutes, seconds, milliseconds, microseconds
964 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
965 * as `5s` after making the suffix visible in the current scope.
966 * The suffixes can be made visible by a using-directive or
967 * using-declaration such as:
968 * - `using namespace std::chrono_literals;`
969 * - `using namespace std::literals;`
970 * - `using namespace std::chrono;`
971 * - `using namespace std;`
972 * - `using std::chrono_literals::operator""s;`
974 * The result of these suffixes on an integer literal is one of the
975 * standard typedefs such as `std::chrono::hours`.
976 * The result on a floating-point literal is a duration type with the
977 * specified tick period and an unspecified floating-point representation,
978 * for example `1.5e2ms` might be equivalent to
979 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
983 inline namespace chrono_literals
985 #pragma GCC diagnostic push
986 #pragma GCC diagnostic ignored "-Wliteral-suffix"
987 /// @cond undocumented
988 template<typename _Dur, char... _Digits>
989 constexpr _Dur __check_overflow()
991 using _Val = __parse_int::_Parse_int<_Digits...>;
992 constexpr typename _Dur::rep __repval = _Val::value;
993 static_assert(__repval >= 0 && __repval == _Val::value,
994 "literal value cannot be represented by duration type");
995 return _Dur(__repval);
999 /// Literal suffix for durations representing non-integer hours
1000 constexpr chrono::duration<long double, ratio<3600,1>>
1001 operator""h(long double __hours)
1002 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
1004 /// Literal suffix for durations of type `std::chrono::hours`
1005 template <char... _Digits>
1006 constexpr chrono::hours
1008 { return __check_overflow<chrono::hours, _Digits...>(); }
1010 /// Literal suffix for durations representing non-integer minutes
1011 constexpr chrono::duration<long double, ratio<60,1>>
1012 operator""min(long double __mins)
1013 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
1015 /// Literal suffix for durations of type `std::chrono::minutes`
1016 template <char... _Digits>
1017 constexpr chrono::minutes
1019 { return __check_overflow<chrono::minutes, _Digits...>(); }
1021 /// Literal suffix for durations representing non-integer seconds
1022 constexpr chrono::duration<long double>
1023 operator""s(long double __secs)
1024 { return chrono::duration<long double>{__secs}; }
1026 /// Literal suffix for durations of type `std::chrono::seconds`
1027 template <char... _Digits>
1028 constexpr chrono::seconds
1030 { return __check_overflow<chrono::seconds, _Digits...>(); }
1032 /// Literal suffix for durations representing non-integer milliseconds
1033 constexpr chrono::duration<long double, milli>
1034 operator""ms(long double __msecs)
1035 { return chrono::duration<long double, milli>{__msecs}; }
1037 /// Literal suffix for durations of type `std::chrono::milliseconds`
1038 template <char... _Digits>
1039 constexpr chrono::milliseconds
1041 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1043 /// Literal suffix for durations representing non-integer microseconds
1044 constexpr chrono::duration<long double, micro>
1045 operator""us(long double __usecs)
1046 { return chrono::duration<long double, micro>{__usecs}; }
1048 /// Literal suffix for durations of type `std::chrono::microseconds`
1049 template <char... _Digits>
1050 constexpr chrono::microseconds
1052 { return __check_overflow<chrono::microseconds, _Digits...>(); }
1054 /// Literal suffix for durations representing non-integer nanoseconds
1055 constexpr chrono::duration<long double, nano>
1056 operator""ns(long double __nsecs)
1057 { return chrono::duration<long double, nano>{__nsecs}; }
1059 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1060 template <char... _Digits>
1061 constexpr chrono::nanoseconds
1063 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1065 #pragma GCC diagnostic pop
1066 } // inline namespace chrono_literals
1067 } // inline namespace literals
1071 using namespace literals::chrono_literals;
1072 } // namespace chrono
1076 _GLIBCXX_END_NAMESPACE_VERSION
1081 #endif //_GLIBCXX_CHRONO