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