Where Online Learning is simpler!
The C and C++ Include Header Files
cat -n /usr/include/c++/15/bits/chrono.h
1 // chrono::duration and chrono::time_point -*- C++ -*- 2 3 // Copyright (C) 2008-2025 Free Software Foundation, Inc. 4 // 5 // This file is part of the GNU ISO C++ Library. This library is free 6 // software; you can redistribute it and/or modify it under the 7 // terms of the GNU General Public License as published by the 8 // Free Software Foundation; either version 3, or (at your option) 9 // any later version. 10 11 // This library is distributed in the hope that it will be useful, 12 // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 // GNU General Public License for more details. 15 16 // Under Section 7 of GPL version 3, you are granted additional 17 // permissions described in the GCC Runtime Library Exception, version 18 // 3.1, as published by the Free Software Foundation. 19 20 // You should have received a copy of the GNU General Public License and 21 // a copy of the GCC Runtime Library Exception along with this program; 22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23 // <http://www.gnu.org/licenses/>. 24 25 /** @file include/bits/chrono.h 26 * This is an internal header file, included by other library headers. 27 * Do not attempt to use it directly. @headername{chrono} 28 */ 29 30 #ifndef _GLIBCXX_CHRONO_H 31 #define _GLIBCXX_CHRONO_H 1 32 33 #ifdef _GLIBCXX_SYSHDR 34 #pragma GCC system_header 35 #endif 36 37 #if __cplusplus >= 201103L 38 39 #include <ratio> 40 #include <type_traits> 41 #include <limits> 42 #if _GLIBCXX_HOSTED 43 # include <ctime> 44 #endif 45 #include <bits/parse_numbers.h> // for literals support. 46 #if __cplusplus >= 202002L 47 # include <concepts> 48 # include <compare> 49 #endif 50 51 #include <bits/version.h> 52 53 namespace std _GLIBCXX_VISIBILITY(default) 54 { 55 _GLIBCXX_BEGIN_NAMESPACE_VERSION 56 57 #if __cplusplus >= 201703L && _GLIBCXX_HOSTED 58 namespace filesystem { struct __file_clock; }; 59 #endif 60 61 namespace chrono 62 { 63 /// @addtogroup chrono 64 /// @{ 65 66 /// `chrono::duration` represents a distance between two points in time 67 template<typename _Rep, typename _Period = ratio<1>> 68 class duration; 69 70 /// `chrono::time_point` represents a point in time as measured by a clock 71 template<typename _Clock, typename _Dur = typename _Clock::duration> 72 class time_point; 73 /// @} 74 } 75 76 /// @addtogroup chrono 77 /// @{ 78 79 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly) 80 81 /// @cond undocumented 82 83 template<typename _CT, typename _Period1, typename _Period2, typename = void> 84 struct __duration_common_type 85 { }; 86 87 template<typename _CT, typename _Period1, typename _Period2> 88 struct __duration_common_type<_CT, _Period1, _Period2, 89 __void_t<typename _CT::type>> 90 { 91 private: 92 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>; 93 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>; 94 using __cr = typename _CT::type; 95 using __r = ratio<__gcd_num::value, 96 (_Period1::den / __gcd_den::value) * _Period2::den>; 97 98 public: 99 using type = chrono::duration<__cr, typename __r::type>; 100 }; 101 102 /// @endcond 103 104 /// @{ 105 /// @relates chrono::duration 106 107 /// Specialization of common_type for chrono::duration types. 108 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> 109 struct common_type<chrono::duration<_Rep1, _Period1>, 110 chrono::duration<_Rep2, _Period2>> 111 : __duration_common_type<common_type<_Rep1, _Rep2>, 112 typename _Period1::type, 113 typename _Period2::type> 114 { }; 115 116 /// Specialization of common_type for two identical chrono::duration types. 117 template<typename _Rep, typename _Period> 118 struct common_type<chrono::duration<_Rep, _Period>, 119 chrono::duration<_Rep, _Period>> 120 { 121 using type = chrono::duration<typename common_type<_Rep>::type, 122 typename _Period::type>; 123 }; 124 125 /// Specialization of common_type for one chrono::duration type. 126 template<typename _Rep, typename _Period> 127 struct common_type<chrono::duration<_Rep, _Period>> 128 { 129 using type = chrono::duration<typename common_type<_Rep>::type, 130 typename _Period::type>; 131 }; 132 /// @} 133 134 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) 135 136 /// @cond undocumented 137 138 template<typename _CT, typename _Clock, typename = void> 139 struct __timepoint_common_type 140 { }; 141 142 template<typename _CT, typename _Clock> 143 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>> 144 { 145 using type = chrono::time_point<_Clock, typename _CT::type>; 146 }; 147 148 /// @endcond 149 150 /// @{ 151 /// @relates chrono::time_point 152 153 /// Specialization of common_type for chrono::time_point types. 154 template<typename _Clock, typename _Duration1, typename _Duration2> 155 struct common_type<chrono::time_point<_Clock, _Duration1>, 156 chrono::time_point<_Clock, _Duration2>> 157 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock> 158 { }; 159 160 /// Specialization of common_type for two identical chrono::time_point types. 161 template<typename _Clock, typename _Duration> 162 struct common_type<chrono::time_point<_Clock, _Duration>, 163 chrono::time_point<_Clock, _Duration>> 164 { using type = chrono::time_point<_Clock, _Duration>; }; 165 166 /// Specialization of common_type for one chrono::time_point type. 167 template<typename _Clock, typename _Duration> 168 struct common_type<chrono::time_point<_Clock, _Duration>> 169 { using type = chrono::time_point<_Clock, _Duration>; }; 170 /// @} 171 172 /// @} group chrono 173 174 namespace chrono 175 { 176 /// @addtogroup chrono 177 /// @{ 178 179 /// @cond undocumented 180 181 // Primary template for duration_cast impl. 182 template<typename _ToDur, typename _CF, typename _CR, 183 bool _NumIsOne = false, bool _DenIsOne = false> 184 struct __duration_cast_impl 185 { 186 template<typename _Rep, typename _Period> 187 static constexpr _ToDur 188 __cast(const duration<_Rep, _Period>& __d) 189 { 190 typedef typename _ToDur::rep __to_rep; 191 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count()) 192 * static_cast<_CR>(_CF::num) 193 / static_cast<_CR>(_CF::den))); 194 } 195 }; 196 197 template<typename _ToDur, typename _CF, typename _CR> 198 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true> 199 { 200 template<typename _Rep, typename _Period> 201 static constexpr _ToDur 202 __cast(const duration<_Rep, _Period>& __d) 203 { 204 typedef typename _ToDur::rep __to_rep; 205 return _ToDur(static_cast<__to_rep>(__d.count())); 206 } 207 }; 208 209 template<typename _ToDur, typename _CF, typename _CR> 210 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false> 211 { 212 template<typename _Rep, typename _Period> 213 static constexpr _ToDur 214 __cast(const duration<_Rep, _Period>& __d) 215 { 216 typedef typename _ToDur::rep __to_rep; 217 return _ToDur(static_cast<__to_rep>( 218 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); 219 } 220 }; 221 222 template<typename _ToDur, typename _CF, typename _CR> 223 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true> 224 { 225 template<typename _Rep, typename _Period> 226 static constexpr _ToDur 227 __cast(const duration<_Rep, _Period>& __d) 228 { 229 typedef typename _ToDur::rep __to_rep; 230 return _ToDur(static_cast<__to_rep>( 231 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num))); 232 } 233 }; 234 235 template<typename _Tp> 236 struct __is_duration 237 : std::false_type 238 { }; 239 240 template<typename _Rep, typename _Period> 241 struct __is_duration<duration<_Rep, _Period>> 242 : std::true_type 243 { }; 244 245 template<typename _Tp> 246 using __enable_if_is_duration 247 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type; 248 249 template<typename _Tp> 250 using __disable_if_is_duration 251 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type; 252 253 #if __cplusplus >= 201703L 254 template<typename _Tp> 255 inline constexpr bool __is_duration_v = false; 256 template<typename _Rep, typename _Period> 257 inline constexpr bool __is_duration_v<duration<_Rep, _Period>> = true; 258 template<typename _Tp> 259 inline constexpr bool __is_time_point_v = false; 260 template<typename _Clock, typename _Dur> 261 inline constexpr bool __is_time_point_v<time_point<_Clock, _Dur>> = true; 262 #endif 263 264 /// @endcond 265 266 /** Convert a `duration` to type `ToDur`. 267 * 268 * If the duration cannot be represented accurately in the result type, 269 * returns the result of integer truncation (i.e., rounded towards zero). 270 * 271 * @tparam _ToDur The result type must be a `duration`. 272 * @param __d A duration. 273 * @return The value of `__d` converted to type `_ToDur`. 274 * @since C++11 275 */ 276 template<typename _ToDur, typename _Rep, typename _Period> 277 _GLIBCXX_NODISCARD 278 constexpr __enable_if_is_duration<_ToDur> 279 duration_cast(const duration<_Rep, _Period>& __d) 280 { 281 #if __cpp_inline_variables && __cpp_if_constexpr 282 if constexpr (is_same_v<_ToDur, duration<_Rep, _Period>>) 283 return __d; 284 else 285 { 286 #endif 287 using __to_period = typename _ToDur::period; 288 using __to_rep = typename _ToDur::rep; 289 using __cf = ratio_divide<_Period, __to_period>; 290 using __cr = typename common_type<__to_rep, _Rep, intmax_t>::type; 291 using __dc = __duration_cast_impl<_ToDur, __cf, __cr, 292 __cf::num == 1, __cf::den == 1>; 293 return __dc::__cast(__d); 294 #if __cpp_inline_variables && __cpp_if_constexpr 295 } 296 #endif 297 } 298 299 /** Trait indicating whether to treat a type as a floating-point type. 300 * 301 * The chrono library uses this trait to tell whether a `duration` can 302 * represent fractional values of the given precision, or only integral 303 * values. 304 * 305 * You should specialize this trait for your own numeric types that are 306 * used with `duration` and can represent non-integral values. 307 * 308 * @since C++11 309 */ 310 template<typename _Rep> 311 struct treat_as_floating_point 312 : is_floating_point<_Rep> 313 { }; 314 315 #if __cplusplus > 201402L 316 template <typename _Rep> 317 inline constexpr bool treat_as_floating_point_v = 318 treat_as_floating_point<_Rep>::value; 319 320 template<> 321 inline constexpr bool treat_as_floating_point_v<int> = false; 322 template<> 323 inline constexpr bool treat_as_floating_point_v<long> = false; 324 template<> 325 inline constexpr bool treat_as_floating_point_v<long long> = false; 326 template<> 327 inline constexpr bool treat_as_floating_point_v<float> = true; 328 template<> 329 inline constexpr bool treat_as_floating_point_v<double> = true; 330 template<> 331 inline constexpr bool treat_as_floating_point_v<long double> = true; 332 #endif // C++17 333 334 #if __cplusplus > 201703L 335 #if __cpp_lib_concepts 336 template<typename _Tp> 337 inline constexpr bool is_clock_v = false; 338 339 template<typename _Tp> 340 requires requires { 341 typename _Tp::rep; 342 typename _Tp::period; 343 typename _Tp::duration; 344 typename _Tp::time_point::clock; 345 typename _Tp::time_point::duration; 346 { &_Tp::is_steady } -> same_as<const bool*>; 347 { _Tp::now() } -> same_as<typename _Tp::time_point>; 348 requires same_as<typename _Tp::duration, 349 duration<typename _Tp::rep, typename _Tp::period>>; 350 requires same_as<typename _Tp::time_point::duration, 351 typename _Tp::duration>; 352 } 353 inline constexpr bool is_clock_v<_Tp> = true; 354 #else 355 template<typename _Tp, typename = void> 356 inline constexpr bool is_clock_v = false; 357 358 template<typename _Tp> 359 inline constexpr bool 360 is_clock_v<_Tp, void_t<typename _Tp::rep, typename _Tp::period, 361 typename _Tp::duration, 362 typename _Tp::time_point::duration, 363 decltype(_Tp::is_steady), 364 decltype(_Tp::now())>> 365 = __and_v<is_same<typename _Tp::duration, 366 duration<typename _Tp::rep, typename _Tp::period>>, 367 is_same<typename _Tp::time_point::duration, 368 typename _Tp::duration>, 369 is_same<decltype(&_Tp::is_steady), const bool*>, 370 is_same<decltype(_Tp::now()), typename _Tp::time_point>>; 371 #endif 372 373 template<typename _Tp> 374 struct is_clock 375 : bool_constant<is_clock_v<_Tp>> 376 { }; 377 #endif // C++20 378 379 #if __cplusplus >= 201703L // C++ >= 17 380 /** Convert a `duration` to type `ToDur` and round down. 381 * 382 * If the duration cannot be represented exactly in the result type, 383 * returns the closest value that is less than the argument. 384 * 385 * @tparam _ToDur The result type must be a `duration`. 386 * @param __d A duration. 387 * @return The value of `__d` converted to type `_ToDur`. 388 * @since C++17 389 */ 390 template<typename _ToDur, typename _Rep, typename _Period> 391 [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> 392 floor(const duration<_Rep, _Period>& __d) 393 { 394 auto __to = chrono::duration_cast<_ToDur>(__d); 395 if (__to > __d) 396 return __to - _ToDur{1}; 397 return __to; 398 } 399 400 /** Convert a `duration` to type `ToDur` and round up. 401 * 402 * If the duration cannot be represented exactly in the result type, 403 * returns the closest value that is greater than the argument. 404 * 405 * @tparam _ToDur The result type must be a `duration`. 406 * @param __d A duration. 407 * @return The value of `__d` converted to type `_ToDur`. 408 * @since C++17 409 */ 410 template<typename _ToDur, typename _Rep, typename _Period> 411 [[nodiscard]] constexpr __enable_if_is_duration<_ToDur> 412 ceil(const duration<_Rep, _Period>& __d) 413 { 414 auto __to = chrono::duration_cast<_ToDur>(__d); 415 if (__to < __d) 416 return __to + _ToDur{1}; 417 return __to; 418 } 419 420 /** Convert a `duration` to type `ToDur` and round to the closest value. 421 * 422 * If the duration cannot be represented exactly in the result type, 423 * returns the closest value, rounding ties to even. 424 * 425 * @tparam _ToDur The result type must be a `duration` with a 426 * non-floating-point `rep` type. 427 * @param __d A duration. 428 * @return The value of `__d` converted to type `_ToDur`. 429 * @since C++17 430 */ 431 template <typename _ToDur, typename _Rep, typename _Period> 432 [[nodiscard]] constexpr 433 enable_if_t< 434 __and_<__is_duration<_ToDur>, 435 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value, 436 _ToDur> 437 round(const duration<_Rep, _Period>& __d) 438 { 439 _ToDur __t0 = chrono::floor<_ToDur>(__d); 440 _ToDur __t1 = __t0 + _ToDur{1}; 441 auto __diff0 = __d - __t0; 442 auto __diff1 = __t1 - __d; 443 if (__diff0 == __diff1) 444 { 445 if (__t0.count() & 1) 446 return __t1; 447 return __t0; 448 } 449 else if (__diff0 < __diff1) 450 return __t0; 451 return __t1; 452 } 453 454 /** The absolute (non-negative) value of a duration. 455 * 456 * @param __d A duration with a signed `rep` type. 457 * @return A duration of the same type as the argument, with value |d|. 458 * @since C++17 459 */ 460 template<typename _Rep, typename _Period> 461 [[nodiscard]] constexpr 462 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>> 463 abs(duration<_Rep, _Period> __d) 464 { 465 if (__d >= __d.zero()) 466 return __d; 467 return -__d; 468 } 469 470 // Make chrono::ceil<D> also usable as chrono::__detail::ceil<D>. 471 namespace __detail { using chrono::ceil; } 472 473 #else // ! __glibcxx_chrono 474 475 // We want to use ceil even when compiling for earlier standards versions. 476 // C++11 only allows a single statement in a constexpr function, so we 477 // need to move the comparison into a separate function, __ceil_impl. 478 namespace __detail 479 { 480 template<typename _Tp, typename _Up> 481 constexpr _Tp 482 __ceil_impl(const _Tp& __t, const _Up& __u) 483 { 484 return (__t < __u) ? (__t + _Tp{1}) : __t; 485 } 486 487 // C++11-friendly version of std::chrono::ceil<D> for internal use. 488 template<typename _ToDur, typename _Rep, typename _Period> 489 constexpr _ToDur 490 ceil(const duration<_Rep, _Period>& __d) 491 { 492 return __detail::__ceil_impl(chrono::duration_cast<_ToDur>(__d), __d); 493 } 494 } 495 #endif // __glibcxx_chrono 496 497 /// duration_values 498 template<typename _Rep> 499 struct duration_values 500 { 501 static constexpr _Rep 502 zero() noexcept 503 { return _Rep(0); } 504 505 static constexpr _Rep 506 max() noexcept 507 { return numeric_limits<_Rep>::max(); } 508 509 static constexpr _Rep 510 min() noexcept 511 { return numeric_limits<_Rep>::lowest(); } 512 }; 513 514 template<typename _Rep, typename _Period> 515 class duration 516 { 517 static_assert(!__is_duration<_Rep>::value, 518 "rep cannot be a std::chrono::duration"); 519 static_assert(__is_ratio<_Period>::value, 520 "period must be a specialization of std::ratio"); 521 static_assert(_Period::num > 0, "period must be positive"); 522 523 template<typename _Rep2> 524 using __is_float = treat_as_floating_point<_Rep2>; 525 526 static constexpr intmax_t 527 _S_gcd(intmax_t __m, intmax_t __n) noexcept 528 { 529 // Duration only allows positive periods so we don't need to 530 // handle negative values here (unlike __static_gcd and std::gcd). 531 #if __cplusplus >= 201402L 532 do 533 { 534 intmax_t __rem = __m % __n; 535 __m = __n; 536 __n = __rem; 537 } 538 while (__n != 0); 539 return __m; 540 #else 541 // C++11 doesn't allow loops in constexpr functions, but this 542 // recursive version can be more expensive to evaluate. 543 return (__n == 0) ? __m : _S_gcd(__n, __m % __n); 544 #endif 545 } 546 547 // _GLIBCXX_RESOLVE_LIB_DEFECTS 548 // 2094. overflow shouldn't participate in overload resolution 549 // 3090. What is [2094] intended to mean? 550 // This only produces a valid type if no overflow occurs. 551 template<typename _R1, typename _R2, 552 intmax_t __gcd1 = _S_gcd(_R1::num, _R2::num), 553 intmax_t __gcd2 = _S_gcd(_R1::den, _R2::den)> 554 using __divide = ratio<(_R1::num / __gcd1) * (_R2::den / __gcd2), 555 (_R1::den / __gcd2) * (_R2::num / __gcd1)>; 556 557 // _Period2 is an exact multiple of _Period 558 template<typename _Period2> 559 using __is_harmonic 560 = __bool_constant<__divide<_Period2, _Period>::den == 1>; 561 562 public: 563 564 using rep = _Rep; 565 using period = typename _Period::type; 566 567 // 20.11.5.1 construction / copy / destroy 568 constexpr duration() = default; 569 570 duration(const duration&) = default; 571 572 // _GLIBCXX_RESOLVE_LIB_DEFECTS 573 // 3050. Conversion specification problem in chrono::duration 574 template<typename _Rep2, typename = _Require< 575 is_convertible<const _Rep2&, rep>, 576 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>> 577 constexpr explicit duration(const _Rep2& __rep) 578 : __r(static_cast<rep>(__rep)) { } 579 580 template<typename _Rep2, typename _Period2, typename = _Require< 581 is_convertible<const _Rep2&, rep>, 582 __or_<__is_float<rep>, 583 __and_<__is_harmonic<_Period2>, 584 __not_<__is_float<_Rep2>>>>>> 585 constexpr duration(const duration<_Rep2, _Period2>& __d) 586 : __r(duration_cast<duration>(__d).count()) { } 587 588 ~duration() = default; 589 duration& operator=(const duration&) = default; 590 591 // 20.11.5.2 observer 592 constexpr rep 593 count() const 594 { return __r; } 595 596 // 20.11.5.3 arithmetic 597 598 constexpr duration<typename common_type<rep>::type, period> 599 operator+() const 600 { return duration<typename common_type<rep>::type, period>(__r); } 601 602 constexpr duration<typename common_type<rep>::type, period> 603 operator-() const 604 { return duration<typename common_type<rep>::type, period>(-__r); } 605 606 _GLIBCXX17_CONSTEXPR duration& 607 operator++() 608 { 609 ++__r; 610 return *this; 611 } 612 613 _GLIBCXX17_CONSTEXPR duration 614 operator++(int) 615 { return duration(__r++); } 616 617 _GLIBCXX17_CONSTEXPR duration& 618 operator--() 619 { 620 --__r; 621 return *this; 622 } 623 624 _GLIBCXX17_CONSTEXPR duration 625 operator--(int) 626 { return duration(__r--); } 627 628 _GLIBCXX17_CONSTEXPR duration& 629 operator+=(const duration& __d) 630 { 631 __r += __d.count(); 632 return *this; 633 } 634 635 _GLIBCXX17_CONSTEXPR duration& 636 operator-=(const duration& __d) 637 { 638 __r -= __d.count(); 639 return *this; 640 } 641 642 _GLIBCXX17_CONSTEXPR duration& 643 operator*=(const rep& __rhs) 644 { 645 __r *= __rhs; 646 return *this; 647 } 648 649 _GLIBCXX17_CONSTEXPR duration& 650 operator/=(const rep& __rhs) 651 { 652 __r /= __rhs; 653 return *this; 654 } 655 656 // DR 934. 657 template<typename _Rep2 = rep> 658 _GLIBCXX17_CONSTEXPR 659 __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&> 660 operator%=(const rep& __rhs) 661 { 662 __r %= __rhs; 663 return *this; 664 } 665 666 template<typename _Rep2 = rep> 667 _GLIBCXX17_CONSTEXPR 668 __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&> 669 operator%=(const duration& __d) 670 { 671 __r %= __d.count(); 672 return *this; 673 } 674 675 // 20.11.5.4 special values 676 static constexpr duration 677 zero() noexcept 678 { return duration(duration_values<rep>::zero()); } 679 680 static constexpr duration 681 min() noexcept 682 { return duration(duration_values<rep>::min()); } 683 684 static constexpr duration 685 max() noexcept 686 { return duration(duration_values<rep>::max()); } 687 688 private: 689 rep __r; 690 }; 691 692 /// @{ 693 /// @relates std::chrono::duration 694 695 /// The sum of two durations. 696 template<typename _Rep1, typename _Period1, 697 typename _Rep2, typename _Period2> 698 constexpr typename common_type<duration<_Rep1, _Period1>, 699 duration<_Rep2, _Period2>>::type 700 operator+(const duration<_Rep1, _Period1>& __lhs, 701 const duration<_Rep2, _Period2>& __rhs) 702 { 703 typedef duration<_Rep1, _Period1> __dur1; 704 typedef duration<_Rep2, _Period2> __dur2; 705 typedef typename common_type<__dur1,__dur2>::type __cd; 706 return __cd(__cd(__lhs).count() + __cd(__rhs).count()); 707 } 708 709 /// The difference between two durations. 710 template<typename _Rep1, typename _Period1, 711 typename _Rep2, typename _Period2> 712 constexpr typename common_type<duration<_Rep1, _Period1>, 713 duration<_Rep2, _Period2>>::type 714 operator-(const duration<_Rep1, _Period1>& __lhs, 715 const duration<_Rep2, _Period2>& __rhs) 716 { 717 typedef duration<_Rep1, _Period1> __dur1; 718 typedef duration<_Rep2, _Period2> __dur2; 719 typedef typename common_type<__dur1,__dur2>::type __cd; 720 return __cd(__cd(__lhs).count() - __cd(__rhs).count()); 721 } 722 723 /// @} 724 725 /// @cond undocumented 726 727 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2 728 // is implicitly convertible to it. 729 // _GLIBCXX_RESOLVE_LIB_DEFECTS 730 // 3050. Conversion specification problem in chrono::duration constructor 731 template<typename _Rep1, typename _Rep2, 732 typename _CRep = typename common_type<_Rep1, _Rep2>::type> 733 using __common_rep_t = typename 734 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type; 735 736 /// @endcond 737 738 /** @{ 739 * Arithmetic operators for chrono::duration 740 * @relates std::chrono::duration 741 */ 742 743 template<typename _Rep1, typename _Period, typename _Rep2> 744 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period> 745 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 746 { 747 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 748 __cd; 749 return __cd(__cd(__d).count() * __s); 750 } 751 752 template<typename _Rep1, typename _Rep2, typename _Period> 753 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period> 754 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) 755 { return __d * __s; } 756 757 template<typename _Rep1, typename _Period, typename _Rep2> 758 constexpr 759 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> 760 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 761 { 762 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 763 __cd; 764 return __cd(__cd(__d).count() / __s); 765 } 766 767 template<typename _Rep1, typename _Period1, 768 typename _Rep2, typename _Period2> 769 constexpr typename common_type<_Rep1, _Rep2>::type 770 operator/(const duration<_Rep1, _Period1>& __lhs, 771 const duration<_Rep2, _Period2>& __rhs) 772 { 773 typedef duration<_Rep1, _Period1> __dur1; 774 typedef duration<_Rep2, _Period2> __dur2; 775 typedef typename common_type<__dur1,__dur2>::type __cd; 776 return __cd(__lhs).count() / __cd(__rhs).count(); 777 } 778 779 // DR 934. 780 template<typename _Rep1, typename _Period, typename _Rep2> 781 constexpr 782 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period> 783 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 784 { 785 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 786 __cd; 787 return __cd(__cd(__d).count() % __s); 788 } 789 790 template<typename _Rep1, typename _Period1, 791 typename _Rep2, typename _Period2> 792 constexpr typename common_type<duration<_Rep1, _Period1>, 793 duration<_Rep2, _Period2>>::type 794 operator%(const duration<_Rep1, _Period1>& __lhs, 795 const duration<_Rep2, _Period2>& __rhs) 796 { 797 typedef duration<_Rep1, _Period1> __dur1; 798 typedef duration<_Rep2, _Period2> __dur2; 799 typedef typename common_type<__dur1,__dur2>::type __cd; 800 return __cd(__cd(__lhs).count() % __cd(__rhs).count()); 801 } 802 /// @} 803 804 // comparisons 805 806 /** @{ 807 * Comparisons for chrono::duration 808 * @relates std::chrono::duration 809 */ 810 811 template<typename _Rep1, typename _Period1, 812 typename _Rep2, typename _Period2> 813 constexpr bool 814 operator==(const duration<_Rep1, _Period1>& __lhs, 815 const duration<_Rep2, _Period2>& __rhs) 816 { 817 typedef duration<_Rep1, _Period1> __dur1; 818 typedef duration<_Rep2, _Period2> __dur2; 819 typedef typename common_type<__dur1,__dur2>::type __ct; 820 return __ct(__lhs).count() == __ct(__rhs).count(); 821 } 822 823 template<typename _Rep1, typename _Period1, 824 typename _Rep2, typename _Period2> 825 constexpr bool 826 operator<(const duration<_Rep1, _Period1>& __lhs, 827 const duration<_Rep2, _Period2>& __rhs) 828 { 829 typedef duration<_Rep1, _Period1> __dur1; 830 typedef duration<_Rep2, _Period2> __dur2; 831 typedef typename common_type<__dur1,__dur2>::type __ct; 832 return __ct(__lhs).count() < __ct(__rhs).count(); 833 } 834 835 #if __cpp_lib_three_way_comparison 836 template<typename _Rep1, typename _Period1, 837 typename _Rep2, typename _Period2> 838 requires three_way_comparable<common_type_t<_Rep1, _Rep2>> 839 constexpr auto 840 operator<=>(const duration<_Rep1, _Period1>& __lhs, 841 const duration<_Rep2, _Period2>& __rhs) 842 { 843 using __ct = common_type_t<duration<_Rep1, _Period1>, 844 duration<_Rep2, _Period2>>; 845 return __ct(__lhs).count() <=> __ct(__rhs).count(); 846 } 847 #else 848 template<typename _Rep1, typename _Period1, 849 typename _Rep2, typename _Period2> 850 constexpr bool 851 operator!=(const duration<_Rep1, _Period1>& __lhs, 852 const duration<_Rep2, _Period2>& __rhs) 853 { return !(__lhs == __rhs); } 854 #endif 855 856 template<typename _Rep1, typename _Period1, 857 typename _Rep2, typename _Period2> 858 constexpr bool 859 operator<=(const duration<_Rep1, _Period1>& __lhs, 860 const duration<_Rep2, _Period2>& __rhs) 861 { return !(__rhs < __lhs); } 862 863 template<typename _Rep1, typename _Period1, 864 typename _Rep2, typename _Period2> 865 constexpr bool 866 operator>(const duration<_Rep1, _Period1>& __lhs, 867 const duration<_Rep2, _Period2>& __rhs) 868 { return __rhs < __lhs; } 869 870 template<typename _Rep1, typename _Period1, 871 typename _Rep2, typename _Period2> 872 constexpr bool 873 operator>=(const duration<_Rep1, _Period1>& __lhs, 874 const duration<_Rep2, _Period2>& __rhs) 875 { return !(__lhs < __rhs); } 876 877 /// @} 878 879 /// @cond undocumented 880 #ifdef _GLIBCXX_USE_C99_STDINT 881 # define _GLIBCXX_CHRONO_INT64_T int64_t 882 #elif defined __INT64_TYPE__ 883 # define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__ 884 #else 885 static_assert(std::numeric_limits<unsigned long long>::digits >= 64, 886 "Representation type for nanoseconds must have at least 64 bits"); 887 # define _GLIBCXX_CHRONO_INT64_T long long 888 #endif 889 /// @endcond 890 891 /// nanoseconds 892 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>; 893 894 /// microseconds 895 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>; 896 897 /// milliseconds 898 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>; 899 900 /// seconds 901 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>; 902 903 /// minutes 904 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>; 905 906 /// hours 907 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>; 908 909 #if __cplusplus > 201703L 910 /// days 911 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>; 912 913 /// weeks 914 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>; 915 916 /// years 917 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>; 918 919 /// months 920 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>; 921 #endif // C++20 922 923 #undef _GLIBCXX_CHRONO_INT64_T 924 925 template<typename _Clock, typename _Dur> 926 class time_point 927 { 928 static_assert(__is_duration<_Dur>::value, 929 "duration must be a specialization of std::chrono::duration"); 930 931 public: 932 typedef _Clock clock; 933 typedef _Dur duration; 934 typedef typename duration::rep rep; 935 typedef typename duration::period period; 936 937 constexpr time_point() : __d(duration::zero()) 938 { } 939 940 constexpr explicit time_point(const duration& __dur) 941 : __d(__dur) 942 { } 943 944 // conversions 945 template<typename _Dur2, 946 typename = _Require<is_convertible<_Dur2, _Dur>>> 947 constexpr time_point(const time_point<clock, _Dur2>& __t) 948 : __d(__t.time_since_epoch()) 949 { } 950 951 // observer 952 constexpr duration 953 time_since_epoch() const 954 { return __d; } 955 956 #if __cplusplus > 201703L 957 constexpr time_point& 958 operator++() 959 { 960 ++__d; 961 return *this; 962 } 963 964 constexpr time_point 965 operator++(int) 966 { return time_point{__d++}; } 967 968 constexpr time_point& 969 operator--() 970 { 971 --__d; 972 return *this; 973 } 974 975 constexpr time_point 976 operator--(int) 977 { return time_point{__d--}; } 978 #endif 979 980 // arithmetic 981 _GLIBCXX17_CONSTEXPR time_point& 982 operator+=(const duration& __dur) 983 { 984 __d += __dur; 985 return *this; 986 } 987 988 _GLIBCXX17_CONSTEXPR time_point& 989 operator-=(const duration& __dur) 990 { 991 __d -= __dur; 992 return *this; 993 } 994 995 // special values 996 static constexpr time_point 997 min() noexcept 998 { return time_point(duration::min()); } 999 1000 static constexpr time_point 1001 max() noexcept 1002 { return time_point(duration::max()); } 1003 1004 private: 1005 duration __d; 1006 }; 1007 1008 /** Convert a `time_point` to use `duration` type `ToDur`. 1009 * 1010 * The result is the same time point as measured by the same clock, but 1011 * using the specified `duration` to represent the time. 1012 * If the time point cannot be represented accurately in the result type, 1013 * returns the result of integer truncation (i.e., rounded towards zero). 1014 * 1015 * @tparam _ToDur The `duration` type to use for the result. 1016 * @param __t A time point. 1017 * @return The value of `__t` converted to use type `_ToDur`. 1018 * @since C++11 1019 */ 1020 template<typename _ToDur, typename _Clock, typename _Dur> 1021 _GLIBCXX_NODISCARD constexpr 1022 __enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>> 1023 time_point_cast(const time_point<_Clock, _Dur>& __t) 1024 { 1025 typedef time_point<_Clock, _ToDur> __time_point; 1026 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch())); 1027 } 1028 1029 #if __cplusplus > 201402L 1030 /** Convert a `time_point` to type `ToDur` and round down. 1031 * 1032 * The result is the same time point as measured by the same clock, but 1033 * using the specified `duration` to represent the time. 1034 * If the time point cannot be represented exactly in the result type, 1035 * returns the closest value that is less than the argument. 1036 * 1037 * @tparam _ToDur The `duration` type to use for the result. 1038 * @param __t A time point. 1039 * @return The value of `__d` converted to type `_ToDur`. 1040 * @since C++17 1041 */ 1042 template<typename _ToDur, typename _Clock, typename _Dur> 1043 [[nodiscard]] constexpr 1044 enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> 1045 floor(const time_point<_Clock, _Dur>& __tp) 1046 { 1047 return time_point<_Clock, _ToDur>{ 1048 chrono::floor<_ToDur>(__tp.time_since_epoch())}; 1049 } 1050 1051 /** Convert a `time_point` to type `ToDur` and round up. 1052 * 1053 * The result is the same time point as measured by the same clock, but 1054 * using the specified `duration` to represent the time. 1055 * If the time point cannot be represented exactly in the result type, 1056 * returns the closest value that is greater than the argument. 1057 * 1058 * @tparam _ToDur The `duration` type to use for the result. 1059 * @param __t A time point. 1060 * @return The value of `__d` converted to type `_ToDur`. 1061 * @since C++17 1062 */ 1063 template<typename _ToDur, typename _Clock, typename _Dur> 1064 [[nodiscard]] constexpr 1065 enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>> 1066 ceil(const time_point<_Clock, _Dur>& __tp) 1067 { 1068 return time_point<_Clock, _ToDur>{ 1069 chrono::ceil<_ToDur>(__tp.time_since_epoch())}; 1070 } 1071 1072 /** Convert a `time_point` to type `ToDur` and round to the closest value. 1073 * 1074 * The result is the same time point as measured by the same clock, but 1075 * using the specified `duration` to represent the time. 1076 * If the time point cannot be represented exactly in the result type, 1077 * returns the closest value, rounding ties to even. 1078 * 1079 * @tparam _ToDur The `duration` type to use for the result, 1080 * which must have a non-floating-point `rep` type. 1081 * @param __t A time point. 1082 * @return The value of `__d` converted to type `_ToDur`. 1083 * @since C++17 1084 */ 1085 template<typename _ToDur, typename _Clock, typename _Dur> 1086 [[nodiscard]] constexpr 1087 enable_if_t<__is_duration_v<_ToDur> 1088 && !treat_as_floating_point_v<typename _ToDur::rep>, 1089 time_point<_Clock, _ToDur>> 1090 round(const time_point<_Clock, _Dur>& __tp) 1091 { 1092 return time_point<_Clock, _ToDur>{ 1093 chrono::round<_ToDur>(__tp.time_since_epoch())}; 1094 } 1095 #endif // C++17 1096 1097 /// @{ 1098 /// @relates time_point 1099 1100 /// Adjust a time point forwards by the given duration. 1101 template<typename _Clock, typename _Dur1, 1102 typename _Rep2, typename _Period2> 1103 constexpr time_point<_Clock, 1104 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 1105 operator+(const time_point<_Clock, _Dur1>& __lhs, 1106 const duration<_Rep2, _Period2>& __rhs) 1107 { 1108 typedef duration<_Rep2, _Period2> __dur2; 1109 typedef typename common_type<_Dur1,__dur2>::type __ct; 1110 typedef time_point<_Clock, __ct> __time_point; 1111 return __time_point(__lhs.time_since_epoch() + __rhs); 1112 } 1113 1114 /// Adjust a time point forwards by the given duration. 1115 template<typename _Rep1, typename _Period1, 1116 typename _Clock, typename _Dur2> 1117 constexpr time_point<_Clock, 1118 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type> 1119 operator+(const duration<_Rep1, _Period1>& __lhs, 1120 const time_point<_Clock, _Dur2>& __rhs) 1121 { 1122 typedef duration<_Rep1, _Period1> __dur1; 1123 typedef typename common_type<__dur1,_Dur2>::type __ct; 1124 typedef time_point<_Clock, __ct> __time_point; 1125 return __time_point(__rhs.time_since_epoch() + __lhs); 1126 } 1127 1128 /// Adjust a time point backwards by the given duration. 1129 template<typename _Clock, typename _Dur1, 1130 typename _Rep2, typename _Period2> 1131 constexpr time_point<_Clock, 1132 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 1133 operator-(const time_point<_Clock, _Dur1>& __lhs, 1134 const duration<_Rep2, _Period2>& __rhs) 1135 { 1136 typedef duration<_Rep2, _Period2> __dur2; 1137 typedef typename common_type<_Dur1,__dur2>::type __ct; 1138 typedef time_point<_Clock, __ct> __time_point; 1139 return __time_point(__lhs.time_since_epoch() -__rhs); 1140 } 1141 1142 /// The difference between two time points (as a duration) 1143 template<typename _Clock, typename _Dur1, typename _Dur2> 1144 constexpr typename common_type<_Dur1, _Dur2>::type 1145 operator-(const time_point<_Clock, _Dur1>& __lhs, 1146 const time_point<_Clock, _Dur2>& __rhs) 1147 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); } 1148 /// @} 1149 1150 /** @{ 1151 * Comparisons for time_point 1152 * @relates chrono::time_point 1153 */ 1154 1155 template<typename _Clock, typename _Dur1, typename _Dur2> 1156 constexpr bool 1157 operator==(const time_point<_Clock, _Dur1>& __lhs, 1158 const time_point<_Clock, _Dur2>& __rhs) 1159 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } 1160 1161 #if __cpp_lib_three_way_comparison 1162 template<typename _Clock, typename _Dur1, 1163 three_way_comparable_with<_Dur1> _Dur2> 1164 constexpr auto 1165 operator<=>(const time_point<_Clock, _Dur1>& __lhs, 1166 const time_point<_Clock, _Dur2>& __rhs) 1167 { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); } 1168 #else 1169 template<typename _Clock, typename _Dur1, typename _Dur2> 1170 constexpr bool 1171 operator!=(const time_point<_Clock, _Dur1>& __lhs, 1172 const time_point<_Clock, _Dur2>& __rhs) 1173 { return !(__lhs == __rhs); } 1174 #endif 1175 1176 template<typename _Clock, typename _Dur1, typename _Dur2> 1177 constexpr bool 1178 operator<(const time_point<_Clock, _Dur1>& __lhs, 1179 const time_point<_Clock, _Dur2>& __rhs) 1180 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); } 1181 1182 template<typename _Clock, typename _Dur1, typename _Dur2> 1183 constexpr bool 1184 operator<=(const time_point<_Clock, _Dur1>& __lhs, 1185 const time_point<_Clock, _Dur2>& __rhs) 1186 { return !(__rhs < __lhs); } 1187 1188 template<typename _Clock, typename _Dur1, typename _Dur2> 1189 constexpr bool 1190 operator>(const time_point<_Clock, _Dur1>& __lhs, 1191 const time_point<_Clock, _Dur2>& __rhs) 1192 { return __rhs < __lhs; } 1193 1194 template<typename _Clock, typename _Dur1, typename _Dur2> 1195 constexpr bool 1196 operator>=(const time_point<_Clock, _Dur1>& __lhs, 1197 const time_point<_Clock, _Dur2>& __rhs) 1198 { return !(__lhs < __rhs); } 1199 1200 /// @} 1201 /// @} group chrono 1202 1203 #if _GLIBCXX_HOSTED 1204 // Clocks. 1205 1206 // Why nanosecond resolution as the default? 1207 // Why have std::system_clock always count in the highest 1208 // resolution (ie nanoseconds), even if on some OSes the low 3 1209 // or 9 decimal digits will be always zero? This allows later 1210 // implementations to change the system_clock::now() 1211 // implementation any time to provide better resolution without 1212 // changing function signature or units. 1213 1214 // To support the (forward) evolution of the library's defined 1215 // clocks, wrap inside inline namespace so that the current 1216 // defintions of system_clock, steady_clock, and 1217 // high_resolution_clock types are uniquely mangled. This way, new 1218 // code can use the latests clocks, while the library can contain 1219 // compatibility definitions for previous versions. At some 1220 // point, when these clocks settle down, the inlined namespaces 1221 // can be removed. XXX GLIBCXX_ABI Deprecated 1222 _GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2) 1223 1224 /** 1225 * @brief System clock. 1226 * 1227 * Time returned represents wall time from the system-wide clock. 1228 * @ingroup chrono 1229 */ 1230 struct system_clock 1231 { 1232 typedef chrono::nanoseconds duration; 1233 typedef duration::rep rep; 1234 typedef duration::period period; 1235 typedef chrono::time_point<system_clock, duration> time_point; 1236 1237 static_assert(system_clock::duration::min() 1238 < system_clock::duration::zero(), 1239 "a clock's minimum duration cannot be less than its epoch"); 1240 1241 static constexpr bool is_steady = false; 1242 1243 static time_point 1244 now() noexcept; 1245 1246 // Map to C API 1247 [[__gnu__::__always_inline__]] 1248 static std::time_t 1249 to_time_t(const time_point& __t) noexcept 1250 { 1251 return std::time_t(duration_cast<chrono::seconds> 1252 (__t.time_since_epoch()).count()); 1253 } 1254 1255 [[__gnu__::__always_inline__]] 1256 static time_point 1257 from_time_t(std::time_t __t) noexcept 1258 { 1259 typedef chrono::time_point<system_clock, seconds> __from; 1260 return time_point_cast<system_clock::duration> 1261 (__from(chrono::seconds(__t))); 1262 } 1263 }; 1264 1265 1266 /** 1267 * @brief Monotonic clock 1268 * 1269 * Time returned has the property of only increasing at a uniform rate. 1270 * @ingroup chrono 1271 */ 1272 struct steady_clock 1273 { 1274 typedef chrono::nanoseconds duration; 1275 typedef duration::rep rep; 1276 typedef duration::period period; 1277 typedef chrono::time_point<steady_clock, duration> time_point; 1278 1279 static constexpr bool is_steady = true; 1280 1281 static time_point 1282 now() noexcept; 1283 }; 1284 1285 1286 /** 1287 * @brief Highest-resolution clock 1288 * 1289 * This is the clock "with the shortest tick period." Alias to 1290 * std::system_clock until higher-than-nanosecond definitions 1291 * become feasible. 1292 * @ingroup chrono 1293 */ 1294 using high_resolution_clock = system_clock; 1295 1296 _GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2) 1297 1298 #if __cplusplus >= 202002L 1299 /// @addtogroup chrono 1300 /// @{ 1301 template<typename _Duration> 1302 using sys_time = time_point<system_clock, _Duration>; 1303 using sys_seconds = sys_time<seconds>; 1304 using sys_days = sys_time<days>; 1305 1306 using file_clock = ::std::filesystem::__file_clock; 1307 1308 template<typename _Duration> 1309 using file_time = time_point<file_clock, _Duration>; 1310 1311 template<> struct is_clock<system_clock> : true_type { }; 1312 template<> struct is_clock<steady_clock> : true_type { }; 1313 template<> struct is_clock<file_clock> : true_type { }; 1314 1315 template<> inline constexpr bool is_clock_v<system_clock> = true; 1316 template<> inline constexpr bool is_clock_v<steady_clock> = true; 1317 template<> inline constexpr bool is_clock_v<file_clock> = true; 1318 /// @} 1319 #endif // C++20 1320 #elif __cplusplus >= 202002L 1321 // Define a fake clock like chrono::local_t so that sys_time etc. 1322 // can be used for freestanding. 1323 struct __sys_t; 1324 template<typename _Duration> 1325 using sys_time = time_point<__sys_t, _Duration>; 1326 using sys_seconds = sys_time<seconds>; 1327 using sys_days = sys_time<days>; 1328 #endif // _GLIBCXX_HOSTED 1329 } // namespace chrono 1330 1331 #ifdef __glibcxx_chrono_udls // C++ >= 14 1332 inline namespace literals 1333 { 1334 /** ISO C++ 2014 namespace for suffixes for duration literals. 1335 * 1336 * These suffixes can be used to create `chrono::duration` values with 1337 * tick periods of hours, minutes, seconds, milliseconds, microseconds 1338 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written 1339 * as `5s` after making the suffix visible in the current scope. 1340 * The suffixes can be made visible by a using-directive or 1341 * using-declaration such as: 1342 * - `using namespace std::chrono_literals;` 1343 * - `using namespace std::literals;` 1344 * - `using namespace std::chrono;` 1345 * - `using namespace std;` 1346 * - `using std::chrono_literals::operator""s;` 1347 * 1348 * The result of these suffixes on an integer literal is one of the 1349 * standard typedefs such as `std::chrono::hours`. 1350 * The result on a floating-point literal is a duration type with the 1351 * specified tick period and an unspecified floating-point representation, 1352 * for example `1.5e2ms` might be equivalent to 1353 * `chrono::duration<long double, chrono::milli>(1.5e2)`. 1354 * 1355 * @since C+14 1356 * @ingroup chrono 1357 */ 1358 inline namespace chrono_literals 1359 { 1360 /// @addtogroup chrono 1361 /// @{ 1362 1363 #pragma GCC diagnostic push 1364 #pragma GCC diagnostic ignored "-Wliteral-suffix" 1365 /// @cond undocumented 1366 template<typename _Dur, char... _Digits> 1367 constexpr _Dur __check_overflow() 1368 { 1369 using _Val = __parse_int::_Parse_int<_Digits...>; 1370 constexpr typename _Dur::rep __repval = _Val::value; 1371 static_assert(__repval >= 0 && __repval == _Val::value, 1372 "literal value cannot be represented by duration type"); 1373 return _Dur(__repval); 1374 } 1375 /// @endcond 1376 1377 /// Literal suffix for durations representing non-integer hours 1378 constexpr chrono::duration<long double, ratio<3600,1>> 1379 operator""h(long double __hours) 1380 { return chrono::duration<long double, ratio<3600,1>>{__hours}; } 1381 1382 /// Literal suffix for durations of type `std::chrono::hours` 1383 template <char... _Digits> 1384 constexpr chrono::hours 1385 operator""h() 1386 { return __check_overflow<chrono::hours, _Digits...>(); } 1387 1388 /// Literal suffix for durations representing non-integer minutes 1389 constexpr chrono::duration<long double, ratio<60,1>> 1390 operator""min(long double __mins) 1391 { return chrono::duration<long double, ratio<60,1>>{__mins}; } 1392 1393 /// Literal suffix for durations of type `std::chrono::minutes` 1394 template <char... _Digits> 1395 constexpr chrono::minutes 1396 operator""min() 1397 { return __check_overflow<chrono::minutes, _Digits...>(); } 1398 1399 /// Literal suffix for durations representing non-integer seconds 1400 constexpr chrono::duration<long double> 1401 operator""s(long double __secs) 1402 { return chrono::duration<long double>{__secs}; } 1403 1404 /// Literal suffix for durations of type `std::chrono::seconds` 1405 template <char... _Digits> 1406 constexpr chrono::seconds 1407 operator""s() 1408 { return __check_overflow<chrono::seconds, _Digits...>(); } 1409 1410 /// Literal suffix for durations representing non-integer milliseconds 1411 constexpr chrono::duration<long double, milli> 1412 operator""ms(long double __msecs) 1413 { return chrono::duration<long double, milli>{__msecs}; } 1414 1415 /// Literal suffix for durations of type `std::chrono::milliseconds` 1416 template <char... _Digits> 1417 constexpr chrono::milliseconds 1418 operator""ms() 1419 { return __check_overflow<chrono::milliseconds, _Digits...>(); } 1420 1421 /// Literal suffix for durations representing non-integer microseconds 1422 constexpr chrono::duration<long double, micro> 1423 operator""us(long double __usecs) 1424 { return chrono::duration<long double, micro>{__usecs}; } 1425 1426 /// Literal suffix for durations of type `std::chrono::microseconds` 1427 template <char... _Digits> 1428 constexpr chrono::microseconds 1429 operator""us() 1430 { return __check_overflow<chrono::microseconds, _Digits...>(); } 1431 1432 /// Literal suffix for durations representing non-integer nanoseconds 1433 constexpr chrono::duration<long double, nano> 1434 operator""ns(long double __nsecs) 1435 { return chrono::duration<long double, nano>{__nsecs}; } 1436 1437 /// Literal suffix for durations of type `std::chrono::nanoseconds` 1438 template <char... _Digits> 1439 constexpr chrono::nanoseconds 1440 operator""ns() 1441 { return __check_overflow<chrono::nanoseconds, _Digits...>(); } 1442 1443 #pragma GCC diagnostic pop 1444 /// @} 1445 } // inline namespace chrono_literals 1446 } // inline namespace literals 1447 1448 namespace chrono 1449 { 1450 using namespace literals::chrono_literals; 1451 } // namespace chrono 1452 #endif // __glibcxx_chrono_udls 1453 1454 #if __cplusplus >= 201703L && _GLIBCXX_HOSTED 1455 namespace filesystem 1456 { 1457 struct __file_clock 1458 { 1459 using duration = chrono::nanoseconds; 1460 using rep = duration::rep; 1461 using period = duration::period; 1462 using time_point = chrono::time_point<__file_clock>; 1463 static constexpr bool is_steady = false; 1464 1465 static time_point 1466 now() noexcept 1467 { return _S_from_sys(chrono::system_clock::now()); } 1468 1469 #if __cplusplus > 201703L 1470 template<typename _Dur> 1471 static 1472 chrono::file_time<common_type_t<_Dur, chrono::seconds>> 1473 from_sys(const chrono::sys_time<_Dur>& __t) noexcept 1474 { return _S_from_sys(__t); } 1475 1476 // For internal use only 1477 template<typename _Dur> 1478 static 1479 chrono::sys_time<common_type_t<_Dur, chrono::seconds>> 1480 to_sys(const chrono::file_time<_Dur>& __t) noexcept 1481 { return _S_to_sys(__t); } 1482 #endif // C++20 1483 1484 private: 1485 using __sys_clock = chrono::system_clock; 1486 1487 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC. 1488 // A signed 64-bit duration with nanosecond resolution gives roughly 1489 // +/- 292 years, which covers the 1901-2446 date range for ext4. 1490 static constexpr chrono::seconds _S_epoch_diff{6437664000}; 1491 1492 protected: 1493 // For internal use only 1494 template<typename _Dur> 1495 static 1496 chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>> 1497 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept 1498 { 1499 using _CDur = common_type_t<_Dur, chrono::seconds>; 1500 using __file_time = chrono::time_point<__file_clock, _CDur>; 1501 return __file_time{__t.time_since_epoch()} - _S_epoch_diff; 1502 } 1503 1504 // For internal use only 1505 template<typename _Dur> 1506 static 1507 chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>> 1508 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept 1509 { 1510 using _CDur = common_type_t<_Dur, chrono::seconds>; 1511 using __sys_time = chrono::time_point<__sys_clock, _CDur>; 1512 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff; 1513 } 1514 }; 1515 } // namespace filesystem 1516 #endif // C++17 && HOSTED 1517 1518 _GLIBCXX_END_NAMESPACE_VERSION 1519 } // namespace std 1520 1521 #endif // C++11 1522 1523 #endif //_GLIBCXX_CHRONO_H