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