libstdc++
chrono
Go to the documentation of this file.
1// <chrono> -*- C++ -*-
2
3// Copyright (C) 2008-2020 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/chrono
26 * This is a Standard C++ Library header.
27 * @ingroup chrono
28 */
29
30#ifndef _GLIBCXX_CHRONO
31#define _GLIBCXX_CHRONO 1
32
33#pragma GCC system_header
34
35#if __cplusplus < 201103L
36# include <bits/c++0x_warning.h>
37#else
38
39#include <ratio>
40#include <type_traits>
41#include <limits>
42#include <ctime>
43#include <bits/parse_numbers.h> // for literals support.
44#if __cplusplus > 201703L
45# include <concepts>
46#endif
47
48namespace std _GLIBCXX_VISIBILITY(default)
49{
50_GLIBCXX_BEGIN_NAMESPACE_VERSION
51
52#if __cplusplus >= 201703L
53 namespace filesystem { struct __file_clock; };
54#endif
55
56 /**
57 * @defgroup chrono Time
58 * @ingroup utilities
59 *
60 * Classes and functions for time.
61 * @{
62 */
63
64 /** @namespace std::chrono
65 * @brief ISO C++ 2011 namespace for date and time utilities
66 */
67 namespace chrono
68 {
69 template<typename _Rep, typename _Period = ratio<1>>
70 struct duration;
71
72 template<typename _Clock, typename _Dur = typename _Clock::duration>
73 struct time_point;
74 }
75
76 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly)
77
78 /// @cond undocumented
79
80 template<typename _CT, typename _Period1, typename _Period2, typename = void>
81 struct __duration_common_type
82 { };
83
84 template<typename _CT, typename _Period1, typename _Period2>
85 struct __duration_common_type<_CT, _Period1, _Period2,
86 __void_t<typename _CT::type>>
87 {
88 private:
89 using __gcd_num = __static_gcd<_Period1::num, _Period2::num>;
90 using __gcd_den = __static_gcd<_Period1::den, _Period2::den>;
91 using __cr = typename _CT::type;
92 using __r = ratio<__gcd_num::value,
93 (_Period1::den / __gcd_den::value) * _Period2::den>;
94
95 public:
96 using type = chrono::duration<__cr, __r>;
97 };
98
99 template<typename _Period1, typename _Period2>
100 struct __duration_common_type<__failure_type, _Period1, _Period2>
101 { typedef __failure_type type; };
102
103 /// @endcond
104
105 /// Specialization of common_type for chrono::duration types.
106 /// @relates duration
107 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2>
108 struct common_type<chrono::duration<_Rep1, _Period1>,
109 chrono::duration<_Rep2, _Period2>>
110 : __duration_common_type<common_type<_Rep1, _Rep2>, _Period1, _Period2>
111 { };
112
113 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly)
114
115 /// @cond undocumented
116
117 template<typename _CT, typename _Clock, typename = void>
118 struct __timepoint_common_type
119 { };
120
121 template<typename _CT, typename _Clock>
122 struct __timepoint_common_type<_CT, _Clock, __void_t<typename _CT::type>>
123 {
124 using type = chrono::time_point<_Clock, typename _CT::type>;
125 };
126
127 /// @endcond
128
129 /// Specialization of common_type for chrono::time_point types.
130 /// @relates time_point
131 template<typename _Clock, typename _Duration1, typename _Duration2>
132 struct common_type<chrono::time_point<_Clock, _Duration1>,
133 chrono::time_point<_Clock, _Duration2>>
134 : __timepoint_common_type<common_type<_Duration1, _Duration2>, _Clock>
135 { };
136
137 // @} group chrono
138
139 namespace chrono
140 {
141 /// @addtogroup chrono
142 /// @{
143
144 /// @cond undocumented
145
146 // Primary template for duration_cast impl.
147 template<typename _ToDur, typename _CF, typename _CR,
148 bool _NumIsOne = false, bool _DenIsOne = false>
149 struct __duration_cast_impl
150 {
151 template<typename _Rep, typename _Period>
152 static constexpr _ToDur
153 __cast(const duration<_Rep, _Period>& __d)
154 {
155 typedef typename _ToDur::rep __to_rep;
156 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count())
157 * static_cast<_CR>(_CF::num)
158 / static_cast<_CR>(_CF::den)));
159 }
160 };
161
162 template<typename _ToDur, typename _CF, typename _CR>
163 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true>
164 {
165 template<typename _Rep, typename _Period>
166 static constexpr _ToDur
167 __cast(const duration<_Rep, _Period>& __d)
168 {
169 typedef typename _ToDur::rep __to_rep;
170 return _ToDur(static_cast<__to_rep>(__d.count()));
171 }
172 };
173
174 template<typename _ToDur, typename _CF, typename _CR>
175 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false>
176 {
177 template<typename _Rep, typename _Period>
178 static constexpr _ToDur
179 __cast(const duration<_Rep, _Period>& __d)
180 {
181 typedef typename _ToDur::rep __to_rep;
182 return _ToDur(static_cast<__to_rep>(
183 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den)));
184 }
185 };
186
187 template<typename _ToDur, typename _CF, typename _CR>
188 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true>
189 {
190 template<typename _Rep, typename _Period>
191 static constexpr _ToDur
192 __cast(const duration<_Rep, _Period>& __d)
193 {
194 typedef typename _ToDur::rep __to_rep;
195 return _ToDur(static_cast<__to_rep>(
196 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num)));
197 }
198 };
199
200 template<typename _Tp>
201 struct __is_duration
203 { };
204
205 template<typename _Rep, typename _Period>
206 struct __is_duration<duration<_Rep, _Period>>
208 { };
209
210 template<typename _Tp>
211 using __enable_if_is_duration
212 = typename enable_if<__is_duration<_Tp>::value, _Tp>::type;
213
214 template<typename _Tp>
215 using __disable_if_is_duration
216 = typename enable_if<!__is_duration<_Tp>::value, _Tp>::type;
217
218 /// @endcond
219
220 /// duration_cast
221 template<typename _ToDur, typename _Rep, typename _Period>
222 constexpr __enable_if_is_duration<_ToDur>
224 {
225 typedef typename _ToDur::period __to_period;
226 typedef typename _ToDur::rep __to_rep;
229 __cr;
230 typedef __duration_cast_impl<_ToDur, __cf, __cr,
231 __cf::num == 1, __cf::den == 1> __dc;
232 return __dc::__cast(__d);
233 }
234
235 /// treat_as_floating_point
236 template<typename _Rep>
238 : is_floating_point<_Rep>
239 { };
240
241#if __cplusplus > 201402L
242 template <typename _Rep>
243 inline constexpr bool treat_as_floating_point_v =
245#endif // C++17
246
247#if __cplusplus > 201703L
248 template<typename _Tp>
249 struct is_clock;
250
251 template<typename _Tp>
252 inline constexpr bool is_clock_v = is_clock<_Tp>::value;
253
254#if __cpp_lib_concepts
255 template<typename _Tp>
256 struct is_clock : false_type
257 { };
258
259 template<typename _Tp>
260 requires requires {
261 typename _Tp::rep;
262 typename _Tp::period;
263 typename _Tp::duration;
264 typename _Tp::time_point::clock;
265 typename _Tp::time_point::duration;
266 { &_Tp::is_steady } -> same_as<const bool*>;
267 { _Tp::now() } -> same_as<typename _Tp::time_point>;
268 requires same_as<typename _Tp::duration,
269 duration<typename _Tp::rep, typename _Tp::period>>;
270 requires same_as<typename _Tp::time_point::duration,
271 typename _Tp::duration>;
272 }
273 struct is_clock<_Tp> : true_type
274 { };
275#else
276 template<typename _Tp, typename = void>
277 struct __is_clock_impl : false_type
278 { };
279
280 template<typename _Tp>
281 struct __is_clock_impl<_Tp,
282 void_t<typename _Tp::rep, typename _Tp::period,
283 typename _Tp::duration,
284 typename _Tp::time_point::duration,
285 decltype(_Tp::is_steady),
286 decltype(_Tp::now())>>
287 : __and_<is_same<typename _Tp::duration,
288 duration<typename _Tp::rep, typename _Tp::period>>,
289 is_same<typename _Tp::time_point::duration,
290 typename _Tp::duration>,
291 is_same<decltype(&_Tp::is_steady), const bool*>,
292 is_same<decltype(_Tp::now()), typename _Tp::time_point>>::type
293 { };
294
295 template<typename _Tp>
296 struct is_clock : __is_clock_impl<_Tp>::type
297 { };
298#endif
299#endif // C++20
300
301#if __cplusplus >= 201703L
302# define __cpp_lib_chrono 201611
303
304 template<typename _ToDur, typename _Rep, typename _Period>
305 constexpr __enable_if_is_duration<_ToDur>
306 floor(const duration<_Rep, _Period>& __d)
307 {
308 auto __to = chrono::duration_cast<_ToDur>(__d);
309 if (__to > __d)
310 return __to - _ToDur{1};
311 return __to;
312 }
313
314 template<typename _ToDur, typename _Rep, typename _Period>
315 constexpr __enable_if_is_duration<_ToDur>
316 ceil(const duration<_Rep, _Period>& __d)
317 {
318 auto __to = chrono::duration_cast<_ToDur>(__d);
319 if (__to < __d)
320 return __to + _ToDur{1};
321 return __to;
322 }
323
324 template <typename _ToDur, typename _Rep, typename _Period>
325 constexpr enable_if_t<
326 __and_<__is_duration<_ToDur>,
327 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
328 _ToDur>
329 round(const duration<_Rep, _Period>& __d)
330 {
331 _ToDur __t0 = chrono::floor<_ToDur>(__d);
332 _ToDur __t1 = __t0 + _ToDur{1};
333 auto __diff0 = __d - __t0;
334 auto __diff1 = __t1 - __d;
335 if (__diff0 == __diff1)
336 {
337 if (__t0.count() & 1)
338 return __t1;
339 return __t0;
340 }
341 else if (__diff0 < __diff1)
342 return __t0;
343 return __t1;
344 }
345
346 template<typename _Rep, typename _Period>
347 constexpr
348 enable_if_t<numeric_limits<_Rep>::is_signed, duration<_Rep, _Period>>
349 abs(duration<_Rep, _Period> __d)
350 {
351 if (__d >= __d.zero())
352 return __d;
353 return -__d;
354 }
355#endif // C++17
356
357 /// duration_values
358 template<typename _Rep>
360 {
361 static constexpr _Rep
362 zero() noexcept
363 { return _Rep(0); }
364
365 static constexpr _Rep
366 max() noexcept
367 { return numeric_limits<_Rep>::max(); }
368
369 static constexpr _Rep
370 min() noexcept
371 { return numeric_limits<_Rep>::lowest(); }
372 };
373
374 /// @cond undocumented
375
376 template<typename _Tp>
377 struct __is_ratio
379 { };
380
381 template<intmax_t _Num, intmax_t _Den>
382 struct __is_ratio<ratio<_Num, _Den>>
384 { };
385
386 /// @endcond
387
388 /// duration
389 template<typename _Rep, typename _Period>
390 struct duration
391 {
392 private:
393 template<typename _Rep2>
395
396 // _Period2 is an exact multiple of _Period
397 template<typename _Period2>
398 using __is_harmonic
400
401 public:
402
403 typedef _Rep rep;
404 typedef _Period period;
405
406 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration");
407 static_assert(__is_ratio<_Period>::value,
408 "period must be a specialization of ratio");
409 static_assert(_Period::num > 0, "period must be positive");
410
411 // 20.11.5.1 construction / copy / destroy
412 constexpr duration() = default;
413
414 duration(const duration&) = default;
415
416 // _GLIBCXX_RESOLVE_LIB_DEFECTS
417 // 3050. Conversion specification problem in chrono::duration
418 template<typename _Rep2, typename = _Require<
420 __or_<__is_float<rep>, __not_<__is_float<_Rep2>>>>>
421 constexpr explicit duration(const _Rep2& __rep)
422 : __r(static_cast<rep>(__rep)) { }
423
424 template<typename _Rep2, typename _Period2, typename = _Require<
425 __or_<__is_float<rep>,
426 __and_<__is_harmonic<_Period2>,
427 __not_<__is_float<_Rep2>>>>>>
428 constexpr duration(const duration<_Rep2, _Period2>& __d)
429 : __r(duration_cast<duration>(__d).count()) { }
430
431 ~duration() = default;
432 duration& operator=(const duration&) = default;
433
434 // 20.11.5.2 observer
435 constexpr rep
436 count() const
437 { return __r; }
438
439 // 20.11.5.3 arithmetic
440 constexpr duration
441 operator+() const
442 { return *this; }
443
444 constexpr duration
445 operator-() const
446 { return duration(-__r); }
447
448 _GLIBCXX17_CONSTEXPR duration&
449 operator++()
450 {
451 ++__r;
452 return *this;
453 }
454
455 _GLIBCXX17_CONSTEXPR duration
456 operator++(int)
457 { return duration(__r++); }
458
459 _GLIBCXX17_CONSTEXPR duration&
460 operator--()
461 {
462 --__r;
463 return *this;
464 }
465
466 _GLIBCXX17_CONSTEXPR duration
467 operator--(int)
468 { return duration(__r--); }
469
470 _GLIBCXX17_CONSTEXPR duration&
471 operator+=(const duration& __d)
472 {
473 __r += __d.count();
474 return *this;
475 }
476
477 _GLIBCXX17_CONSTEXPR duration&
478 operator-=(const duration& __d)
479 {
480 __r -= __d.count();
481 return *this;
482 }
483
484 _GLIBCXX17_CONSTEXPR duration&
485 operator*=(const rep& __rhs)
486 {
487 __r *= __rhs;
488 return *this;
489 }
490
491 _GLIBCXX17_CONSTEXPR duration&
492 operator/=(const rep& __rhs)
493 {
494 __r /= __rhs;
495 return *this;
496 }
497
498 // DR 934.
499 template<typename _Rep2 = rep>
500 _GLIBCXX17_CONSTEXPR
502 duration&>::type
503 operator%=(const rep& __rhs)
504 {
505 __r %= __rhs;
506 return *this;
507 }
508
509 template<typename _Rep2 = rep>
510 _GLIBCXX17_CONSTEXPR
512 duration&>::type
513 operator%=(const duration& __d)
514 {
515 __r %= __d.count();
516 return *this;
517 }
518
519 // 20.11.5.4 special values
520 static constexpr duration
521 zero() noexcept
523
524 static constexpr duration
525 min() noexcept
527
528 static constexpr duration
529 max() noexcept
531
532 private:
533 rep __r;
534 };
535
536 /// @relates duration @{
537
538 /// The sum of two durations.
539 template<typename _Rep1, typename _Period1,
540 typename _Rep2, typename _Period2>
541 constexpr typename common_type<duration<_Rep1, _Period1>,
544 const duration<_Rep2, _Period2>& __rhs)
545 {
546 typedef duration<_Rep1, _Period1> __dur1;
547 typedef duration<_Rep2, _Period2> __dur2;
548 typedef typename common_type<__dur1,__dur2>::type __cd;
549 return __cd(__cd(__lhs).count() + __cd(__rhs).count());
550 }
551
552 /// The difference between two durations.
553 template<typename _Rep1, typename _Period1,
554 typename _Rep2, typename _Period2>
555 constexpr typename common_type<duration<_Rep1, _Period1>,
558 const duration<_Rep2, _Period2>& __rhs)
559 {
560 typedef duration<_Rep1, _Period1> __dur1;
561 typedef duration<_Rep2, _Period2> __dur2;
562 typedef typename common_type<__dur1,__dur2>::type __cd;
563 return __cd(__cd(__lhs).count() - __cd(__rhs).count());
564 }
565
566 /// @}
567
568 /// @cond undocumented
569
570 // SFINAE helper to obtain common_type<_Rep1, _Rep2> only if _Rep2
571 // is implicitly convertible to it.
572 // _GLIBCXX_RESOLVE_LIB_DEFECTS
573 // 3050. Conversion specification problem in chrono::duration constructor
574 template<typename _Rep1, typename _Rep2,
575 typename _CRep = typename common_type<_Rep1, _Rep2>::type>
576 using __common_rep_t = typename
578
579 /// @endcond
580
581 /// @relates duration @{
582
583 /// Multiply a duration by a scalar value.
584 template<typename _Rep1, typename _Period, typename _Rep2>
585 constexpr duration<__common_rep_t<_Rep1, _Rep2>, _Period>
586 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
587 {
589 __cd;
590 return __cd(__cd(__d).count() * __s);
591 }
592
593 /// Multiply a duration by a scalar value.
594 template<typename _Rep1, typename _Rep2, typename _Period>
595 constexpr duration<__common_rep_t<_Rep2, _Rep1>, _Period>
596 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
597 { return __d * __s; }
598
599 template<typename _Rep1, typename _Period, typename _Rep2>
600 constexpr
602 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
603 {
605 __cd;
606 return __cd(__cd(__d).count() / __s);
607 }
608
609 template<typename _Rep1, typename _Period1,
610 typename _Rep2, typename _Period2>
611 constexpr typename common_type<_Rep1, _Rep2>::type
612 operator/(const duration<_Rep1, _Period1>& __lhs,
613 const duration<_Rep2, _Period2>& __rhs)
614 {
615 typedef duration<_Rep1, _Period1> __dur1;
616 typedef duration<_Rep2, _Period2> __dur2;
617 typedef typename common_type<__dur1,__dur2>::type __cd;
618 return __cd(__lhs).count() / __cd(__rhs).count();
619 }
620
621 // DR 934.
622 template<typename _Rep1, typename _Period, typename _Rep2>
623 constexpr
624 duration<__common_rep_t<_Rep1, __disable_if_is_duration<_Rep2>>, _Period>
625 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
626 {
627 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period>
628 __cd;
629 return __cd(__cd(__d).count() % __s);
630 }
631
632 template<typename _Rep1, typename _Period1,
633 typename _Rep2, typename _Period2>
634 constexpr typename common_type<duration<_Rep1, _Period1>,
635 duration<_Rep2, _Period2>>::type
636 operator%(const duration<_Rep1, _Period1>& __lhs,
637 const duration<_Rep2, _Period2>& __rhs)
638 {
639 typedef duration<_Rep1, _Period1> __dur1;
640 typedef duration<_Rep2, _Period2> __dur2;
641 typedef typename common_type<__dur1,__dur2>::type __cd;
642 return __cd(__cd(__lhs).count() % __cd(__rhs).count());
643 }
644
645 // comparisons
646
647 template<typename _Rep1, typename _Period1,
648 typename _Rep2, typename _Period2>
649 constexpr bool
650 operator==(const duration<_Rep1, _Period1>& __lhs,
651 const duration<_Rep2, _Period2>& __rhs)
652 {
653 typedef duration<_Rep1, _Period1> __dur1;
654 typedef duration<_Rep2, _Period2> __dur2;
655 typedef typename common_type<__dur1,__dur2>::type __ct;
656 return __ct(__lhs).count() == __ct(__rhs).count();
657 }
658
659 template<typename _Rep1, typename _Period1,
660 typename _Rep2, typename _Period2>
661 constexpr bool
662 operator<(const duration<_Rep1, _Period1>& __lhs,
663 const duration<_Rep2, _Period2>& __rhs)
664 {
665 typedef duration<_Rep1, _Period1> __dur1;
666 typedef duration<_Rep2, _Period2> __dur2;
667 typedef typename common_type<__dur1,__dur2>::type __ct;
668 return __ct(__lhs).count() < __ct(__rhs).count();
669 }
670
671 template<typename _Rep1, typename _Period1,
672 typename _Rep2, typename _Period2>
673 constexpr bool
674 operator!=(const duration<_Rep1, _Period1>& __lhs,
675 const duration<_Rep2, _Period2>& __rhs)
676 { return !(__lhs == __rhs); }
677
678 template<typename _Rep1, typename _Period1,
679 typename _Rep2, typename _Period2>
680 constexpr bool
681 operator<=(const duration<_Rep1, _Period1>& __lhs,
682 const duration<_Rep2, _Period2>& __rhs)
683 { return !(__rhs < __lhs); }
684
685 template<typename _Rep1, typename _Period1,
686 typename _Rep2, typename _Period2>
687 constexpr bool
688 operator>(const duration<_Rep1, _Period1>& __lhs,
689 const duration<_Rep2, _Period2>& __rhs)
690 { return __rhs < __lhs; }
691
692 template<typename _Rep1, typename _Period1,
693 typename _Rep2, typename _Period2>
694 constexpr bool
695 operator>=(const duration<_Rep1, _Period1>& __lhs,
696 const duration<_Rep2, _Period2>& __rhs)
697 { return !(__lhs < __rhs); }
698
699 /// @}
700
701#ifdef _GLIBCXX_USE_C99_STDINT_TR1
702# define _GLIBCXX_CHRONO_INT64_T int64_t
703#elif defined __INT64_TYPE__
704# define _GLIBCXX_CHRONO_INT64_T __INT64_TYPE__
705#else
707 "Representation type for nanoseconds must have at least 64 bits");
708# define _GLIBCXX_CHRONO_INT64_T long long
709#endif
710
711 /// nanoseconds
713
714 /// microseconds
716
717 /// milliseconds
719
720 /// seconds
722
723 /// minutes
725
726 /// hours
728
729#if __cplusplus > 201703L
730 /// days
732
733 /// weeks
735
736 /// years
738
739 /// months
741#endif // C++20
742
743#undef _GLIBCXX_CHRONO_INT64_T
744
745 /// time_point
746 template<typename _Clock, typename _Dur>
748 {
749 static_assert(__is_duration<_Dur>::value,
750 "duration must be a specialization of std::chrono::duration");
751
752 typedef _Clock clock;
753 typedef _Dur duration;
754 typedef typename duration::rep rep;
755 typedef typename duration::period period;
756
757 constexpr time_point() : __d(duration::zero())
758 { }
759
760 constexpr explicit time_point(const duration& __dur)
761 : __d(__dur)
762 { }
763
764 // conversions
765 template<typename _Dur2,
766 typename = _Require<is_convertible<_Dur2, _Dur>>>
767 constexpr time_point(const time_point<clock, _Dur2>& __t)
768 : __d(__t.time_since_epoch())
769 { }
770
771 // observer
772 constexpr duration
773 time_since_epoch() const
774 { return __d; }
775
776 // arithmetic
777 _GLIBCXX17_CONSTEXPR time_point&
778 operator+=(const duration& __dur)
779 {
780 __d += __dur;
781 return *this;
782 }
783
784 _GLIBCXX17_CONSTEXPR time_point&
785 operator-=(const duration& __dur)
786 {
787 __d -= __dur;
788 return *this;
789 }
790
791 // special values
792 static constexpr time_point
793 min() noexcept
794 { return time_point(duration::min()); }
795
796 static constexpr time_point
797 max() noexcept
798 { return time_point(duration::max()); }
799
800 private:
801 duration __d;
802 };
803
804 /// time_point_cast
805 template<typename _ToDur, typename _Clock, typename _Dur>
809 {
810 typedef time_point<_Clock, _ToDur> __time_point;
811 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch()));
812 }
813
814#if __cplusplus > 201402L
815 template<typename _ToDur, typename _Clock, typename _Dur>
816 constexpr
817 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
818 floor(const time_point<_Clock, _Dur>& __tp)
819 {
820 return time_point<_Clock, _ToDur>{
821 chrono::floor<_ToDur>(__tp.time_since_epoch())};
822 }
823
824 template<typename _ToDur, typename _Clock, typename _Dur>
825 constexpr
826 enable_if_t<__is_duration<_ToDur>::value, time_point<_Clock, _ToDur>>
827 ceil(const time_point<_Clock, _Dur>& __tp)
828 {
829 return time_point<_Clock, _ToDur>{
830 chrono::ceil<_ToDur>(__tp.time_since_epoch())};
831 }
832
833 template<typename _ToDur, typename _Clock, typename _Dur>
834 constexpr enable_if_t<
835 __and_<__is_duration<_ToDur>,
836 __not_<treat_as_floating_point<typename _ToDur::rep>>>::value,
837 time_point<_Clock, _ToDur>>
838 round(const time_point<_Clock, _Dur>& __tp)
839 {
840 return time_point<_Clock, _ToDur>{
841 chrono::round<_ToDur>(__tp.time_since_epoch())};
842 }
843#endif // C++17
844
845 /// @relates time_point @{
846
847 /// Adjust a time point forwards by the given duration.
848 template<typename _Clock, typename _Dur1,
849 typename _Rep2, typename _Period2>
850 constexpr time_point<_Clock,
851 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type>
853 const duration<_Rep2, _Period2>& __rhs)
854 {
855 typedef duration<_Rep2, _Period2> __dur2;
856 typedef typename common_type<_Dur1,__dur2>::type __ct;
857 typedef time_point<_Clock, __ct> __time_point;
858 return __time_point(__lhs.time_since_epoch() + __rhs);
859 }
860
861 /// Adjust a time point forwards by the given duration.
862 template<typename _Rep1, typename _Period1,
863 typename _Clock, typename _Dur2>
864 constexpr time_point<_Clock,
865 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type>
866 operator+(const duration<_Rep1, _Period1>& __lhs,
867 const time_point<_Clock, _Dur2>& __rhs)
868 {
869 typedef duration<_Rep1, _Period1> __dur1;
870 typedef typename common_type<__dur1,_Dur2>::type __ct;
871 typedef time_point<_Clock, __ct> __time_point;
872 return __time_point(__rhs.time_since_epoch() + __lhs);
873 }
874
875 /// Adjust a time point backwards by the given duration.
876 template<typename _Clock, typename _Dur1,
877 typename _Rep2, typename _Period2>
878 constexpr time_point<_Clock,
881 const duration<_Rep2, _Period2>& __rhs)
882 {
883 typedef duration<_Rep2, _Period2> __dur2;
884 typedef typename common_type<_Dur1,__dur2>::type __ct;
885 typedef time_point<_Clock, __ct> __time_point;
886 return __time_point(__lhs.time_since_epoch() -__rhs);
887 }
888
889 /// @}
890
891 /// @relates time_point @{
892
893 /// The difference between two time points (as a duration)
894 template<typename _Clock, typename _Dur1, typename _Dur2>
895 constexpr typename common_type<_Dur1, _Dur2>::type
897 const time_point<_Clock, _Dur2>& __rhs)
898 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); }
899
900 template<typename _Clock, typename _Dur1, typename _Dur2>
901 constexpr bool
902 operator==(const time_point<_Clock, _Dur1>& __lhs,
903 const time_point<_Clock, _Dur2>& __rhs)
904 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); }
905
906 template<typename _Clock, typename _Dur1, typename _Dur2>
907 constexpr bool
908 operator!=(const time_point<_Clock, _Dur1>& __lhs,
909 const time_point<_Clock, _Dur2>& __rhs)
910 { return !(__lhs == __rhs); }
911
912 template<typename _Clock, typename _Dur1, typename _Dur2>
913 constexpr bool
914 operator<(const time_point<_Clock, _Dur1>& __lhs,
915 const time_point<_Clock, _Dur2>& __rhs)
916 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); }
917
918 template<typename _Clock, typename _Dur1, typename _Dur2>
919 constexpr bool
920 operator<=(const time_point<_Clock, _Dur1>& __lhs,
921 const time_point<_Clock, _Dur2>& __rhs)
922 { return !(__rhs < __lhs); }
923
924 template<typename _Clock, typename _Dur1, typename _Dur2>
925 constexpr bool
926 operator>(const time_point<_Clock, _Dur1>& __lhs,
927 const time_point<_Clock, _Dur2>& __rhs)
928 { return __rhs < __lhs; }
929
930 template<typename _Clock, typename _Dur1, typename _Dur2>
931 constexpr bool
932 operator>=(const time_point<_Clock, _Dur1>& __lhs,
933 const time_point<_Clock, _Dur2>& __rhs)
934 { return !(__lhs < __rhs); }
935
936 // @}
937
938 // Clocks.
939
940 // Why nanosecond resolution as the default?
941 // Why have std::system_clock always count in the highest
942 // resolution (ie nanoseconds), even if on some OSes the low 3
943 // or 9 decimal digits will be always zero? This allows later
944 // implementations to change the system_clock::now()
945 // implementation any time to provide better resolution without
946 // changing function signature or units.
947
948 // To support the (forward) evolution of the library's defined
949 // clocks, wrap inside inline namespace so that the current
950 // defintions of system_clock, steady_clock, and
951 // high_resolution_clock types are uniquely mangled. This way, new
952 // code can use the latests clocks, while the library can contain
953 // compatibility definitions for previous versions. At some
954 // point, when these clocks settle down, the inlined namespaces
955 // can be removed. XXX GLIBCXX_ABI Deprecated
956 inline namespace _V2 {
957
958 /**
959 * @brief System clock.
960 *
961 * Time returned represents wall time from the system-wide clock.
962 * @ingroup chrono
963 */
965 {
967 typedef duration::rep rep;
968 typedef duration::period period;
970
971 static_assert(system_clock::duration::min()
972 < system_clock::duration::zero(),
973 "a clock's minimum duration cannot be less than its epoch");
974
975 static constexpr bool is_steady = false;
976
977 static time_point
978 now() noexcept;
979
980 // Map to C API
981 static std::time_t
982 to_time_t(const time_point& __t) noexcept
983 {
984 return std::time_t(duration_cast<chrono::seconds>
985 (__t.time_since_epoch()).count());
986 }
987
988 static time_point
989 from_time_t(std::time_t __t) noexcept
990 {
992 return time_point_cast<system_clock::duration>
993 (__from(chrono::seconds(__t)));
994 }
995 };
996
997
998 /**
999 * @brief Monotonic clock
1000 *
1001 * Time returned has the property of only increasing at a uniform rate.
1002 * @ingroup chrono
1003 */
1005 {
1007 typedef duration::rep rep;
1008 typedef duration::period period;
1010
1011 static constexpr bool is_steady = true;
1012
1013 static time_point
1014 now() noexcept;
1015 };
1016
1017
1018 /**
1019 * @brief Highest-resolution clock
1020 *
1021 * This is the clock "with the shortest tick period." Alias to
1022 * std::system_clock until higher-than-nanosecond definitions
1023 * become feasible.
1024 * @ingroup chrono
1025 */
1027
1028 } // end inline namespace _V2
1029
1030#if __cplusplus > 201703L
1031 template<typename _Duration>
1032 using sys_time = time_point<system_clock, _Duration>;
1033 using sys_seconds = sys_time<seconds>;
1034 using sys_days = sys_time<days>;
1035
1036 using file_clock = ::std::filesystem::__file_clock;
1037
1038 template<typename _Duration>
1039 using file_time = time_point<file_clock, _Duration>;
1040
1041 template<> struct is_clock<system_clock> : true_type { };
1042 template<> struct is_clock<steady_clock> : true_type { };
1043 template<> struct is_clock<file_clock> : true_type { };
1044
1045 template<> inline constexpr bool is_clock_v<system_clock> = true;
1046 template<> inline constexpr bool is_clock_v<steady_clock> = true;
1047 template<> inline constexpr bool is_clock_v<file_clock> = true;
1048
1049 struct local_t { };
1050 template<typename _Duration>
1051 using local_time = time_point<local_t, _Duration>;
1052 using local_seconds = local_time<seconds>;
1053 using local_days = local_time<days>;
1054#endif // C++20
1055
1056 // @}
1057 } // namespace chrono
1058
1059#if __cplusplus > 201103L
1060
1061#define __cpp_lib_chrono_udls 201304
1062
1063 inline namespace literals
1064 {
1065 /** ISO C++ 2014 namespace for suffixes for duration literals.
1066 *
1067 * These suffixes can be used to create `chrono::duration` values with
1068 * tick periods of hours, minutes, seconds, milliseconds, microseconds
1069 * or nanoseconds. For example, `std::chrono::seconds(5)` can be written
1070 * as `5s` after making the suffix visible in the current scope.
1071 * The suffixes can be made visible by a using-directive or
1072 * using-declaration such as:
1073 * - `using namespace std::chrono_literals;`
1074 * - `using namespace std::literals;`
1075 * - `using namespace std::chrono;`
1076 * - `using namespace std;`
1077 * - `using std::chrono_literals::operator""s;`
1078 *
1079 * The result of these suffixes on an integer literal is one of the
1080 * standard typedefs such as `std::chrono::hours`.
1081 * The result on a floating-point literal is a duration type with the
1082 * specified tick period and an unspecified floating-point representation,
1083 * for example `1.5e2ms` might be equivalent to
1084 * `chrono::duration<long double, chrono::milli>(1.5e2)`.
1085 *
1086 * @ingroup chrono
1087 */
1088 inline namespace chrono_literals
1089 {
1090#pragma GCC diagnostic push
1091#pragma GCC diagnostic ignored "-Wliteral-suffix"
1092 /// @cond undocumented
1093 template<typename _Dur, char... _Digits>
1094 constexpr _Dur __check_overflow()
1095 {
1096 using _Val = __parse_int::_Parse_int<_Digits...>;
1097 constexpr typename _Dur::rep __repval = _Val::value;
1098 static_assert(__repval >= 0 && __repval == _Val::value,
1099 "literal value cannot be represented by duration type");
1100 return _Dur(__repval);
1101 }
1102 /// @endcond
1103
1104 /// Literal suffix for durations representing non-integer hours
1106 operator""h(long double __hours)
1108
1109 /// Literal suffix for durations of type `std::chrono::hours`
1110 template <char... _Digits>
1111 constexpr chrono::hours
1112 operator""h()
1113 { return __check_overflow<chrono::hours, _Digits...>(); }
1114
1115 /// Literal suffix for durations representing non-integer minutes
1117 operator""min(long double __mins)
1119
1120 /// Literal suffix for durations of type `std::chrono::minutes`
1121 template <char... _Digits>
1122 constexpr chrono::minutes
1123 operator""min()
1124 { return __check_overflow<chrono::minutes, _Digits...>(); }
1125
1126 /// Literal suffix for durations representing non-integer seconds
1128 operator""s(long double __secs)
1129 { return chrono::duration<long double>{__secs}; }
1130
1131 /// Literal suffix for durations of type `std::chrono::seconds`
1132 template <char... _Digits>
1133 constexpr chrono::seconds
1134 operator""s()
1135 { return __check_overflow<chrono::seconds, _Digits...>(); }
1136
1137 /// Literal suffix for durations representing non-integer milliseconds
1139 operator""ms(long double __msecs)
1140 { return chrono::duration<long double, milli>{__msecs}; }
1141
1142 /// Literal suffix for durations of type `std::chrono::milliseconds`
1143 template <char... _Digits>
1144 constexpr chrono::milliseconds
1145 operator""ms()
1146 { return __check_overflow<chrono::milliseconds, _Digits...>(); }
1147
1148 /// Literal suffix for durations representing non-integer microseconds
1150 operator""us(long double __usecs)
1151 { return chrono::duration<long double, micro>{__usecs}; }
1152
1153 /// Literal suffix for durations of type `std::chrono::microseconds`
1154 template <char... _Digits>
1155 constexpr chrono::microseconds
1156 operator""us()
1157 { return __check_overflow<chrono::microseconds, _Digits...>(); }
1158
1159 /// Literal suffix for durations representing non-integer nanoseconds
1161 operator""ns(long double __nsecs)
1162 { return chrono::duration<long double, nano>{__nsecs}; }
1163
1164 /// Literal suffix for durations of type `std::chrono::nanoseconds`
1165 template <char... _Digits>
1166 constexpr chrono::nanoseconds
1167 operator""ns()
1168 { return __check_overflow<chrono::nanoseconds, _Digits...>(); }
1169
1170#pragma GCC diagnostic pop
1171 } // inline namespace chrono_literals
1172 } // inline namespace literals
1173
1174 namespace chrono
1175 {
1176 using namespace literals::chrono_literals;
1177 } // namespace chrono
1178
1179#if __cplusplus >= 201703L
1180 namespace filesystem
1181 {
1182 struct __file_clock
1183 {
1184 using duration = chrono::nanoseconds;
1185 using rep = duration::rep;
1186 using period = duration::period;
1187 using time_point = chrono::time_point<__file_clock>;
1188 static constexpr bool is_steady = false;
1189
1190 static time_point
1191 now() noexcept
1192 { return _S_from_sys(chrono::system_clock::now()); }
1193
1194#if __cplusplus > 201703L
1195 template<typename _Dur>
1196 static
1197 chrono::file_time<_Dur>
1198 from_sys(const chrono::sys_time<_Dur>& __t) noexcept
1199 { return _S_from_sys(__t); }
1200
1201 // For internal use only
1202 template<typename _Dur>
1203 static
1204 chrono::sys_time<_Dur>
1205 to_sys(const chrono::file_time<_Dur>& __t) noexcept
1206 { return _S_to_sys(__t); }
1207#endif // C++20
1208
1209 private:
1210 using __sys_clock = chrono::system_clock;
1211
1212 // This clock's (unspecified) epoch is 2174-01-01 00:00:00 UTC.
1213 // A signed 64-bit duration with nanosecond resolution gives roughly
1214 // +/- 292 years, which covers the 1901-2446 date range for ext4.
1215 static constexpr chrono::seconds _S_epoch_diff{6437664000};
1216
1217 protected:
1218 // For internal use only
1219 template<typename _Dur>
1220 static
1221 chrono::time_point<__file_clock, _Dur>
1222 _S_from_sys(const chrono::time_point<__sys_clock, _Dur>& __t) noexcept
1223 {
1224 using __file_time = chrono::time_point<__file_clock, _Dur>;
1225 return __file_time{__t.time_since_epoch()} - _S_epoch_diff;
1226 }
1227
1228 // For internal use only
1229 template<typename _Dur>
1230 static
1231 chrono::time_point<__sys_clock, _Dur>
1232 _S_to_sys(const chrono::time_point<__file_clock, _Dur>& __t) noexcept
1233 {
1234 using __sys_time = chrono::time_point<__sys_clock, _Dur>;
1235 return __sys_time{__t.time_since_epoch()} + _S_epoch_diff;
1236 }
1237 };
1238 } // namespace filesystem
1239#endif // C++17
1240#endif // C++14
1241
1242_GLIBCXX_END_NAMESPACE_VERSION
1243} // namespace std
1244
1245#endif // C++11
1246
1247#endif //_GLIBCXX_CHRONO
constexpr duration< __common_rep_t< _Rep2, _Rep1 >, _Period > operator*(const _Rep1 &__s, const duration< _Rep2, _Period > &__d)
Multiply a duration by a scalar value.
Definition: chrono:596
duration< int64_t, milli > milliseconds
milliseconds
Definition: chrono:718
constexpr time_point< _Clock, typename common_type< _Dur1, duration< _Rep2, _Period2 > >::type > operator-(const time_point< _Clock, _Dur1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Adjust a time point backwards by the given duration.
Definition: chrono:880
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:557
duration< int64_t, ratio< 3600 > > hours
hours
Definition: chrono:727
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:852
duration< int64_t, ratio< 60 > > minutes
minutes
Definition: chrono:724
constexpr common_type< duration< _Rep1, _Period1 >, duration< _Rep2, _Period2 > >::type operator+(const duration< _Rep1, _Period1 > &__lhs, const duration< _Rep2, _Period2 > &__rhs)
Definition: chrono:543
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:866
constexpr enable_if< __is_duration< _ToDur >::value, time_point< _Clock, _ToDur > >::type time_point_cast(const time_point< _Clock, _Dur > &__t)
time_point_cast
Definition: chrono:808
duration< int64_t, nano > nanoseconds
nanoseconds
Definition: chrono:712
constexpr common_type< _Dur1, _Dur2 >::type operator-(const time_point< _Clock, _Dur1 > &__lhs, const time_point< _Clock, _Dur2 > &__rhs)
Definition: chrono:896
constexpr duration< __common_rep_t< _Rep1, _Rep2 >, _Period > operator*(const duration< _Rep1, _Period > &__d, const _Rep2 &__s)
Definition: chrono:586
duration< int64_t, micro > microseconds
microseconds
Definition: chrono:715
duration< int64_t > seconds
seconds
Definition: chrono:721
constexpr __enable_if_is_duration< _ToDur > duration_cast(const duration< _Rep, _Period > &__d)
duration_cast
Definition: chrono:223
_Tp abs(const complex< _Tp > &)
Return magnitude of z.
Definition: complex:629
constexpr complex< _Tp > operator/(const complex< _Tp > &__x, const complex< _Tp > &__y)
Return new complex value x divided by y.
Definition: complex:421
typename __ratio_divide< _R1, _R2 >::type ratio_divide
ratio_divide
Definition: ratio:347
void void_t
A metafunction that always yields void, used for detecting valid types.
Definition: type_traits:2563
integral_constant< bool, true > true_type
The type used as a compile-time boolean with true value.
Definition: type_traits:75
typename enable_if< _Cond, _Tp >::type enable_if_t
Alias template for enable_if.
Definition: type_traits:2541
integral_constant< bool, false > false_type
The type used as a compile-time boolean with false value.
Definition: type_traits:78
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
ISO C++ entities toplevel namespace is std.
time_point
Definition: chrono:748
treat_as_floating_point
Definition: chrono:239
duration_values
Definition: chrono:360
Monotonic clock.
Definition: chrono:1005
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:58
is_floating_point
Definition: type_traits:395
is_convertible
Definition: type_traits:1437
Define a member typedef type only if a boolean constant is true.
Definition: type_traits:2170
common_type
Definition: type_traits:2202