xref: /openbsd-src/gnu/llvm/libcxx/include/__chrono/duration.h (revision 4bdff4bed0e3d54e55670334c7d0077db4170f86)
1*4bdff4beSrobert // -*- C++ -*-
2*4bdff4beSrobert //===----------------------------------------------------------------------===//
3*4bdff4beSrobert //
4*4bdff4beSrobert // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5*4bdff4beSrobert // See https://llvm.org/LICENSE.txt for license information.
6*4bdff4beSrobert // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7*4bdff4beSrobert //
8*4bdff4beSrobert //===----------------------------------------------------------------------===//
9*4bdff4beSrobert 
10*4bdff4beSrobert #ifndef _LIBCPP___CHRONO_DURATION_H
11*4bdff4beSrobert #define _LIBCPP___CHRONO_DURATION_H
12*4bdff4beSrobert 
13*4bdff4beSrobert #include <__config>
14*4bdff4beSrobert #include <__type_traits/common_type.h>
15*4bdff4beSrobert #include <__type_traits/enable_if.h>
16*4bdff4beSrobert #include <__type_traits/is_convertible.h>
17*4bdff4beSrobert #include <__type_traits/is_floating_point.h>
18*4bdff4beSrobert #include <limits>
19*4bdff4beSrobert #include <ratio>
20*4bdff4beSrobert 
21*4bdff4beSrobert #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22*4bdff4beSrobert #  pragma GCC system_header
23*4bdff4beSrobert #endif
24*4bdff4beSrobert 
25*4bdff4beSrobert _LIBCPP_PUSH_MACROS
26*4bdff4beSrobert #include <__undef_macros>
27*4bdff4beSrobert 
28*4bdff4beSrobert _LIBCPP_BEGIN_NAMESPACE_STD
29*4bdff4beSrobert 
30*4bdff4beSrobert namespace chrono
31*4bdff4beSrobert {
32*4bdff4beSrobert 
33*4bdff4beSrobert template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
34*4bdff4beSrobert 
35*4bdff4beSrobert template <class _Tp>
36*4bdff4beSrobert struct __is_duration : false_type {};
37*4bdff4beSrobert 
38*4bdff4beSrobert template <class _Rep, class _Period>
39*4bdff4beSrobert struct __is_duration<duration<_Rep, _Period> > : true_type  {};
40*4bdff4beSrobert 
41*4bdff4beSrobert template <class _Rep, class _Period>
42*4bdff4beSrobert struct __is_duration<const duration<_Rep, _Period> > : true_type  {};
43*4bdff4beSrobert 
44*4bdff4beSrobert template <class _Rep, class _Period>
45*4bdff4beSrobert struct __is_duration<volatile duration<_Rep, _Period> > : true_type  {};
46*4bdff4beSrobert 
47*4bdff4beSrobert template <class _Rep, class _Period>
48*4bdff4beSrobert struct __is_duration<const volatile duration<_Rep, _Period> > : true_type  {};
49*4bdff4beSrobert 
50*4bdff4beSrobert } // namespace chrono
51*4bdff4beSrobert 
52*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
53*4bdff4beSrobert struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
54*4bdff4beSrobert                                          chrono::duration<_Rep2, _Period2> >
55*4bdff4beSrobert {
56*4bdff4beSrobert     typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
57*4bdff4beSrobert                              typename __ratio_gcd<_Period1, _Period2>::type> type;
58*4bdff4beSrobert };
59*4bdff4beSrobert 
60*4bdff4beSrobert namespace chrono {
61*4bdff4beSrobert 
62*4bdff4beSrobert // duration_cast
63*4bdff4beSrobert 
64*4bdff4beSrobert template <class _FromDuration, class _ToDuration,
65*4bdff4beSrobert           class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
66*4bdff4beSrobert           bool = _Period::num == 1,
67*4bdff4beSrobert           bool = _Period::den == 1>
68*4bdff4beSrobert struct __duration_cast;
69*4bdff4beSrobert 
70*4bdff4beSrobert template <class _FromDuration, class _ToDuration, class _Period>
71*4bdff4beSrobert struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
72*4bdff4beSrobert {
73*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
74*4bdff4beSrobert     _ToDuration operator()(const _FromDuration& __fd) const
75*4bdff4beSrobert     {
76*4bdff4beSrobert         return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
77*4bdff4beSrobert     }
78*4bdff4beSrobert };
79*4bdff4beSrobert 
80*4bdff4beSrobert template <class _FromDuration, class _ToDuration, class _Period>
81*4bdff4beSrobert struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
82*4bdff4beSrobert {
83*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
84*4bdff4beSrobert     _ToDuration operator()(const _FromDuration& __fd) const
85*4bdff4beSrobert     {
86*4bdff4beSrobert         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
87*4bdff4beSrobert         return _ToDuration(static_cast<typename _ToDuration::rep>(
88*4bdff4beSrobert                            static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
89*4bdff4beSrobert     }
90*4bdff4beSrobert };
91*4bdff4beSrobert 
92*4bdff4beSrobert template <class _FromDuration, class _ToDuration, class _Period>
93*4bdff4beSrobert struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
94*4bdff4beSrobert {
95*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
96*4bdff4beSrobert     _ToDuration operator()(const _FromDuration& __fd) const
97*4bdff4beSrobert     {
98*4bdff4beSrobert         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
99*4bdff4beSrobert         return _ToDuration(static_cast<typename _ToDuration::rep>(
100*4bdff4beSrobert                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
101*4bdff4beSrobert     }
102*4bdff4beSrobert };
103*4bdff4beSrobert 
104*4bdff4beSrobert template <class _FromDuration, class _ToDuration, class _Period>
105*4bdff4beSrobert struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
106*4bdff4beSrobert {
107*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
108*4bdff4beSrobert     _ToDuration operator()(const _FromDuration& __fd) const
109*4bdff4beSrobert     {
110*4bdff4beSrobert         typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
111*4bdff4beSrobert         return _ToDuration(static_cast<typename _ToDuration::rep>(
112*4bdff4beSrobert                            static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
113*4bdff4beSrobert                                                           / static_cast<_Ct>(_Period::den)));
114*4bdff4beSrobert     }
115*4bdff4beSrobert };
116*4bdff4beSrobert 
117*4bdff4beSrobert template <class _ToDuration, class _Rep, class _Period>
118*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
119*4bdff4beSrobert _LIBCPP_CONSTEXPR
120*4bdff4beSrobert typename enable_if
121*4bdff4beSrobert <
122*4bdff4beSrobert     __is_duration<_ToDuration>::value,
123*4bdff4beSrobert     _ToDuration
124*4bdff4beSrobert >::type
125*4bdff4beSrobert duration_cast(const duration<_Rep, _Period>& __fd)
126*4bdff4beSrobert {
127*4bdff4beSrobert     return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
128*4bdff4beSrobert }
129*4bdff4beSrobert 
130*4bdff4beSrobert template <class _Rep>
131*4bdff4beSrobert struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
132*4bdff4beSrobert 
133*4bdff4beSrobert #if _LIBCPP_STD_VER > 14
134*4bdff4beSrobert template <class _Rep>
135*4bdff4beSrobert inline constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
136*4bdff4beSrobert #endif
137*4bdff4beSrobert 
138*4bdff4beSrobert template <class _Rep>
139*4bdff4beSrobert struct _LIBCPP_TEMPLATE_VIS duration_values
140*4bdff4beSrobert {
141*4bdff4beSrobert public:
142*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() _NOEXCEPT {return _Rep(0);}
143*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max()  _NOEXCEPT {return numeric_limits<_Rep>::max();}
144*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min()  _NOEXCEPT {return numeric_limits<_Rep>::lowest();}
145*4bdff4beSrobert };
146*4bdff4beSrobert 
147*4bdff4beSrobert #if _LIBCPP_STD_VER > 14
148*4bdff4beSrobert template <class _ToDuration, class _Rep, class _Period>
149*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
150*4bdff4beSrobert typename enable_if
151*4bdff4beSrobert <
152*4bdff4beSrobert     __is_duration<_ToDuration>::value,
153*4bdff4beSrobert     _ToDuration
154*4bdff4beSrobert >::type
155*4bdff4beSrobert floor(const duration<_Rep, _Period>& __d)
156*4bdff4beSrobert {
157*4bdff4beSrobert     _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
158*4bdff4beSrobert     if (__t > __d)
159*4bdff4beSrobert         __t = __t - _ToDuration{1};
160*4bdff4beSrobert     return __t;
161*4bdff4beSrobert }
162*4bdff4beSrobert 
163*4bdff4beSrobert template <class _ToDuration, class _Rep, class _Period>
164*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
165*4bdff4beSrobert typename enable_if
166*4bdff4beSrobert <
167*4bdff4beSrobert     __is_duration<_ToDuration>::value,
168*4bdff4beSrobert     _ToDuration
169*4bdff4beSrobert >::type
170*4bdff4beSrobert ceil(const duration<_Rep, _Period>& __d)
171*4bdff4beSrobert {
172*4bdff4beSrobert     _ToDuration __t = chrono::duration_cast<_ToDuration>(__d);
173*4bdff4beSrobert     if (__t < __d)
174*4bdff4beSrobert         __t = __t + _ToDuration{1};
175*4bdff4beSrobert     return __t;
176*4bdff4beSrobert }
177*4bdff4beSrobert 
178*4bdff4beSrobert template <class _ToDuration, class _Rep, class _Period>
179*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
180*4bdff4beSrobert typename enable_if
181*4bdff4beSrobert <
182*4bdff4beSrobert     __is_duration<_ToDuration>::value,
183*4bdff4beSrobert     _ToDuration
184*4bdff4beSrobert >::type
185*4bdff4beSrobert round(const duration<_Rep, _Period>& __d)
186*4bdff4beSrobert {
187*4bdff4beSrobert     _ToDuration __lower = chrono::floor<_ToDuration>(__d);
188*4bdff4beSrobert     _ToDuration __upper = __lower + _ToDuration{1};
189*4bdff4beSrobert     auto __lowerDiff = __d - __lower;
190*4bdff4beSrobert     auto __upperDiff = __upper - __d;
191*4bdff4beSrobert     if (__lowerDiff < __upperDiff)
192*4bdff4beSrobert         return __lower;
193*4bdff4beSrobert     if (__lowerDiff > __upperDiff)
194*4bdff4beSrobert         return __upper;
195*4bdff4beSrobert     return __lower.count() & 1 ? __upper : __lower;
196*4bdff4beSrobert }
197*4bdff4beSrobert #endif
198*4bdff4beSrobert 
199*4bdff4beSrobert // duration
200*4bdff4beSrobert 
201*4bdff4beSrobert template <class _Rep, class _Period>
202*4bdff4beSrobert class _LIBCPP_TEMPLATE_VIS duration
203*4bdff4beSrobert {
204*4bdff4beSrobert     static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
205*4bdff4beSrobert     static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
206*4bdff4beSrobert     static_assert(_Period::num > 0, "duration period must be positive");
207*4bdff4beSrobert 
208*4bdff4beSrobert     template <class _R1, class _R2>
209*4bdff4beSrobert     struct __no_overflow
210*4bdff4beSrobert     {
211*4bdff4beSrobert     private:
212*4bdff4beSrobert         static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
213*4bdff4beSrobert         static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
214*4bdff4beSrobert         static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
215*4bdff4beSrobert         static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
216*4bdff4beSrobert         static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
217*4bdff4beSrobert         static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
218*4bdff4beSrobert         static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
219*4bdff4beSrobert 
220*4bdff4beSrobert         template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
221*4bdff4beSrobert         struct __mul    // __overflow == false
222*4bdff4beSrobert         {
223*4bdff4beSrobert             static const intmax_t value = _Xp * _Yp;
224*4bdff4beSrobert         };
225*4bdff4beSrobert 
226*4bdff4beSrobert         template <intmax_t _Xp, intmax_t _Yp>
227*4bdff4beSrobert         struct __mul<_Xp, _Yp, true>
228*4bdff4beSrobert         {
229*4bdff4beSrobert             static const intmax_t value = 1;
230*4bdff4beSrobert         };
231*4bdff4beSrobert 
232*4bdff4beSrobert     public:
233*4bdff4beSrobert         static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
234*4bdff4beSrobert         typedef ratio<__mul<__n1, __d2, !value>::value,
235*4bdff4beSrobert                       __mul<__n2, __d1, !value>::value> type;
236*4bdff4beSrobert     };
237*4bdff4beSrobert 
238*4bdff4beSrobert public:
239*4bdff4beSrobert     typedef _Rep rep;
240*4bdff4beSrobert     typedef typename _Period::type period;
241*4bdff4beSrobert private:
242*4bdff4beSrobert     rep __rep_;
243*4bdff4beSrobert public:
244*4bdff4beSrobert 
245*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
246*4bdff4beSrobert #ifndef _LIBCPP_CXX03_LANG
247*4bdff4beSrobert         duration() = default;
248*4bdff4beSrobert #else
249*4bdff4beSrobert         duration() {}
250*4bdff4beSrobert #endif
251*4bdff4beSrobert 
252*4bdff4beSrobert     template <class _Rep2>
253*4bdff4beSrobert         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
254*4bdff4beSrobert         explicit duration(const _Rep2& __r,
255*4bdff4beSrobert             typename enable_if
256*4bdff4beSrobert             <
257*4bdff4beSrobert                is_convertible<const _Rep2&, rep>::value &&
258*4bdff4beSrobert                (treat_as_floating_point<rep>::value ||
259*4bdff4beSrobert                !treat_as_floating_point<_Rep2>::value)
260*4bdff4beSrobert             >::type* = nullptr)
261*4bdff4beSrobert                 : __rep_(__r) {}
262*4bdff4beSrobert 
263*4bdff4beSrobert     // conversions
264*4bdff4beSrobert     template <class _Rep2, class _Period2>
265*4bdff4beSrobert         _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
266*4bdff4beSrobert         duration(const duration<_Rep2, _Period2>& __d,
267*4bdff4beSrobert             typename enable_if
268*4bdff4beSrobert             <
269*4bdff4beSrobert                 __no_overflow<_Period2, period>::value && (
270*4bdff4beSrobert                 treat_as_floating_point<rep>::value ||
271*4bdff4beSrobert                 (__no_overflow<_Period2, period>::type::den == 1 &&
272*4bdff4beSrobert                  !treat_as_floating_point<_Rep2>::value))
273*4bdff4beSrobert             >::type* = nullptr)
274*4bdff4beSrobert                 : __rep_(chrono::duration_cast<duration>(__d).count()) {}
275*4bdff4beSrobert 
276*4bdff4beSrobert     // observer
277*4bdff4beSrobert 
278*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
279*4bdff4beSrobert 
280*4bdff4beSrobert     // arithmetic
281*4bdff4beSrobert 
282*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
283*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
284*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator++()      {++__rep_; return *this;}
285*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration  operator++(int)   {return duration(__rep_++);}
286*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator--()      {--__rep_; return *this;}
287*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration  operator--(int)   {return duration(__rep_--);}
288*4bdff4beSrobert 
289*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
290*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
291*4bdff4beSrobert 
292*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator*=(const rep& __rhs) {__rep_ *= __rhs; return *this;}
293*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator/=(const rep& __rhs) {__rep_ /= __rhs; return *this;}
294*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const rep& __rhs) {__rep_ %= __rhs; return *this;}
295*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_SINCE_CXX17 duration& operator%=(const duration& __rhs) {__rep_ %= __rhs.count(); return *this;}
296*4bdff4beSrobert 
297*4bdff4beSrobert     // special values
298*4bdff4beSrobert 
299*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() _NOEXCEPT {return duration(duration_values<rep>::zero());}
300*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min()  _NOEXCEPT {return duration(duration_values<rep>::min());}
301*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max()  _NOEXCEPT {return duration(duration_values<rep>::max());}
302*4bdff4beSrobert };
303*4bdff4beSrobert 
304*4bdff4beSrobert typedef duration<long long,         nano> nanoseconds;
305*4bdff4beSrobert typedef duration<long long,        micro> microseconds;
306*4bdff4beSrobert typedef duration<long long,        milli> milliseconds;
307*4bdff4beSrobert typedef duration<long long              > seconds;
308*4bdff4beSrobert typedef duration<     long, ratio<  60> > minutes;
309*4bdff4beSrobert typedef duration<     long, ratio<3600> > hours;
310*4bdff4beSrobert #if _LIBCPP_STD_VER > 17
311*4bdff4beSrobert typedef duration<     int, ratio_multiply<ratio<24>, hours::period>>         days;
312*4bdff4beSrobert typedef duration<     int, ratio_multiply<ratio<7>,   days::period>>         weeks;
313*4bdff4beSrobert typedef duration<     int, ratio_multiply<ratio<146097, 400>, days::period>> years;
314*4bdff4beSrobert typedef duration<     int, ratio_divide<years::period, ratio<12>>>           months;
315*4bdff4beSrobert #endif
316*4bdff4beSrobert // Duration ==
317*4bdff4beSrobert 
318*4bdff4beSrobert template <class _LhsDuration, class _RhsDuration>
319*4bdff4beSrobert struct __duration_eq
320*4bdff4beSrobert {
321*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
322*4bdff4beSrobert     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
323*4bdff4beSrobert         {
324*4bdff4beSrobert             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
325*4bdff4beSrobert             return _Ct(__lhs).count() == _Ct(__rhs).count();
326*4bdff4beSrobert         }
327*4bdff4beSrobert };
328*4bdff4beSrobert 
329*4bdff4beSrobert template <class _LhsDuration>
330*4bdff4beSrobert struct __duration_eq<_LhsDuration, _LhsDuration>
331*4bdff4beSrobert {
332*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
333*4bdff4beSrobert     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
334*4bdff4beSrobert         {return __lhs.count() == __rhs.count();}
335*4bdff4beSrobert };
336*4bdff4beSrobert 
337*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
338*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
339*4bdff4beSrobert _LIBCPP_CONSTEXPR
340*4bdff4beSrobert bool
341*4bdff4beSrobert operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
342*4bdff4beSrobert {
343*4bdff4beSrobert     return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
344*4bdff4beSrobert }
345*4bdff4beSrobert 
346*4bdff4beSrobert // Duration !=
347*4bdff4beSrobert 
348*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
349*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
350*4bdff4beSrobert _LIBCPP_CONSTEXPR
351*4bdff4beSrobert bool
352*4bdff4beSrobert operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
353*4bdff4beSrobert {
354*4bdff4beSrobert     return !(__lhs == __rhs);
355*4bdff4beSrobert }
356*4bdff4beSrobert 
357*4bdff4beSrobert // Duration <
358*4bdff4beSrobert 
359*4bdff4beSrobert template <class _LhsDuration, class _RhsDuration>
360*4bdff4beSrobert struct __duration_lt
361*4bdff4beSrobert {
362*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
363*4bdff4beSrobert     bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
364*4bdff4beSrobert         {
365*4bdff4beSrobert             typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
366*4bdff4beSrobert             return _Ct(__lhs).count() < _Ct(__rhs).count();
367*4bdff4beSrobert         }
368*4bdff4beSrobert };
369*4bdff4beSrobert 
370*4bdff4beSrobert template <class _LhsDuration>
371*4bdff4beSrobert struct __duration_lt<_LhsDuration, _LhsDuration>
372*4bdff4beSrobert {
373*4bdff4beSrobert     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
374*4bdff4beSrobert     bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
375*4bdff4beSrobert         {return __lhs.count() < __rhs.count();}
376*4bdff4beSrobert };
377*4bdff4beSrobert 
378*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
379*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
380*4bdff4beSrobert _LIBCPP_CONSTEXPR
381*4bdff4beSrobert bool
382*4bdff4beSrobert operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
383*4bdff4beSrobert {
384*4bdff4beSrobert     return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
385*4bdff4beSrobert }
386*4bdff4beSrobert 
387*4bdff4beSrobert // Duration >
388*4bdff4beSrobert 
389*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
390*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
391*4bdff4beSrobert _LIBCPP_CONSTEXPR
392*4bdff4beSrobert bool
393*4bdff4beSrobert operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
394*4bdff4beSrobert {
395*4bdff4beSrobert     return __rhs < __lhs;
396*4bdff4beSrobert }
397*4bdff4beSrobert 
398*4bdff4beSrobert // Duration <=
399*4bdff4beSrobert 
400*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
401*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
402*4bdff4beSrobert _LIBCPP_CONSTEXPR
403*4bdff4beSrobert bool
404*4bdff4beSrobert operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
405*4bdff4beSrobert {
406*4bdff4beSrobert     return !(__rhs < __lhs);
407*4bdff4beSrobert }
408*4bdff4beSrobert 
409*4bdff4beSrobert // Duration >=
410*4bdff4beSrobert 
411*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
412*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
413*4bdff4beSrobert _LIBCPP_CONSTEXPR
414*4bdff4beSrobert bool
415*4bdff4beSrobert operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
416*4bdff4beSrobert {
417*4bdff4beSrobert     return !(__lhs < __rhs);
418*4bdff4beSrobert }
419*4bdff4beSrobert 
420*4bdff4beSrobert // Duration +
421*4bdff4beSrobert 
422*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
423*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
424*4bdff4beSrobert _LIBCPP_CONSTEXPR
425*4bdff4beSrobert typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
426*4bdff4beSrobert operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
427*4bdff4beSrobert {
428*4bdff4beSrobert     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
429*4bdff4beSrobert     return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
430*4bdff4beSrobert }
431*4bdff4beSrobert 
432*4bdff4beSrobert // Duration -
433*4bdff4beSrobert 
434*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
435*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
436*4bdff4beSrobert _LIBCPP_CONSTEXPR
437*4bdff4beSrobert typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
438*4bdff4beSrobert operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
439*4bdff4beSrobert {
440*4bdff4beSrobert     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
441*4bdff4beSrobert     return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
442*4bdff4beSrobert }
443*4bdff4beSrobert 
444*4bdff4beSrobert // Duration *
445*4bdff4beSrobert 
446*4bdff4beSrobert template <class _Rep1, class _Period, class _Rep2>
447*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
448*4bdff4beSrobert _LIBCPP_CONSTEXPR
449*4bdff4beSrobert typename enable_if
450*4bdff4beSrobert <
451*4bdff4beSrobert     is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
452*4bdff4beSrobert     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
453*4bdff4beSrobert >::type
454*4bdff4beSrobert operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
455*4bdff4beSrobert {
456*4bdff4beSrobert     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
457*4bdff4beSrobert     typedef duration<_Cr, _Period> _Cd;
458*4bdff4beSrobert     return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
459*4bdff4beSrobert }
460*4bdff4beSrobert 
461*4bdff4beSrobert template <class _Rep1, class _Period, class _Rep2>
462*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
463*4bdff4beSrobert _LIBCPP_CONSTEXPR
464*4bdff4beSrobert typename enable_if
465*4bdff4beSrobert <
466*4bdff4beSrobert     is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
467*4bdff4beSrobert     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
468*4bdff4beSrobert >::type
469*4bdff4beSrobert operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
470*4bdff4beSrobert {
471*4bdff4beSrobert     return __d * __s;
472*4bdff4beSrobert }
473*4bdff4beSrobert 
474*4bdff4beSrobert // Duration /
475*4bdff4beSrobert 
476*4bdff4beSrobert template <class _Rep1, class _Period, class _Rep2>
477*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
478*4bdff4beSrobert _LIBCPP_CONSTEXPR
479*4bdff4beSrobert typename enable_if
480*4bdff4beSrobert <
481*4bdff4beSrobert     !__is_duration<_Rep2>::value &&
482*4bdff4beSrobert       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
483*4bdff4beSrobert     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
484*4bdff4beSrobert >::type
485*4bdff4beSrobert operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
486*4bdff4beSrobert {
487*4bdff4beSrobert     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
488*4bdff4beSrobert     typedef duration<_Cr, _Period> _Cd;
489*4bdff4beSrobert     return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
490*4bdff4beSrobert }
491*4bdff4beSrobert 
492*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
493*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
494*4bdff4beSrobert _LIBCPP_CONSTEXPR
495*4bdff4beSrobert typename common_type<_Rep1, _Rep2>::type
496*4bdff4beSrobert operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
497*4bdff4beSrobert {
498*4bdff4beSrobert     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
499*4bdff4beSrobert     return _Ct(__lhs).count() / _Ct(__rhs).count();
500*4bdff4beSrobert }
501*4bdff4beSrobert 
502*4bdff4beSrobert // Duration %
503*4bdff4beSrobert 
504*4bdff4beSrobert template <class _Rep1, class _Period, class _Rep2>
505*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
506*4bdff4beSrobert _LIBCPP_CONSTEXPR
507*4bdff4beSrobert typename enable_if
508*4bdff4beSrobert <
509*4bdff4beSrobert     !__is_duration<_Rep2>::value &&
510*4bdff4beSrobert       is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
511*4bdff4beSrobert     duration<typename common_type<_Rep1, _Rep2>::type, _Period>
512*4bdff4beSrobert >::type
513*4bdff4beSrobert operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
514*4bdff4beSrobert {
515*4bdff4beSrobert     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
516*4bdff4beSrobert     typedef duration<_Cr, _Period> _Cd;
517*4bdff4beSrobert     return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
518*4bdff4beSrobert }
519*4bdff4beSrobert 
520*4bdff4beSrobert template <class _Rep1, class _Period1, class _Rep2, class _Period2>
521*4bdff4beSrobert inline _LIBCPP_INLINE_VISIBILITY
522*4bdff4beSrobert _LIBCPP_CONSTEXPR
523*4bdff4beSrobert typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
524*4bdff4beSrobert operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
525*4bdff4beSrobert {
526*4bdff4beSrobert     typedef typename common_type<_Rep1, _Rep2>::type _Cr;
527*4bdff4beSrobert     typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
528*4bdff4beSrobert     return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
529*4bdff4beSrobert }
530*4bdff4beSrobert 
531*4bdff4beSrobert } // namespace chrono
532*4bdff4beSrobert 
533*4bdff4beSrobert #if _LIBCPP_STD_VER > 11
534*4bdff4beSrobert // Suffixes for duration literals [time.duration.literals]
535*4bdff4beSrobert inline namespace literals
536*4bdff4beSrobert {
537*4bdff4beSrobert   inline namespace chrono_literals
538*4bdff4beSrobert   {
539*4bdff4beSrobert 
540*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::hours operator""h(unsigned long long __h)
541*4bdff4beSrobert     {
542*4bdff4beSrobert         return chrono::hours(static_cast<chrono::hours::rep>(__h));
543*4bdff4beSrobert     }
544*4bdff4beSrobert 
545*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
546*4bdff4beSrobert     {
547*4bdff4beSrobert         return chrono::duration<long double, ratio<3600,1>>(__h);
548*4bdff4beSrobert     }
549*4bdff4beSrobert 
550*4bdff4beSrobert 
551*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::minutes operator""min(unsigned long long __m)
552*4bdff4beSrobert     {
553*4bdff4beSrobert         return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
554*4bdff4beSrobert     }
555*4bdff4beSrobert 
556*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
557*4bdff4beSrobert     {
558*4bdff4beSrobert         return chrono::duration<long double, ratio<60,1>> (__m);
559*4bdff4beSrobert     }
560*4bdff4beSrobert 
561*4bdff4beSrobert 
562*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::seconds operator""s(unsigned long long __s)
563*4bdff4beSrobert     {
564*4bdff4beSrobert         return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
565*4bdff4beSrobert     }
566*4bdff4beSrobert 
567*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double> operator""s(long double __s)
568*4bdff4beSrobert     {
569*4bdff4beSrobert         return chrono::duration<long double> (__s);
570*4bdff4beSrobert     }
571*4bdff4beSrobert 
572*4bdff4beSrobert 
573*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
574*4bdff4beSrobert     {
575*4bdff4beSrobert         return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
576*4bdff4beSrobert     }
577*4bdff4beSrobert 
578*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
579*4bdff4beSrobert     {
580*4bdff4beSrobert         return chrono::duration<long double, milli>(__ms);
581*4bdff4beSrobert     }
582*4bdff4beSrobert 
583*4bdff4beSrobert 
584*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::microseconds operator""us(unsigned long long __us)
585*4bdff4beSrobert     {
586*4bdff4beSrobert         return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
587*4bdff4beSrobert     }
588*4bdff4beSrobert 
589*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, micro> operator""us(long double __us)
590*4bdff4beSrobert     {
591*4bdff4beSrobert         return chrono::duration<long double, micro> (__us);
592*4bdff4beSrobert     }
593*4bdff4beSrobert 
594*4bdff4beSrobert 
595*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
596*4bdff4beSrobert     {
597*4bdff4beSrobert         return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
598*4bdff4beSrobert     }
599*4bdff4beSrobert 
600*4bdff4beSrobert     _LIBCPP_HIDE_FROM_ABI constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
601*4bdff4beSrobert     {
602*4bdff4beSrobert         return chrono::duration<long double, nano> (__ns);
603*4bdff4beSrobert     }
604*4bdff4beSrobert 
605*4bdff4beSrobert } // namespace chrono_literals
606*4bdff4beSrobert } // namespace literals
607*4bdff4beSrobert 
608*4bdff4beSrobert namespace chrono { // hoist the literals into namespace std::chrono
609*4bdff4beSrobert    using namespace literals::chrono_literals;
610*4bdff4beSrobert } // namespace chrono
611*4bdff4beSrobert 
612*4bdff4beSrobert #endif // _LIBCPP_STD_VER > 11
613*4bdff4beSrobert 
614*4bdff4beSrobert _LIBCPP_END_NAMESPACE_STD
615*4bdff4beSrobert 
616*4bdff4beSrobert _LIBCPP_POP_MACROS
617*4bdff4beSrobert 
618*4bdff4beSrobert #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
619*4bdff4beSrobert #  include <type_traits>
620*4bdff4beSrobert #endif
621*4bdff4beSrobert 
622*4bdff4beSrobert #endif // _LIBCPP___CHRONO_DURATION_H
623