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