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