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