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