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