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