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