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