libstdc++: chrono.h Source File

libstdc++

chrono.h
Go to the documentation of this file.
1// chrono::duration and chrono::time_point -*- C++ -*-
2
3// Copyright (C) 2008-2026 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
53namespace 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 // _GLIBCXX_RESOLVE_LIB_DEFECTS
518 // 4481. Disallow chrono::duration<const T, P>
519 static_assert(is_same<_Rep, __remove_cvref_t<_Rep>>::value,
520 "rep should be cv-unqualified object type");
521 static_assert(!__is_duration<_Rep>::value,
522 "rep cannot be a std::chrono::duration");
523 static_assert(__is_ratio<_Period>::value,
524 "period must be a specialization of std::ratio");
525 static_assert(_Period::num > 0, "period must be positive");
526
527 template<typename _Rep2>
528 using __is_float = treat_as_floating_point<_Rep2>;
529
530 static constexpr intmax_t
531 _S_gcd(intmax_t __m, intmax_t __n) noexcept
532 {
533 // Duration only allows positive periods so we don't need to
534 // handle negative values here (unlike __static_gcd and std::gcd).
535#if __cplusplus >= 201402L
536 do
537 {
538 intmax_t __rem = __m % __n;
539 __m = __n;
540 __n = __rem;
541 }
542 while (__n != 0);
543 return __m;
544#else
545 // C++11 doesn't allow loops in constexpr functions, but this
546 // recursive version can be more expensive to evaluate.
547 return (__n == 0) ? __m : _S_gcd(__n, __m % __n);
548#endif
549 }
550
551 // _GLIBCXX_RESOLVE_LIB_DEFECTS
552 // 2094. overflow shouldn't participate in overload resolution
553 // 3090. What is [2094] intended to mean?
554 // This only produces a valid type if no overflow occurs.
555 template<typename _R1, typename _R2,
556 intmax_t __gcd1 = _S_gcd(_R1::num, _R2::num),
557 intmax_t __gcd2 = _S_gcd(_R1::den, _R2::den)>
558 using __divide = ratio<(_R1::num / __gcd1) * (_R2::den / __gcd2),
559 (_R1::den / __gcd2) * (_R2::num / __gcd1)>;
560
561 // _Period2 is an exact multiple of _Period
562 template<typename _Period2>
563 using __is_harmonic
564 = __bool_constant<__divide<_Period2, _Period>::den == 1>;
565
566 public:
567
568 using rep = _Rep;
569 using period = typename _Period::type;
570
571 // 20.11.5.1 construction / copy / destroy
572 constexpr duration() = default;
573
574 duration(const duration&) = default;
575
576 // _GLIBCXX_RESOLVE_LIB_DEFECTS
577 // 3050. Conversion specification problem in chrono::duration
578 template<typename _Rep2, typename = _Require<
579 is_convertible<const _Rep2&, rep>,
580 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
581 constexpr explicit duration(const _Rep2& __rep)
582 : __r(static_cast<rep>(__rep)) { }
583
584 template<typename _Rep2, typename _Period2, typename = _Require<
585 is_convertible<const _Rep2&, rep>,
586 __or_<__is_float<rep>,
587 __and_<__is_harmonic<_Period2>,
588 __not_<__is_float<_Rep2>>>>>>
589 constexpr duration(const duration<_Rep2, _Period2>& __d)
590 : __r(duration_cast<duration>(__d).count()) { }
591
592 ~duration() = default;
593 duration& operator=(const duration&) = default;
594
595 // 20.11.5.2 observer
596 constexpr rep
597 count() const
598 { return __r; }
599
600 // 20.11.5.3 arithmetic
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 constexpr duration<typename common_type<rep>::type, period>
607 operator-() const
608 { return duration<typename common_type<rep>::type, period>(-__r); }
609
610 _GLIBCXX17_CONSTEXPR duration&
611 operator++()
612 {
613 ++__r;
614 return *this;
615 }
616
617 _GLIBCXX17_CONSTEXPR duration
618 operator++(int)
619 { return duration(__r++); }
620
621 _GLIBCXX17_CONSTEXPR duration&
622 operator--()
623 {
624 --__r;
625 return *this;
626 }
627
628 _GLIBCXX17_CONSTEXPR duration
629 operator--(int)
630 { return duration(__r--); }
631
632 _GLIBCXX17_CONSTEXPR duration&
633 operator+=(const duration& __d)
634 {
635 __r += __d.count();
636 return *this;
637 }
638
639 _GLIBCXX17_CONSTEXPR duration&
640 operator-=(const duration& __d)
641 {
642 __r -= __d.count();
643 return *this;
644 }
645
646 _GLIBCXX17_CONSTEXPR duration&
647 operator*=(const rep& __rhs)
648 {
649 __r *= __rhs;
650 return *this;
651 }
652
653 _GLIBCXX17_CONSTEXPR duration&
654 operator/=(const rep& __rhs)
655 {
656 __r /= __rhs;
657 return *this;
658 }
659
660 // DR 934.
661 template<typename _Rep2 = rep>
662 _GLIBCXX17_CONSTEXPR
663 __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&>
664 operator%=(const rep& __rhs)
665 {
666 __r %= __rhs;
667 return *this;
668 }
669
670 template<typename _Rep2 = rep>
671 _GLIBCXX17_CONSTEXPR
672 __enable_if_t<!treat_as_floating_point<_Rep2>::value, duration&>
673 operator%=(const duration& __d)
674 {
675 __r %= __d.count();
676 return *this;
677 }
678
679 // 20.11.5.4 special values
680 static constexpr duration
681 zero() noexcept
682 { return duration(duration_values<rep>::zero()); }
683
684 static constexpr duration
685 min() noexcept
686 { return duration(duration_values<rep>::min()); }
687
688 static constexpr duration
689 max() noexcept
690 { return duration(duration_values<rep>::max()); }
691
692 private:
693 rep __r;
694 };
695
696 /// @{
697 /// @relates std::chrono::duration
698
699 /// The sum of two durations.
700 template<typename _Rep1, typename _Period1,
701 typename _Rep2, typename _Period2>
702 constexpr typename common_type<duration<_Rep1, _Period1>,
703 duration<_Rep2, _Period2>>::type
704 operator+(const duration<_Rep1, _Period1>& __lhs,
705 const duration<_Rep2, _Period2>& __rhs)
706 {
707 typedef duration<_Rep1, _Period1> __dur1;
708 typedef duration<_Rep2, _Period2> __dur2;
709 typedef typename common_type<__dur1,__dur2>::type __cd;
710 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
711 }
712
713 /// The difference between two durations.
714 template<typename _Rep1, typename _Period1,
715 typename _Rep2, typename _Period2>
716 constexpr typename common_type<duration<_Rep1, _Period1>,
717 duration<_Rep2, _Period2>>::type
718 operator-(const duration<_Rep1, _Period1>& __lhs,
719 const duration<_Rep2, _Period2>& __rhs)
720 {
721 typedef duration<_Rep1, _Period1> __dur1;
722 typedef duration<_Rep2, _Period2> __dur2;
723 typedef typename common_type<__dur1,__dur2>::type __cd;
724 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
725 }
726
727 /// @}
728
729 /// @cond undocumented
730
731 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
732 // is implicitly convertible to it.
733 // _GLIBCXX_RESOLVE_LIB_DEFECTS
734 // 3050. Conversion specification problem in chrono::duration constructor
735 template<typename _Rep1, typename _Rep2,
736 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
737 using __common_rep_t = typename
738 enable_if<is_convertible<const _Rep2&, _CRep>::value, _CRep>::type;
739
740 /// @endcond
741
742 /** @{
743 * Arithmetic operators for chrono::duration
744 * @relates std::chrono::duration
745 */
746
747 template<typename _Rep1, typename _Period, typename _Rep2>
748 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
749 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
750 {
751 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
752 __cd;
753 return __cd(__cd(__d).count() * __s);
754 }
755
756 template<typename _Rep1, typename _Rep2, typename _Period>
757 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
758 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
759 { return __d * __s; }
760
761 template<typename _Rep1, typename _Period, typename _Rep2>
762 constexpr
763 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
764 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
765 {
766 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
767 __cd;
768 return __cd(__cd(__d).count() / __s);
769 }
770
771 template<typename _Rep1, typename _Period1,
772 typename _Rep2, typename _Period2>
773 constexpr typename common_type<_Rep1, _Rep2>::type
774 operator/(const duration<_Rep1, _Period1>& __lhs,
775 const duration<_Rep2, _Period2>& __rhs)
776 {
777 typedef duration<_Rep1, _Period1> __dur1;
778 typedef duration<_Rep2, _Period2> __dur2;
779 typedef typename common_type<__dur1,__dur2>::type __cd;
780 return __cd(__lhs).count() / __cd(__rhs).count();
781 }
782
783 // DR 934.
784 template<typename _Rep1, typename _Period, typename _Rep2>
785 constexpr
786 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
787 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
788 {
789 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
790 __cd;
791 return __cd(__cd(__d).count() % __s);
792 }
793
794 template<typename _Rep1, typename _Period1,
795 typename _Rep2, typename _Period2>
796 constexpr typename common_type<duration<_Rep1, _Period1>,
797 duration<_Rep2, _Period2>>::type
798 operator%(const duration<_Rep1, _Period1>& __lhs,
799 const duration<_Rep2, _Period2>& __rhs)
800 {
801 typedef duration<_Rep1, _Period1> __dur1;
802 typedef duration<_Rep2, _Period2> __dur2;
803 typedef typename common_type<__dur1,__dur2>::type __cd;
804 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
805 }
806 /// @}
807
808 // comparisons
809
810 /** @{
811 * Comparisons for chrono::duration
812 * @relates std::chrono::duration
813 */
814
815 template<typename _Rep1, typename _Period1,
816 typename _Rep2, typename _Period2>
817 constexpr bool
818 operator==(const duration<_Rep1, _Period1>& __lhs,
819 const duration<_Rep2, _Period2>& __rhs)
820 {
821 typedef duration<_Rep1, _Period1> __dur1;
822 typedef duration<_Rep2, _Period2> __dur2;
823 typedef typename common_type<__dur1,__dur2>::type __ct;
824 return __ct(__lhs).count() == __ct(__rhs).count();
825 }
826
827 template<typename _Rep1, typename _Period1,
828 typename _Rep2, typename _Period2>
829 constexpr bool
830 operator<(const duration<_Rep1, _Period1>& __lhs,
831 const duration<_Rep2, _Period2>& __rhs)
832 {
833 typedef duration<_Rep1, _Period1> __dur1;
834 typedef duration<_Rep2, _Period2> __dur2;
835 typedef typename common_type<__dur1,__dur2>::type __ct;
836 return __ct(__lhs).count() < __ct(__rhs).count();
837 }
838
839#if __cpp_lib_three_way_comparison
840 template<typename _Rep1, typename _Period1,
841 typename _Rep2, typename _Period2>
842 requires three_way_comparable<common_type_t<_Rep1, _Rep2>>
843 constexpr auto
844 operator<=>(const duration<_Rep1, _Period1>& __lhs,
845 const duration<_Rep2, _Period2>& __rhs)
846 {
847 using __ct = common_type_t<duration<_Rep1, _Period1>,
848 duration<_Rep2, _Period2>>;
849 return __ct(__lhs).count() <=> __ct(__rhs).count();
850 }
851#else
852 template<typename _Rep1, typename _Period1,
853 typename _Rep2, typename _Period2>
854 constexpr bool
855 operator!=(const duration<_Rep1, _Period1>& __lhs,
856 const duration<_Rep2, _Period2>& __rhs)
857 { return !(__lhs == __rhs); }
858#endif
859
860 template<typename _Rep1, typename _Period1,
861 typename _Rep2, typename _Period2>
862 constexpr bool
863 operator<=(const duration<_Rep1, _Period1>& __lhs,
864 const duration<_Rep2, _Period2>& __rhs)
865 { return !(__rhs < __lhs); }
866
867 template<typename _Rep1, typename _Period1,
868 typename _Rep2, typename _Period2>
869 constexpr bool
870 operator>(const duration<_Rep1, _Period1>& __lhs,
871 const duration<_Rep2, _Period2>& __rhs)
872 { return __rhs < __lhs; }
873
874 template<typename _Rep1, typename _Period1,
875 typename _Rep2, typename _Period2>
876 constexpr bool
877 operator>=(const duration<_Rep1, _Period1>& __lhs,
878 const duration<_Rep2, _Period2>& __rhs)
879 { return !(__lhs < __rhs); }
880
881 /// @}
882
883 /// @cond undocumented
884#ifdef _GLIBCXX_USE_C99_STDINT
885# define _GLIBCXX_CHRONO_INT64_T int64_t
886#elif defined __INT64_TYPE__
887# define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
888#else
889 static_assert(std::numeric_limits<unsigned long long>::digits >= 64,
890 "Representation type for nanoseconds must have at least 64 bits");
891# define _GLIBCXX_CHRONO_INT64_T long long
892#endif
893 /// @endcond
894
895 /// nanoseconds
896 using nanoseconds = duration<_GLIBCXX_CHRONO_INT64_T, nano>;
897
898 /// microseconds
899 using microseconds = duration<_GLIBCXX_CHRONO_INT64_T, micro>;
900
901 /// milliseconds
902 using milliseconds = duration<_GLIBCXX_CHRONO_INT64_T, milli>;
903
904 /// seconds
905 using seconds = duration<_GLIBCXX_CHRONO_INT64_T>;
906
907 /// minutes
908 using minutes = duration<_GLIBCXX_CHRONO_INT64_T, ratio< 60>>;
909
910 /// hours
911 using hours = duration<_GLIBCXX_CHRONO_INT64_T, ratio<3600>>;
912
913#if __cplusplus > 201703L
914 /// days
915 using days = duration<_GLIBCXX_CHRONO_INT64_T, ratio<86400>>;
916
917 /// weeks
918 using weeks = duration<_GLIBCXX_CHRONO_INT64_T, ratio<604800>>;
919
920 /// years
921 using years = duration<_GLIBCXX_CHRONO_INT64_T, ratio<31556952>>;
922
923 /// months
924 using months = duration<_GLIBCXX_CHRONO_INT64_T, ratio<2629746>>;
925#endif // C++20
926
927#undef _GLIBCXX_CHRONO_INT64_T
928
929 template<typename _Clock, typename _Dur>
930 class time_point
931 {
932 static_assert(__is_duration<_Dur>::value,
933 "duration must be a specialization of std::chrono::duration");
934
935 public:
936 typedef _Clock clock;
937 typedef _Dur duration;
938 typedef typename duration::rep rep;
939 typedef typename duration::period period;
940
941 constexpr time_point() : __d(duration::zero())
942 { }
943
944 constexpr explicit time_point(const duration& __dur)
945 : __d(__dur)
946 { }
947
948 // conversions
949 template<typename _Dur2,
950 typename = _Require<is_convertible<_Dur2, _Dur>>>
951 constexpr time_point(const time_point<clock, _Dur2>& __t)
952 : __d(__t.time_since_epoch())
953 { }
954
955 // observer
956 constexpr duration
957 time_since_epoch() const
958 { return __d; }
959
960#if __cplusplus > 201703L
961 constexpr time_point&
962 operator++()
963 {
964 ++__d;
965 return *this;
966 }
967
968 constexpr time_point
969 operator++(int)
970 { return time_point{__d++}; }
971
972 constexpr time_point&
973 operator--()
974 {
975 --__d;
976 return *this;
977 }
978
979 constexpr time_point
980 operator--(int)
981 { return time_point{__d--}; }
982#endif
983
984 // arithmetic
985 _GLIBCXX17_CONSTEXPR time_point&
986 operator+=(const duration& __dur)
987 {
988 __d += __dur;
989 return *this;
990 }
991
992 _GLIBCXX17_CONSTEXPR time_point&
993 operator-=(const duration& __dur)
994 {
995 __d -= __dur;
996 return *this;
997 }
998
999 // special values
1000 static constexpr time_point
1001 min() noexcept
1002 { return time_point(duration::min()); }
1003
1004 static constexpr time_point
1005 max() noexcept
1006 { return time_point(duration::max()); }
1007
1008 private:
1009 duration __d;
1010 };
1011
1012 /** Convert a `time_point` to use `duration` type `ToDur`.
1013 *
1014 * The result is the same time point as measured by the same clock, but
1015 * using the specified `duration` to represent the time.
1016 * If the time point cannot be represented accurately in the result type,
1017 * returns the result of integer truncation (i.e., rounded towards zero).
1018 *
1019 * @tparam _ToDur The `duration` type to use for the result.
1020 * @param __t A time point.
1021 * @return The value of `__t` converted to use type `_ToDur`.
1022 * @since C++11
1023 */
1024 template<typename _ToDur, typename _Clock, typename _Dur>
1025 _GLIBCXX_NODISCARD constexpr
1026 __enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
1027 time_point_cast(const time_point<_Clock, _Dur>& __t)
1028 {
1029 typedef time_point<_Clock, _ToDur> __time_point;
1030 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
1031 }
1032
1033#if __cplusplus > 201402L
1034 /** Convert a `time_point` to type `ToDur` and round down.
1035 *
1036 * The result is the same time point as measured by the same clock, but
1037 * using the specified `duration` to represent the time.
1038 * If the time point cannot be represented exactly in the result type,
1039 * returns the closest value that is less than the argument.
1040 *
1041 * @tparam _ToDur The `duration` type to use for the result.
1042 * @param __tp A time point.
1043 * @return The value of `__tp` rounded down to the precision of `_ToDur`.
1044 * @since C++17
1045 */
1046 template<typename _ToDur, typename _Clock, typename _Dur>
1047 [[nodiscard]] constexpr
1048 enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>>
1049 floor(const time_point<_Clock, _Dur>& __tp)
1050 {
1051 return time_point<_Clock, _ToDur>{
1052 chrono::floor<_ToDur>(__tp.time_since_epoch())};
1053 }
1054
1055 /** Convert a `time_point` to type `ToDur` and round up.
1056 *
1057 * The result is the same time point as measured by the same clock, but
1058 * using the specified `duration` to represent the time.
1059 * If the time point cannot be represented exactly in the result type,
1060 * returns the closest value that is greater than the argument.
1061 *
1062 * @tparam _ToDur The `duration` type to use for the result.
1063 * @param __tp A time point.
1064 * @return The value of `__tp` rounded up to the precision of `_ToDur`.
1065 * @since C++17
1066 */
1067 template<typename _ToDur, typename _Clock, typename _Dur>
1068 [[nodiscard]] constexpr
1069 enable_if_t<__is_duration_v<_ToDur>, time_point<_Clock, _ToDur>>
1070 ceil(const time_point<_Clock, _Dur>& __tp)
1071 {
1072 return time_point<_Clock, _ToDur>{
1073 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
1074 }
1075
1076 /** Convert a `time_point` to type `ToDur` and round to the closest value.
1077 *
1078 * The result is the same time point as measured by the same clock, but
1079 * using the specified `duration` to represent the time.
1080 * If the time point cannot be represented exactly in the result type,
1081 * returns the closest value, rounding ties to even.
1082 *
1083 * @tparam _ToDur The `duration` type to use for the result,
1084 * which must have a non-floating-point `rep` type.
1085 * @param __tp A time point.
1086 * @return The value of `__tp` rounded to the precision `_ToDur`.
1087 * @since C++17
1088 */
1089 template<typename _ToDur, typename _Clock, typename _Dur>
1090 [[nodiscard]] constexpr
1091 enable_if_t<__is_duration_v<_ToDur>
1092 && !treat_as_floating_point_v<typename _ToDur::rep>,
1093 time_point<_Clock, _ToDur>>
1094 round(const time_point<_Clock, _Dur>& __tp)
1095 {
1096 return time_point<_Clock, _ToDur>{
1097 chrono::round<_ToDur>(__tp.time_since_epoch())};
1098 }
1099#endif // C++17
1100
1101 /// @{
1102 /// @relates time_point
1103
1104 /// Adjust a time point forwards by the given duration.
1105 template<typename _Clock, typename _Dur1,
1106 typename _Rep2, typename _Period2>
1107 constexpr time_point<_Clock,
1108 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
1109 operator+(const time_point<_Clock, _Dur1>& __lhs,
1110 const duration<_Rep2, _Period2>& __rhs)
1111 {
1112 typedef duration<_Rep2, _Period2> __dur2;
1113 typedef typename common_type<_Dur1,__dur2>::type __ct;
1114 typedef time_point<_Clock, __ct> __time_point;
1115 return __time_point(__lhs.time_since_epoch() + __rhs);
1116 }
1117
1118 /// Adjust a time point forwards by the given duration.
1119 template<typename _Rep1, typename _Period1,
1120 typename _Clock, typename _Dur2>
1121 constexpr time_point<_Clock,
1122 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
1123 operator+(const duration<_Rep1, _Period1>& __lhs,
1124 const time_point<_Clock, _Dur2>& __rhs)
1125 {
1126 typedef duration<_Rep1, _Period1> __dur1;
1127 typedef typename common_type<__dur1,_Dur2>::type __ct;
1128 typedef time_point<_Clock, __ct> __time_point;
1129 return __time_point(__rhs.time_since_epoch() + __lhs);
1130 }
1131
1132 /// Adjust a time point backwards by the given duration.
1133 template<typename _Clock, typename _Dur1,
1134 typename _Rep2, typename _Period2>
1135 constexpr time_point<_Clock,
1136 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
1137 operator-(const time_point<_Clock, _Dur1>& __lhs,
1138 const duration<_Rep2, _Period2>& __rhs)
1139 {
1140 typedef duration<_Rep2, _Period2> __dur2;
1141 typedef typename common_type<_Dur1,__dur2>::type __ct;
1142 typedef time_point<_Clock, __ct> __time_point;
1143 return __time_point(__lhs.time_since_epoch() -__rhs);
1144 }
1145
1146 /// The difference between two time points (as a duration)
1147 template<typename _Clock, typename _Dur1, typename _Dur2>
1148 constexpr typename common_type<_Dur1, _Dur2>::type
1149 operator-(const time_point<_Clock, _Dur1>& __lhs,
1150 const time_point<_Clock, _Dur2>& __rhs)
1151 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
1152 /// @}
1153
1154 /** @{
1155 * Comparisons for time_point
1156 * @relates chrono::time_point
1157 */
1158
1159 template<typename _Clock, typename _Dur1, typename _Dur2>
1160 constexpr bool
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
1165#if __cpp_lib_three_way_comparison
1166 template<typename _Clock, typename _Dur1,
1167 three_way_comparable_with<_Dur1> _Dur2>
1168 constexpr auto
1169 operator<=>(const time_point<_Clock, _Dur1>& __lhs,
1170 const time_point<_Clock, _Dur2>& __rhs)
1171 { return __lhs.time_since_epoch() <=> __rhs.time_since_epoch(); }
1172#else
1173 template<typename _Clock, typename _Dur1, typename _Dur2>
1174 constexpr bool
1175 operator!=(const time_point<_Clock, _Dur1>& __lhs,
1176 const time_point<_Clock, _Dur2>& __rhs)
1177 { return !(__lhs == __rhs); }
1178#endif
1179
1180 template<typename _Clock, typename _Dur1, typename _Dur2>
1181 constexpr bool
1182 operator<(const time_point<_Clock, _Dur1>& __lhs,
1183 const time_point<_Clock, _Dur2>& __rhs)
1184 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
1185
1186 template<typename _Clock, typename _Dur1, typename _Dur2>
1187 constexpr bool
1188 operator<=(const time_point<_Clock, _Dur1>& __lhs,
1189 const time_point<_Clock, _Dur2>& __rhs)
1190 { return !(__rhs < __lhs); }
1191
1192 template<typename _Clock, typename _Dur1, typename _Dur2>
1193 constexpr bool
1194 operator>(const time_point<_Clock, _Dur1>& __lhs,
1195 const time_point<_Clock, _Dur2>& __rhs)
1196 { return __rhs < __lhs; }
1197
1198 template<typename _Clock, typename _Dur1, typename _Dur2>
1199 constexpr bool
1200 operator>=(const time_point<_Clock, _Dur1>& __lhs,
1201 const time_point<_Clock, _Dur2>& __rhs)
1202 { return !(__lhs < __rhs); }
1203
1204 /// @}
1205 /// @} group chrono
1206
1207#if _GLIBCXX_HOSTED
1208 // Clocks.
1209
1210 // Why nanosecond resolution as the default?
1211 // Why have std::system_clock always count in the highest
1212 // resolution (ie nanoseconds), even if on some OSes the low 3
1213 // or 9 decimal digits will be always zero? This allows later
1214 // implementations to change the system_clock::now()
1215 // implementation any time to provide better resolution without
1216 // changing function signature or units.
1217
1218 // To support the (forward) evolution of the library's defined
1219 // clocks, wrap inside inline namespace so that the current
1220 // definitions of system_clock, steady_clock, and
1221 // high_resolution_clock types are uniquely mangled. This way, new
1222 // code can use the latests clocks, while the library can contain
1223 // compatibility definitions for previous versions. At some
1224 // point, when these clocks settle down, the inlined namespaces
1225 // can be removed. XXX GLIBCXX_ABI Deprecated
1226_GLIBCXX_BEGIN_INLINE_ABI_NAMESPACE(_V2)
1227
1228 /**
1229 * @brief System clock.
1230 *
1231 * Time returned represents wall time from the system-wide clock.
1232 * @ingroup chrono
1233 */
1234 struct system_clock
1235 {
1236 typedef chrono::nanoseconds duration;
1237 typedef duration::rep rep;
1238 typedef duration::period period;
1239 typedef chrono::time_point<system_clock, duration> time_point;
1240
1241 static_assert(system_clock::duration::min()
1242 < system_clock::duration::zero(),
1243 "a clock's minimum duration cannot be less than its epoch");
1244
1245 static constexpr bool is_steady = false;
1246
1247 static time_point
1248 now() noexcept;
1249
1250 // Map to C API
1251 [[__gnu__::__always_inline__]]
1252 static std::time_t
1253 to_time_t(const time_point& __t) noexcept
1254 {
1255 return std::time_t(duration_cast<chrono::seconds>
1256 (__t.time_since_epoch()).count());
1257 }
1258
1259 [[__gnu__::__always_inline__]]
1260 static time_point
1261 from_time_t(std::time_t __t) noexcept
1262 {
1263 typedef chrono::time_point<system_clock, seconds> __from;
1264 return time_point_cast<system_clock::duration>
1265 (__from(chrono::seconds(__t)));
1266 }
1267 };
1268
1269
1270 /**
1271 * @brief Monotonic clock
1272 *
1273 * Time returned has the property of only increasing at a uniform rate.
1274 * @ingroup chrono
1275 */
1276 struct steady_clock
1277 {
1278 typedef chrono::nanoseconds duration;
1279 typedef duration::rep rep;
1280 typedef duration::period period;
1281 typedef chrono::time_point<steady_clock, duration> time_point;
1282
1283 static constexpr bool is_steady = true;
1284
1285 static time_point
1286 now() noexcept;
1287 };
1288
1289
1290 /**
1291 * @brief Highest-resolution clock
1292 *
1293 * This is the clock "with the shortest tick period." Alias to
1294 * std::system_clock until higher-than-nanosecond definitions
1295 * become feasible.
1296 * @ingroup chrono
1297 */
1298 using high_resolution_clock = system_clock;
1299
1300_GLIBCXX_END_INLINE_ABI_NAMESPACE(_V2)
1301
1302#if __cplusplus >= 202002L
1303 /// @addtogroup chrono
1304 /// @{
1305 template<typename _Duration>
1306 using sys_time = time_point<system_clock, _Duration>;
1307 using sys_seconds = sys_time<seconds>;
1308 using sys_days = sys_time<days>;
1309
1310 using file_clock = ::std::filesystem::__file_clock;
1311
1312 template<typename _Duration>
1313 using file_time = time_point<file_clock, _Duration>;
1314
1315 template<> struct is_clock<system_clock> : true_type { };
1316 template<> struct is_clock<steady_clock> : true_type { };
1317 template<> struct is_clock<file_clock> : true_type { };
1318
1319 template<> inline constexpr bool is_clock_v<system_clock> = true;
1320 template<> inline constexpr bool is_clock_v<steady_clock> = true;
1321 template<> inline constexpr bool is_clock_v<file_clock> = true;
1322 /// @}
1323#endif // C++20
1324#elif __cplusplus >= 202002L
1325 // Define a fake clock like chrono::local_t so that sys_time etc.
1326 // can be used for freestanding.
1327 struct __sys_t;
1328 template<typename _Duration>
1329 using sys_time = time_point<__sys_t, _Duration>;
1330 using sys_seconds = sys_time<seconds>;
1331 using sys_days = sys_time<days>;
1332#endif // _GLIBCXX_HOSTED
1333 } // namespace chrono
1334
1335#ifdef __glibcxx_chrono_udls // C++ >= 14
1336 inline namespace literals
1337 {
1338 /** ISO C++ 2014 namespace for suffixes for duration literals.
1339 *
1340 * These suffixes can be used to create `chrono::duration` values with
1341 * tick periods of hours, minutes, seconds, milliseconds, microseconds
1342 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
1343 * as `5s` after making the suffix visible in the current scope.
1344 * The suffixes can be made visible by a using-directive or
1345 * using-declaration such as:
1346 * - `using namespace std::chrono_literals;`
1347 * - `using namespace std::literals;`
1348 * - `using namespace std::chrono;`
1349 * - `using namespace std;`
1350 * - `using std::chrono_literals::operator""s;`
1351 *
1352 * The result of these suffixes on an integer literal is one of the
1353 * standard typedefs such as `std::chrono::hours`.
1354 * The result on a floating-point literal is a duration type with the
1355 * specified tick period and an unspecified floating-point representation,
1356 * for example `1.5e2ms` might be equivalent to
1357 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
1358 *
1359 * @since C++14
1360 * @ingroup chrono
1361 */
1362 inline namespace chrono_literals
1363 {
1364 /// @addtogroup chrono
1365 /// @{
1366
1367#pragma GCC diagnostic push
1368#pragma GCC diagnostic ignored "-Wliteral-suffix"
1369 /// @cond undocumented
1370 template<typename _Dur, char... _Digits>
1371 constexpr _Dur __check_overflow()
1372 {
1373 using _Val = __parse_int::_Parse_int<_Digits...>;
1374 constexpr typename _Dur::rep __repval = _Val::value;
1375 static_assert(__repval >= 0 && __repval == _Val::value,
1376 "literal value cannot be represented by duration type");
1377 return _Dur(__repval);
1378 }
1379 /// @endcond
1380
1381 /// Literal suffix for durations representing non-integer hours
1382 constexpr chrono::duration<long double, ratio<3600,1>>
1383 operator""h(long double __hours)
1384 { return chrono::duration<long double, ratio<3600,1>>{__hours}; }
1385
1386 /// Literal suffix for durations of type `std::chrono::hours`
1387 template <char... _Digits>
1388 constexpr chrono::hours
1389 operator""h()
1390 { return __check_overflow<chrono::hours, _Digits...>(); }
1391
1392 /// Literal suffix for durations representing non-integer minutes
1393 constexpr chrono::duration<long double, ratio<60,1>>
1394 operator""min(long double __mins)
1395 { return chrono::duration<long double, ratio<60,1>>{__mins}; }
1396
1397 /// Literal suffix for durations of type `std::chrono::minutes`
1398 template <char... _Digits>
1399 constexpr chrono::minutes
1400 operator""min()
1401 { return __check_overflow<chrono::minutes, _Digits...>(); }
1402
1403 /// Literal suffix for durations representing non-integer seconds
1404 constexpr chrono::duration<long double>
1405 operator""s(long double __secs)
1406 { return chrono::duration<long double>{__secs}; }
1407
1408 /// Literal suffix for durations of type `std::chrono::seconds`
1409 template <char... _Digits>
1410 constexpr chrono::seconds
1411 operator""s()
1412 { return __check_overflow<chrono::seconds, _Digits...>(); }
1413
1414 /// Literal suffix for durations representing non-integer milliseconds
1415 constexpr chrono::duration<long double, milli>
1416 operator""ms(long double __msecs)
1417 { return chrono::duration<long double, milli>{__msecs}; }
1418
1419 /// Literal suffix for durations of type `std::chrono::milliseconds`
1420 template <char... _Digits>
1421 constexpr chrono::milliseconds
1422 operator""ms()
1423 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1424
1425 /// Literal suffix for durations representing non-integer microseconds
1426 constexpr chrono::duration<long double, micro>
1427 operator""us(long double __usecs)
1428 { return chrono::duration<long double, micro>{__usecs}; }
1429
1430 /// Literal suffix for durations of type `std::chrono::microseconds`
1431 template <char... _Digits>
1432 constexpr chrono::microseconds
1433 operator""us()
1434 { return __check_overflow<chrono::microseconds, _Digits...>(); }
1435
1436 /// Literal suffix for durations representing non-integer nanoseconds
1437 constexpr chrono::duration<long double, nano>
1438 operator""ns(long double __nsecs)
1439 { return chrono::duration<long double, nano>{__nsecs}; }
1440
1441 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1442 template <char... _Digits>
1443 constexpr chrono::nanoseconds
1444 operator""ns()
1445 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1446
1447#pragma GCC diagnostic pop
1448 /// @}
1449 } // inline namespace chrono_literals
1450 } // inline namespace literals
1451
1452 namespace chrono
1453 {
1454 using namespace literals::chrono_literals;
1455 } // namespace chrono
1456#endif // __glibcxx_chrono_udls
1457
1458#if __cplusplus >= 201703L && _GLIBCXX_HOSTED
1459 namespace filesystem
1460 {
1461 struct __file_clock
1462 {
1463 using duration = chrono::nanoseconds;
1464 using rep = duration::rep;
1465 using period = duration::period;
1466 using time_point = chrono::time_point<__file_clock>;
1467 static constexpr bool is_steady = false;
1468
1469 static time_point
1470 now() noexcept
1471 { return _S_from_sys(chrono::system_clock::now()); }
1472
1473#if __cplusplus > 201703L
1474 template<typename _Dur>
1475 static
1476 chrono::file_time<common_type_t<_Dur, chrono::seconds>>
1477 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1478 { return _S_from_sys(__t); }
1479
1480 // For internal use only
1481 template<typename _Dur>
1482 static
1483 chrono::sys_time<common_type_t<_Dur, chrono::seconds>>
1484 to_sys(const chrono::file_time<_Dur>& __t) noexcept
1485 { return _S_to_sys(__t); }
1486#endif // C++20
1487
1488 private:
1489 using __sys_clock = chrono::system_clock;
1490
1491 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
1492 // A signed 64-bit duration with nanosecond resolution gives roughly
1493 // +/- 292 years, which covers the 1901-2446 date range for ext4.
1494 static constexpr chrono::seconds _S_epoch_diff{6437664000};
1495
1496 protected:
1497 // For internal use only
1498 template<typename _Dur>
1499 static
1500 chrono::time_point<__file_clock, common_type_t<_Dur, chrono::seconds>>
1501 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1502 {
1503 using _CDur = common_type_t<_Dur, chrono::seconds>;
1504 using __file_time = chrono::time_point<__file_clock, _CDur>;
1505 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1506 }
1507
1508 // For internal use only
1509 template<typename _Dur>
1510 static
1511 chrono::time_point<__sys_clock, common_type_t<_Dur, chrono::seconds>>
1512 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1513 {
1514 using _CDur = common_type_t<_Dur, chrono::seconds>;
1515 using __sys_time = chrono::time_point<__sys_clock, _CDur>;
1516 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1517 }
1518 };
1519 } // namespace filesystem
1520#endif // C++17 && HOSTED
1521
1522#if _GLIBCXX_HOSTED
1523#if ! defined _GLIBCXX_NO_SLEEP || defined _GLIBCXX_HAS_GTHREADS \
1524 || _GLIBCXX_HAVE_LINUX_FUTEX
1525namespace chrono
1526{
1527/// @cond undocumented
1528
1529#pragma GCC diagnostic push
1530#pragma GCC diagnostic ignored "-Wc++17-extensions"
1531 // Convert a chrono::duration to a relative time represented as timespec
1532 // (e.g. for use with nanosleep).
1533 template<typename _Rep, typename _Period>
1534 [[__nodiscard__]] _GLIBCXX14_CONSTEXPR inline
1535 struct ::timespec
1536 __to_timeout_timespec(const duration<_Rep, _Period>& __d)
1537 {
1538 struct ::timespec __ts{};
1539
1540 if (__d < __d.zero()) // Negative timeouts don't make sense.
1541 return __ts;
1542
1543 if constexpr (ratio_greater<_Period, ratio<1>>::value
1544 || treat_as_floating_point<_Rep>::value)
1545 {
1546 // Converting from e.g. chrono::hours::max() to chrono::seconds
1547 // would evaluate LLONG_MAX * 3600 which would overflow.
1548 // Limit to chrono::seconds::max().
1549 chrono::duration<double> __fmax(chrono::seconds::max());
1550 if (__d > __fmax) [[__unlikely__]]
1551 return chrono::__to_timeout_timespec(chrono::seconds::max());
1552 }
1553
1554 auto __s = chrono::duration_cast<chrono::seconds>(__d);
1555
1556 if constexpr (is_integral<time_t>::value) // POSIX.1-2001 allows floating
1557 {
1558 // Also limit to time_t maximum (only relevant for 32-bit time_t).
1559 constexpr auto __tmax = numeric_limits<time_t>::max();
1560 if (__s.count() > __tmax) [[__unlikely__]]
1561 {
1562 __ts.tv_sec = __tmax;
1563 return __ts;
1564 }
1565 }
1566
1567 auto __ns = chrono::duration_cast<chrono::nanoseconds>(__d - __s);
1568
1569 if constexpr (treat_as_floating_point<_Rep>::value)
1570 if (__ns.count() > 999999999) [[__unlikely__]]
1571 __ns = chrono::nanoseconds(999999999);
1572
1573 __ts.tv_sec = static_cast<time_t>(__s.count());
1574 __ts.tv_nsec = static_cast<long>(__ns.count());
1575 return __ts;
1576 }
1577#pragma GCC diagnostic pop
1578
1579 // Convert a chrono::time_point to an absolute time represented as timespec.
1580 // All times before the epoch get converted to the epoch, so this assumes
1581 // that we only use it for clocks where that's true.
1582 // It should be safe to use this for system_clock and steady_clock.
1583 template<typename _Clock, typename _Dur>
1584 [[__nodiscard__]] _GLIBCXX14_CONSTEXPR inline
1585 struct ::timespec
1586 __to_timeout_timespec(const time_point<_Clock, _Dur>& __t)
1587 {
1588 return chrono::__to_timeout_timespec(__t.time_since_epoch());
1589 }
1590
1591/// @endcond
1592} // namespace chrono
1593#endif // !NO_SLEEP || HAS_GTHREADS || HAVE_LINUX_FUTEX
1594#endif // HOSTED
1595
1596_GLIBCXX_END_NAMESPACE_VERSION
1597} // namespace std
1598
1599#endif // C++11
1600
1601#endif //_GLIBCXX_CHRONO_H
compare
concepts
limits
ratio
type_traits
ctime
parse_numbers.h
version.h
std::chrono::__detail::ceil
constexpr __enable_if_is_duration< _ToDur > ceil(const duration< _Rep, _Period > &__d)
Definition chrono.h:412
std::chrono::duration::operator==
constexpr bool operator==(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:818
std::chrono::weeks
duration< int64_t, ratio< 604800 > > weeks
weeks
Definition chrono.h:918
std::chrono::floor
constexpr __enable_if_is_duration< _ToDur > floor(const duration< _Rep, _Period > &__d)
Definition chrono.h:392
std::chrono::months
duration< int64_t, ratio< 2629746 > > months
months
Definition chrono.h:924
std::chrono::round
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:437
std::chrono::days
duration< int64_t, ratio< 86400 > > days
days
Definition chrono.h:915
std::chrono::years
duration< int64_t, ratio< 31556952 > > years
years
Definition chrono.h:921
std::chrono::operator<=
constexpr bool operator<=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:863
std::chrono::hours
duration< int64_t, ratio< 3600 > > hours
hours
Definition chrono.h:911
std::chrono::operator>=
constexpr bool operator>=(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:877
std::chrono::high_resolution_clock
system_clock high_resolution_clock
Highest-resolution clock.
Definition chrono.h:1298
std::chrono::operator*
constexpr duration< __common_rep_t< _Rep2, _Rep1 >, _Period > operator*(const _Rep1 &__s, const duration< _Rep2, _Period > &__d)
Definition chrono.h:758
std::chrono::nanoseconds
duration< int64_t, nano > nanoseconds
nanoseconds
Definition chrono.h:896
std::chrono::abs
constexpr enable_if_t< numeric_limits< _Rep >::is_signed, duration< _Rep, _Period > > abs(duration< _Rep, _Period > __d)
Definition chrono.h:463
std::chrono::operator%
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator%(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition chrono.h:787
std::chrono::time_point< local_t, _Duration >::operator+
constexpr time_point< local_t, typename common_type< _Dur1, duration< _Rep2, _Period2 > >::type > operator+(const time_point< local_t, _Dur1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:1109
std::chrono::minutes
duration< int64_t, ratio< 60 > > minutes
minutes
Definition chrono.h:908
std::chrono::time_point_cast
constexpr __enable_if_t< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > > time_point_cast(const time_point< _Clock, _Dur > &__t)
Definition chrono.h:1027
std::chrono::milliseconds
duration< int64_t, milli > milliseconds
milliseconds
Definition chrono.h:902
std::chrono::microseconds
duration< int64_t, micro > microseconds
microseconds
Definition chrono.h:899
std::chrono::operator+
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:1123
std::chrono::operator<=>
constexpr auto operator<=>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:844
std::chrono::duration::operator+
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator+(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
The sum of two durations.
Definition chrono.h:704
std::chrono::operator<
constexpr bool operator<(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:830
std::chrono::seconds
duration< int64_t > seconds
seconds
Definition chrono.h:905
std::chrono::operator>
constexpr bool operator>(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition chrono.h:870
std::chrono::operator-
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:718
std::chrono::operator/
constexpr duration< __common_rep_t< _Rep1, __disable_if_is_duration< _Rep2 > >, _Period > operator/(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition chrono.h:764
std::chrono::duration::operator*
constexpr duration< __common_rep_t< _Rep1, _Rep2 >, _Period > operator*(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition chrono.h:749
std::chrono::ceil
constexpr __enable_if_is_duration< _ToDur > ceil(const duration< _Rep, _Period > &__d)
Definition chrono.h:412
std::chrono::duration_cast
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
Definition chrono.h:279
std::ratio_divide
typename __ratio_divide< _R1, _R2 >::type ratio_divide
ratio_divide
Definition ratio:392
std::true_type
__bool_constant< true > true_type
The type used as a compile-time boolean with true value.
Definition type_traits:119
std::common_type_t
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition type_traits:2977
std::enable_if_t
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition type_traits:2969
std::false_type
__bool_constant< false > false_type
The type used as a compile-time boolean with false value.
Definition type_traits:122
std::min
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition stl_algobase.h:232
std::experimental::fundamentals_v2::void_t
void void_t
A metafunction that always yields void, used for detecting valid types.
Definition experimental/type_traits:262
std
ISO C++ entities toplevel namespace is std.
std::literals
ISO C++ inline namespace for literal suffixes.
std::__detail
Implementation details not part of the namespace std interface.
std::chrono
ISO C++ 2011 namespace for date and time utilities.
std::literals::chrono_literals
Definition chrono:3669
std::filesystem
ISO C++ 2017 namespace for File System library.
Definition chrono.h:58
std::__numeric_limits_base::digits
static constexpr int digits
Definition limits:224
std::numeric_limits::max
static constexpr _Tp max() noexcept
Definition limits:336
std::numeric_limits::lowest
static constexpr _Tp lowest() noexcept
Definition limits:342
std::ratio
Provides compile-time rational arithmetic.
Definition ratio:272
std::enable_if
Define a member typedef type only if a boolean constant is true.
Definition type_traits:137
std::is_floating_point
is_floating_point
Definition type_traits:624
std::is_same
is_same
Definition type_traits:1649
std::common_type
common_type
Definition type_traits:2600
std::chrono::duration
chrono::duration represents a distance between two points in time
Definition chrono.h:516
std::chrono::time_point
chrono::time_point represents a point in time as measured by a clock
Definition chrono.h:931
std::chrono::treat_as_floating_point
Definition chrono.h:313
std::chrono::duration_values
duration_values
Definition chrono.h:500
std::chrono::system_clock
System clock.
Definition chrono.h:1235
std::chrono::steady_clock
Monotonic clock.
Definition chrono.h:1277
std::same_as
[concept.same], concept same_as
Definition concepts:65