xref: /freebsd/contrib/llvm-project/libcxx/include/__chrono/duration.h (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
104eeddc0SDimitry Andric // -*- C++ -*-
204eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
304eeddc0SDimitry Andric //
404eeddc0SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
504eeddc0SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
604eeddc0SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
704eeddc0SDimitry Andric //
804eeddc0SDimitry Andric //===----------------------------------------------------------------------===//
904eeddc0SDimitry Andric 
1004eeddc0SDimitry Andric #ifndef _LIBCPP___CHRONO_DURATION_H
1104eeddc0SDimitry Andric #define _LIBCPP___CHRONO_DURATION_H
1204eeddc0SDimitry Andric 
1304eeddc0SDimitry Andric #include <__config>
14*bdd1243dSDimitry Andric #include <__type_traits/common_type.h>
15*bdd1243dSDimitry Andric #include <__type_traits/enable_if.h>
16*bdd1243dSDimitry Andric #include <__type_traits/is_convertible.h>
17*bdd1243dSDimitry Andric #include <__type_traits/is_floating_point.h>
1804eeddc0SDimitry Andric #include <limits>
1904eeddc0SDimitry Andric #include <ratio>
2004eeddc0SDimitry Andric 
2104eeddc0SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
2204eeddc0SDimitry Andric #  pragma GCC system_header
2304eeddc0SDimitry Andric #endif
2404eeddc0SDimitry Andric 
2504eeddc0SDimitry Andric _LIBCPP_PUSH_MACROS
2604eeddc0SDimitry Andric #include <__undef_macros>
2704eeddc0SDimitry Andric 
2804eeddc0SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
2904eeddc0SDimitry Andric 
3004eeddc0SDimitry Andric namespace chrono
3104eeddc0SDimitry Andric {
3204eeddc0SDimitry Andric 
3304eeddc0SDimitry Andric template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
3404eeddc0SDimitry Andric 
3504eeddc0SDimitry Andric template <class _Tp>
3604eeddc0SDimitry Andric struct __is_duration : false_type {};
3704eeddc0SDimitry Andric 
3804eeddc0SDimitry Andric template <class _Rep, class _Period>
3904eeddc0SDimitry Andric struct __is_duration<duration<_Rep, _Period> > : true_type  {};
4004eeddc0SDimitry Andric 
4104eeddc0SDimitry Andric template <class _Rep, class _Period>
4204eeddc0SDimitry Andric struct __is_duration<const duration<_Rep, _Period> > : true_type  {};
4304eeddc0SDimitry Andric 
4404eeddc0SDimitry Andric template <class _Rep, class _Period>
4504eeddc0SDimitry Andric struct __is_duration<volatile duration<_Rep, _Period> > : true_type  {};
4604eeddc0SDimitry Andric 
4704eeddc0SDimitry Andric template <class _Rep, class _Period>
4804eeddc0SDimitry Andric struct __is_duration<const volatile duration<_Rep, _Period> > : true_type  {};
4904eeddc0SDimitry Andric 
5004eeddc0SDimitry Andric } // namespace chrono
5104eeddc0SDimitry Andric 
5204eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
5304eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
5404eeddc0SDimitry Andric                                          chrono::duration<_Rep2, _Period2> >
5504eeddc0SDimitry Andric {
5604eeddc0SDimitry Andric     typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
5704eeddc0SDimitry Andric                              typename __ratio_gcd<_Period1, _Period2>::type> type;
5804eeddc0SDimitry Andric };
5904eeddc0SDimitry Andric 
6004eeddc0SDimitry Andric namespace chrono {
6104eeddc0SDimitry Andric 
6204eeddc0SDimitry Andric // duration_cast
6304eeddc0SDimitry Andric 
6404eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration,
6504eeddc0SDimitry Andric           class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
6604eeddc0SDimitry Andric           bool = _Period::num == 1,
6704eeddc0SDimitry Andric           bool = _Period::den == 1>
6804eeddc0SDimitry Andric struct __duration_cast;
6904eeddc0SDimitry Andric 
7004eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
7104eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
7204eeddc0SDimitry Andric {
7304eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
7404eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
7504eeddc0SDimitry Andric     {
7604eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
7704eeddc0SDimitry Andric     }
7804eeddc0SDimitry Andric };
7904eeddc0SDimitry Andric 
8004eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
8104eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
8204eeddc0SDimitry Andric {
8304eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
8404eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
8504eeddc0SDimitry Andric     {
8604eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
8704eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
8804eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
8904eeddc0SDimitry Andric     }
9004eeddc0SDimitry Andric };
9104eeddc0SDimitry Andric 
9204eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
9304eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
9404eeddc0SDimitry Andric {
9504eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
9604eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
9704eeddc0SDimitry Andric     {
9804eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
9904eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
10004eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
10104eeddc0SDimitry Andric     }
10204eeddc0SDimitry Andric };
10304eeddc0SDimitry Andric 
10404eeddc0SDimitry Andric template <class _FromDuration, class _ToDuration, class _Period>
10504eeddc0SDimitry Andric struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
10604eeddc0SDimitry Andric {
10704eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
10804eeddc0SDimitry Andric     _ToDuration operator()(const _FromDuration& __fd) const
10904eeddc0SDimitry Andric     {
11004eeddc0SDimitry Andric         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
11104eeddc0SDimitry Andric         return _ToDuration(static_cast<typename _ToDuration::rep>(
11204eeddc0SDimitry Andric                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
11304eeddc0SDimitry Andric                                                           / static_cast<_Ct>(_Period::den)));
11404eeddc0SDimitry Andric     }
11504eeddc0SDimitry Andric };
11604eeddc0SDimitry Andric 
11704eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
11804eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
11904eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
12004eeddc0SDimitry Andric typename enable_if
12104eeddc0SDimitry Andric <
12204eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
12304eeddc0SDimitry Andric     _ToDuration
12404eeddc0SDimitry Andric >::type
12504eeddc0SDimitry Andric duration_cast(const duration<_Rep, _Period>& __fd)
12604eeddc0SDimitry Andric {
12704eeddc0SDimitry Andric     return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
12804eeddc0SDimitry Andric }
12904eeddc0SDimitry Andric 
13004eeddc0SDimitry Andric template <class _Rep>
13104eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
13204eeddc0SDimitry Andric 
13304eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 14
13404eeddc0SDimitry Andric template <class _Rep>
13504eeddc0SDimitry Andric inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
13604eeddc0SDimitry Andric #endif
13704eeddc0SDimitry Andric 
13804eeddc0SDimitry Andric template <class _Rep>
13904eeddc0SDimitry Andric struct _LIBCPP_TEMPLATE_VIS duration_values
14004eeddc0SDimitry Andric {
14104eeddc0SDimitry Andric public:
14204eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() _NOEXCEPT {return _Rep(0);}
14304eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max()  _NOEXCEPT {return numeric_limits<_Rep>::max();}
14404eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min()  _NOEXCEPT {return numeric_limits<_Rep>::lowest();}
14504eeddc0SDimitry Andric };
14604eeddc0SDimitry Andric 
14704eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 14
14804eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
14904eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
15004eeddc0SDimitry Andric typename enable_if
15104eeddc0SDimitry Andric <
15204eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
15304eeddc0SDimitry Andric     _ToDuration
15404eeddc0SDimitry Andric >::type
15504eeddc0SDimitry Andric floor(const duration<_Rep, _Period>& __d)
15604eeddc0SDimitry Andric {
157*bdd1243dSDimitry Andric     _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
15804eeddc0SDimitry Andric     if (__t > __d)
15904eeddc0SDimitry Andric         __t = __t - _ToDuration{1};
16004eeddc0SDimitry Andric     return __t;
16104eeddc0SDimitry Andric }
16204eeddc0SDimitry Andric 
16304eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
16404eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
16504eeddc0SDimitry Andric typename enable_if
16604eeddc0SDimitry Andric <
16704eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
16804eeddc0SDimitry Andric     _ToDuration
16904eeddc0SDimitry Andric >::type
17004eeddc0SDimitry Andric ceil(const duration<_Rep, _Period>& __d)
17104eeddc0SDimitry Andric {
172*bdd1243dSDimitry Andric     _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
17304eeddc0SDimitry Andric     if (__t < __d)
17404eeddc0SDimitry Andric         __t = __t + _ToDuration{1};
17504eeddc0SDimitry Andric     return __t;
17604eeddc0SDimitry Andric }
17704eeddc0SDimitry Andric 
17804eeddc0SDimitry Andric template <class _ToDuration, class _Rep, class _Period>
17904eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
18004eeddc0SDimitry Andric typename enable_if
18104eeddc0SDimitry Andric <
18204eeddc0SDimitry Andric     __is_duration<_ToDuration>::value,
18304eeddc0SDimitry Andric     _ToDuration
18404eeddc0SDimitry Andric >::type
18504eeddc0SDimitry Andric round(const duration<_Rep, _Period>& __d)
18604eeddc0SDimitry Andric {
187*bdd1243dSDimitry Andric     _ToDuration __lower = chrono::floor<_ToDuration>(__d);
18804eeddc0SDimitry Andric     _ToDuration __upper = __lower + _ToDuration{1};
18904eeddc0SDimitry Andric     auto __lowerDiff = __d - __lower;
19004eeddc0SDimitry Andric     auto __upperDiff = __upper - __d;
19104eeddc0SDimitry Andric     if (__lowerDiff < __upperDiff)
19204eeddc0SDimitry Andric         return __lower;
19304eeddc0SDimitry Andric     if (__lowerDiff > __upperDiff)
19404eeddc0SDimitry Andric         return __upper;
19504eeddc0SDimitry Andric     return __lower.count() & 1 ? __upper : __lower;
19604eeddc0SDimitry Andric }
19704eeddc0SDimitry Andric #endif
19804eeddc0SDimitry Andric 
19904eeddc0SDimitry Andric // duration
20004eeddc0SDimitry Andric 
20104eeddc0SDimitry Andric template <class _Rep, class _Period>
20204eeddc0SDimitry Andric class _LIBCPP_TEMPLATE_VIS duration
20304eeddc0SDimitry Andric {
20404eeddc0SDimitry Andric     static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
20504eeddc0SDimitry Andric     static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
20604eeddc0SDimitry Andric     static_assert(_Period::num > 0, "duration period must be positive");
20704eeddc0SDimitry Andric 
20804eeddc0SDimitry Andric     template <class _R1, class _R2>
20904eeddc0SDimitry Andric     struct __no_overflow
21004eeddc0SDimitry Andric     {
21104eeddc0SDimitry Andric     private:
21204eeddc0SDimitry Andric         static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
21304eeddc0SDimitry Andric         static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
21404eeddc0SDimitry Andric         static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
21504eeddc0SDimitry Andric         static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
21604eeddc0SDimitry Andric         static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
21704eeddc0SDimitry Andric         static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
21804eeddc0SDimitry Andric         static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
21904eeddc0SDimitry Andric 
22004eeddc0SDimitry Andric         template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
22104eeddc0SDimitry Andric         struct __mul    // __overflow == false
22204eeddc0SDimitry Andric         {
22304eeddc0SDimitry Andric             static const intmax_t value = _Xp * _Yp;
22404eeddc0SDimitry Andric         };
22504eeddc0SDimitry Andric 
22604eeddc0SDimitry Andric         template <intmax_t _Xp, intmax_t _Yp>
22704eeddc0SDimitry Andric         struct __mul<_Xp, _Yp, true>
22804eeddc0SDimitry Andric         {
22904eeddc0SDimitry Andric             static const intmax_t value = 1;
23004eeddc0SDimitry Andric         };
23104eeddc0SDimitry Andric 
23204eeddc0SDimitry Andric     public:
23304eeddc0SDimitry Andric         static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
23404eeddc0SDimitry Andric         typedef ratio<__mul<__n1, __d2, !value>::value,
23504eeddc0SDimitry Andric                       __mul<__n2, __d1, !value>::value> type;
23604eeddc0SDimitry Andric     };
23704eeddc0SDimitry Andric 
23804eeddc0SDimitry Andric public:
23904eeddc0SDimitry Andric     typedef _Rep rep;
24004eeddc0SDimitry Andric     typedef typename _Period::type period;
24104eeddc0SDimitry Andric private:
24204eeddc0SDimitry Andric     rep __rep_;
24304eeddc0SDimitry Andric public:
24404eeddc0SDimitry Andric 
24504eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
24604eeddc0SDimitry Andric #ifndef _LIBCPP_CXX03_LANG
24704eeddc0SDimitry Andric         duration() = default;
24804eeddc0SDimitry Andric #else
24904eeddc0SDimitry Andric         duration() {}
25004eeddc0SDimitry Andric #endif
25104eeddc0SDimitry Andric 
25204eeddc0SDimitry Andric     template <class _Rep2>
25304eeddc0SDimitry Andric         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
25404eeddc0SDimitry Andric         explicit duration(const _Rep2& __r,
25504eeddc0SDimitry Andric             typename enable_if
25604eeddc0SDimitry Andric             <
2571838bd0fSDimitry Andric                is_convertible<const _Rep2&, rep>::value &&
25804eeddc0SDimitry Andric                (treat_as_floating_point<rep>::value ||
25904eeddc0SDimitry Andric                !treat_as_floating_point<_Rep2>::value)
26004eeddc0SDimitry Andric             >::type* = nullptr)
26104eeddc0SDimitry Andric                 : __rep_(__r) {}
26204eeddc0SDimitry Andric 
26304eeddc0SDimitry Andric     // conversions
26404eeddc0SDimitry Andric     template <class _Rep2, class _Period2>
26504eeddc0SDimitry Andric         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
26604eeddc0SDimitry Andric         duration(const duration<_Rep2, _Period2>& __d,
26704eeddc0SDimitry Andric             typename enable_if
26804eeddc0SDimitry Andric             <
26904eeddc0SDimitry Andric                 __no_overflow<_Period2, period>::value && (
27004eeddc0SDimitry Andric                 treat_as_floating_point<rep>::value ||
27104eeddc0SDimitry Andric                 (__no_overflow<_Period2, period>::type::den == 1 &&
27204eeddc0SDimitry Andric                  !treat_as_floating_point<_Rep2>::value))
27304eeddc0SDimitry Andric             >::type* = nullptr)
27404eeddc0SDimitry Andric                 : __rep_(chrono::duration_cast<duration>(__d).count()) {}
27504eeddc0SDimitry Andric 
27604eeddc0SDimitry Andric     // observer
27704eeddc0SDimitry Andric 
27804eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
27904eeddc0SDimitry Andric 
28004eeddc0SDimitry Andric     // arithmetic
28104eeddc0SDimitry Andric 
28204eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
28304eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
284*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator++()      {++__rep_; return *this;}
285*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration  operator++(int)   {return duration(__rep_++);}
286*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator--()      {--__rep_; return *this;}
287*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration  operator--(int)   {return duration(__rep_--);}
28804eeddc0SDimitry Andric 
289*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
290*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
29104eeddc0SDimitry Andric 
292*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator*=(const rep& __rhs) {__rep_ *= __rhs; return *this;}
293*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator/=(const rep& __rhs) {__rep_ /= __rhs; return *this;}
294*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const rep& __rhs) {__rep_ %= __rhs; return *this;}
295*bdd1243dSDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const duration& __rhs) {__rep_ %= __rhs.count(); return *this;}
29604eeddc0SDimitry Andric 
29704eeddc0SDimitry Andric     // special values
29804eeddc0SDimitry Andric 
29904eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() _NOEXCEPT {return duration(duration_values<rep>::zero());}
30004eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min()  _NOEXCEPT {return duration(duration_values<rep>::min());}
30104eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max()  _NOEXCEPT {return duration(duration_values<rep>::max());}
30204eeddc0SDimitry Andric };
30304eeddc0SDimitry Andric 
30404eeddc0SDimitry Andric typedef duration<long long,         nano> nanoseconds;
30504eeddc0SDimitry Andric typedef duration<long long,        micro> microseconds;
30604eeddc0SDimitry Andric typedef duration<long long,        milli> milliseconds;
30704eeddc0SDimitry Andric typedef duration<long long              > seconds;
30804eeddc0SDimitry Andric typedef duration<     long, ratio<  60> > minutes;
30904eeddc0SDimitry Andric typedef duration<     long, ratio<3600> > hours;
31004eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 17
31104eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<24>, hours::period>>         days;
31204eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<7>,   days::period>>         weeks;
31304eeddc0SDimitry Andric typedef duration<     int, ratio_multiply<ratio<146097, 400>, days::period>> years;
31404eeddc0SDimitry Andric typedef duration<     int, ratio_divide<years::period, ratio<12>>>           months;
31504eeddc0SDimitry Andric #endif
31604eeddc0SDimitry Andric // Duration ==
31704eeddc0SDimitry Andric 
31804eeddc0SDimitry Andric template <class _LhsDuration, class _RhsDuration>
31904eeddc0SDimitry Andric struct __duration_eq
32004eeddc0SDimitry Andric {
32104eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
32204eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
32304eeddc0SDimitry Andric         {
32404eeddc0SDimitry Andric             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
32504eeddc0SDimitry Andric             return _Ct(__lhs).count() == _Ct(__rhs).count();
32604eeddc0SDimitry Andric         }
32704eeddc0SDimitry Andric };
32804eeddc0SDimitry Andric 
32904eeddc0SDimitry Andric template <class _LhsDuration>
33004eeddc0SDimitry Andric struct __duration_eq<_LhsDuration, _LhsDuration>
33104eeddc0SDimitry Andric {
33204eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
33304eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
33404eeddc0SDimitry Andric         {return __lhs.count() == __rhs.count();}
33504eeddc0SDimitry Andric };
33604eeddc0SDimitry Andric 
33704eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
33804eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
33904eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
34004eeddc0SDimitry Andric bool
34104eeddc0SDimitry Andric operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
34204eeddc0SDimitry Andric {
34304eeddc0SDimitry Andric     return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
34404eeddc0SDimitry Andric }
34504eeddc0SDimitry Andric 
34604eeddc0SDimitry Andric // Duration !=
34704eeddc0SDimitry Andric 
34804eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
34904eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
35004eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
35104eeddc0SDimitry Andric bool
35204eeddc0SDimitry Andric operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
35304eeddc0SDimitry Andric {
35404eeddc0SDimitry Andric     return !(__lhs == __rhs);
35504eeddc0SDimitry Andric }
35604eeddc0SDimitry Andric 
35704eeddc0SDimitry Andric // Duration <
35804eeddc0SDimitry Andric 
35904eeddc0SDimitry Andric template <class _LhsDuration, class _RhsDuration>
36004eeddc0SDimitry Andric struct __duration_lt
36104eeddc0SDimitry Andric {
36204eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
36304eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
36404eeddc0SDimitry Andric         {
36504eeddc0SDimitry Andric             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
36604eeddc0SDimitry Andric             return _Ct(__lhs).count() < _Ct(__rhs).count();
36704eeddc0SDimitry Andric         }
36804eeddc0SDimitry Andric };
36904eeddc0SDimitry Andric 
37004eeddc0SDimitry Andric template <class _LhsDuration>
37104eeddc0SDimitry Andric struct __duration_lt<_LhsDuration, _LhsDuration>
37204eeddc0SDimitry Andric {
37304eeddc0SDimitry Andric     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
37404eeddc0SDimitry Andric     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
37504eeddc0SDimitry Andric         {return __lhs.count() < __rhs.count();}
37604eeddc0SDimitry Andric };
37704eeddc0SDimitry Andric 
37804eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
37904eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
38004eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
38104eeddc0SDimitry Andric bool
38204eeddc0SDimitry Andric operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
38304eeddc0SDimitry Andric {
38404eeddc0SDimitry Andric     return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
38504eeddc0SDimitry Andric }
38604eeddc0SDimitry Andric 
38704eeddc0SDimitry Andric // Duration >
38804eeddc0SDimitry Andric 
38904eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
39004eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
39104eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
39204eeddc0SDimitry Andric bool
39304eeddc0SDimitry Andric operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
39404eeddc0SDimitry Andric {
39504eeddc0SDimitry Andric     return __rhs < __lhs;
39604eeddc0SDimitry Andric }
39704eeddc0SDimitry Andric 
39804eeddc0SDimitry Andric // Duration <=
39904eeddc0SDimitry Andric 
40004eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
40104eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
40204eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
40304eeddc0SDimitry Andric bool
40404eeddc0SDimitry Andric operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
40504eeddc0SDimitry Andric {
40604eeddc0SDimitry Andric     return !(__rhs < __lhs);
40704eeddc0SDimitry Andric }
40804eeddc0SDimitry Andric 
40904eeddc0SDimitry Andric // Duration >=
41004eeddc0SDimitry Andric 
41104eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
41204eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
41304eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
41404eeddc0SDimitry Andric bool
41504eeddc0SDimitry Andric operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
41604eeddc0SDimitry Andric {
41704eeddc0SDimitry Andric     return !(__lhs < __rhs);
41804eeddc0SDimitry Andric }
41904eeddc0SDimitry Andric 
42004eeddc0SDimitry Andric // Duration +
42104eeddc0SDimitry Andric 
42204eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
42304eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
42404eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
42504eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
42604eeddc0SDimitry Andric operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
42704eeddc0SDimitry Andric {
42804eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
42904eeddc0SDimitry Andric     return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
43004eeddc0SDimitry Andric }
43104eeddc0SDimitry Andric 
43204eeddc0SDimitry Andric // Duration -
43304eeddc0SDimitry Andric 
43404eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
43504eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
43604eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
43704eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
43804eeddc0SDimitry Andric operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
43904eeddc0SDimitry Andric {
44004eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
44104eeddc0SDimitry Andric     return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
44204eeddc0SDimitry Andric }
44304eeddc0SDimitry Andric 
44404eeddc0SDimitry Andric // Duration *
44504eeddc0SDimitry Andric 
44604eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
44704eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
44804eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
44904eeddc0SDimitry Andric typename enable_if
45004eeddc0SDimitry Andric <
45104eeddc0SDimitry Andric     is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
45204eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
45304eeddc0SDimitry Andric >::type
45404eeddc0SDimitry Andric operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
45504eeddc0SDimitry Andric {
45604eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
45704eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
45804eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
45904eeddc0SDimitry Andric }
46004eeddc0SDimitry Andric 
46104eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
46204eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
46304eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
46404eeddc0SDimitry Andric typename enable_if
46504eeddc0SDimitry Andric <
46604eeddc0SDimitry Andric     is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
46704eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
46804eeddc0SDimitry Andric >::type
46904eeddc0SDimitry Andric operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
47004eeddc0SDimitry Andric {
47104eeddc0SDimitry Andric     return __d * __s;
47204eeddc0SDimitry Andric }
47304eeddc0SDimitry Andric 
47404eeddc0SDimitry Andric // Duration /
47504eeddc0SDimitry Andric 
47604eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
47704eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
47804eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
47904eeddc0SDimitry Andric typename enable_if
48004eeddc0SDimitry Andric <
48104eeddc0SDimitry Andric     !__is_duration<_Rep2>::value &&
48204eeddc0SDimitry Andric       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
48304eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
48404eeddc0SDimitry Andric >::type
48504eeddc0SDimitry Andric operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
48604eeddc0SDimitry Andric {
48704eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
48804eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
48904eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
49004eeddc0SDimitry Andric }
49104eeddc0SDimitry Andric 
49204eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
49304eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
49404eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
49504eeddc0SDimitry Andric typename common_type<_Rep1, _Rep2>::type
49604eeddc0SDimitry Andric operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
49704eeddc0SDimitry Andric {
49804eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
49904eeddc0SDimitry Andric     return _Ct(__lhs).count() / _Ct(__rhs).count();
50004eeddc0SDimitry Andric }
50104eeddc0SDimitry Andric 
50204eeddc0SDimitry Andric // Duration %
50304eeddc0SDimitry Andric 
50404eeddc0SDimitry Andric template <class _Rep1, class _Period, class _Rep2>
50504eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
50604eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
50704eeddc0SDimitry Andric typename enable_if
50804eeddc0SDimitry Andric <
50904eeddc0SDimitry Andric     !__is_duration<_Rep2>::value &&
51004eeddc0SDimitry Andric       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
51104eeddc0SDimitry Andric     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
51204eeddc0SDimitry Andric >::type
51304eeddc0SDimitry Andric operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
51404eeddc0SDimitry Andric {
51504eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
51604eeddc0SDimitry Andric     typedef duration<_Cr, _Period> _Cd;
51704eeddc0SDimitry Andric     return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
51804eeddc0SDimitry Andric }
51904eeddc0SDimitry Andric 
52004eeddc0SDimitry Andric template <class _Rep1, class _Period1, class _Rep2, class _Period2>
52104eeddc0SDimitry Andric inline _LIBCPP_INLINE_VISIBILITY
52204eeddc0SDimitry Andric _LIBCPP_CONSTEXPR
52304eeddc0SDimitry Andric typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
52404eeddc0SDimitry Andric operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
52504eeddc0SDimitry Andric {
52604eeddc0SDimitry Andric     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
52704eeddc0SDimitry Andric     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
52804eeddc0SDimitry Andric     return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
52904eeddc0SDimitry Andric }
53004eeddc0SDimitry Andric 
53104eeddc0SDimitry Andric } // namespace chrono
53204eeddc0SDimitry Andric 
53304eeddc0SDimitry Andric #if _LIBCPP_STD_VER > 11
53404eeddc0SDimitry Andric // Suffixes for duration literals [time.duration.literals]
53504eeddc0SDimitry Andric inline namespace literals
53604eeddc0SDimitry Andric {
53704eeddc0SDimitry Andric   inline namespace chrono_literals
53804eeddc0SDimitry Andric   {
53904eeddc0SDimitry Andric 
540*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours operator""h(unsigned long long __h)
54104eeddc0SDimitry Andric     {
54204eeddc0SDimitry Andric         return chrono::hours(static_cast<chrono::hours::rep>(__h));
54304eeddc0SDimitry Andric     }
54404eeddc0SDimitry Andric 
545*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
54604eeddc0SDimitry Andric     {
54704eeddc0SDimitry Andric         return chrono::duration<long double, ratio<3600,1>>(__h);
54804eeddc0SDimitry Andric     }
54904eeddc0SDimitry Andric 
55004eeddc0SDimitry Andric 
551*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes operator""min(unsigned long long __m)
55204eeddc0SDimitry Andric     {
55304eeddc0SDimitry Andric         return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
55404eeddc0SDimitry Andric     }
55504eeddc0SDimitry Andric 
556*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
55704eeddc0SDimitry Andric     {
55804eeddc0SDimitry Andric         return chrono::duration<long double, ratio<60,1>> (__m);
55904eeddc0SDimitry Andric     }
56004eeddc0SDimitry Andric 
56104eeddc0SDimitry Andric 
562*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds operator""s(unsigned long long __s)
56304eeddc0SDimitry Andric     {
56404eeddc0SDimitry Andric         return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
56504eeddc0SDimitry Andric     }
56604eeddc0SDimitry Andric 
567*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double> operator""s(long double __s)
56804eeddc0SDimitry Andric     {
56904eeddc0SDimitry Andric         return chrono::duration<long double> (__s);
57004eeddc0SDimitry Andric     }
57104eeddc0SDimitry Andric 
57204eeddc0SDimitry Andric 
573*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
57404eeddc0SDimitry Andric     {
57504eeddc0SDimitry Andric         return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
57604eeddc0SDimitry Andric     }
57704eeddc0SDimitry Andric 
578*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
57904eeddc0SDimitry Andric     {
58004eeddc0SDimitry Andric         return chrono::duration<long double, milli>(__ms);
58104eeddc0SDimitry Andric     }
58204eeddc0SDimitry Andric 
58304eeddc0SDimitry Andric 
584*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::microseconds operator""us(unsigned long long __us)
58504eeddc0SDimitry Andric     {
58604eeddc0SDimitry Andric         return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
58704eeddc0SDimitry Andric     }
58804eeddc0SDimitry Andric 
589*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, micro> operator""us(long double __us)
59004eeddc0SDimitry Andric     {
59104eeddc0SDimitry Andric         return chrono::duration<long double, micro> (__us);
59204eeddc0SDimitry Andric     }
59304eeddc0SDimitry Andric 
59404eeddc0SDimitry Andric 
595*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
59604eeddc0SDimitry Andric     {
59704eeddc0SDimitry Andric         return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
59804eeddc0SDimitry Andric     }
59904eeddc0SDimitry Andric 
600*bdd1243dSDimitry Andric     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
60104eeddc0SDimitry Andric     {
60204eeddc0SDimitry Andric         return chrono::duration<long double, nano> (__ns);
60304eeddc0SDimitry Andric     }
60404eeddc0SDimitry Andric 
60504eeddc0SDimitry Andric } // namespace chrono_literals
60604eeddc0SDimitry Andric } // namespace literals
60704eeddc0SDimitry Andric 
60804eeddc0SDimitry Andric namespace chrono { // hoist the literals into namespace std::chrono
60904eeddc0SDimitry Andric    using namespace literals::chrono_literals;
61004eeddc0SDimitry Andric } // namespace chrono
61104eeddc0SDimitry Andric 
61204eeddc0SDimitry Andric #endif // _LIBCPP_STD_VER > 11
61304eeddc0SDimitry Andric 
61404eeddc0SDimitry Andric _LIBCPP_END_NAMESPACE_STD
61504eeddc0SDimitry Andric 
61604eeddc0SDimitry Andric _LIBCPP_POP_MACROS
61704eeddc0SDimitry Andric 
618*bdd1243dSDimitry Andric #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
619*bdd1243dSDimitry Andric #  include <type_traits>
620*bdd1243dSDimitry Andric #endif
621*bdd1243dSDimitry Andric 
62204eeddc0SDimitry Andric #endif // _LIBCPP___CHRONO_DURATION_H
623