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