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