blob: c69e88ae4ded661f6ddf63b82c119597853d1834 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_CHRONO
12#define _LIBCPP_CHRONO
13
14/*
15 chrono synopsis
16
17namespace std
18{
19namespace chrono
20{
21
22template <class ToDuration, class Rep, class Period>
Howard Hinnant473f8382012-07-13 19:17:27 +000023constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024ToDuration
25duration_cast(const duration<Rep, Period>& fd);
26
27template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {};
28
Marshall Clowa3866e42015-11-30 05:39:30 +000029template <class Rep> constexpr bool treat_as_floating_point_v
30 = treat_as_floating_point<Rep>::value; // C++17
31
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000032template <class Rep>
33struct duration_values
34{
35public:
Howard Hinnant473f8382012-07-13 19:17:27 +000036 static constexpr Rep zero();
37 static constexpr Rep max();
38 static constexpr Rep min();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039};
40
41// duration
42
43template <class Rep, class Period = ratio<1>>
44class duration
45{
46 static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration");
47 static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio");
48 static_assert(Period::num > 0, "duration period must be positive");
49public:
50 typedef Rep rep;
Marshall Clowcf6e0db2017-03-21 18:38:57 +000051 typedef typename _Period::type period;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000052
Howard Hinnant473f8382012-07-13 19:17:27 +000053 constexpr duration() = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000054 template <class Rep2>
Howard Hinnant473f8382012-07-13 19:17:27 +000055 constexpr explicit duration(const Rep2& r,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000056 typename enable_if
57 <
58 is_convertible<Rep2, rep>::value &&
59 (treat_as_floating_point<rep>::value ||
60 !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value)
61 >::type* = 0);
62
63 // conversions
64 template <class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +000065 constexpr duration(const duration<Rep2, Period2>& d,
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000066 typename enable_if
67 <
68 treat_as_floating_point<rep>::value ||
69 ratio_divide<Period2, period>::type::den == 1
70 >::type* = 0);
71
72 // observer
73
Howard Hinnant473f8382012-07-13 19:17:27 +000074 constexpr rep count() const;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000075
76 // arithmetic
77
Marshall Clowcf6e0db2017-03-21 18:38:57 +000078 constexpr common_type<duration>::type operator+() const;
79 constexpr common_type<duration>::type operator-() const;
Marshall Clow3df90c92017-01-04 23:03:24 +000080 constexpr duration& operator++();
81 constexpr duration operator++(int);
82 constexpr duration& operator--();
83 constexpr duration operator--(int);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000084
Marshall Clow3df90c92017-01-04 23:03:24 +000085 constexpr duration& operator+=(const duration& d);
86 constexpr duration& operator-=(const duration& d);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000087
88 duration& operator*=(const rep& rhs);
89 duration& operator/=(const rep& rhs);
90
91 // special values
92
Howard Hinnant473f8382012-07-13 19:17:27 +000093 static constexpr duration zero();
94 static constexpr duration min();
95 static constexpr duration max();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000096};
97
98typedef duration<long long, nano> nanoseconds;
99typedef duration<long long, micro> microseconds;
100typedef duration<long long, milli> milliseconds;
101typedef duration<long long > seconds;
102typedef duration< long, ratio< 60> > minutes;
103typedef duration< long, ratio<3600> > hours;
104
105template <class Clock, class Duration = typename Clock::duration>
106class time_point
107{
108public:
109 typedef Clock clock;
110 typedef Duration duration;
111 typedef typename duration::rep rep;
112 typedef typename duration::period period;
113private:
114 duration d_; // exposition only
115
116public:
Marshall Clow832b3042013-07-31 19:32:19 +0000117 time_point(); // has value "epoch" // constexpr in C++14
118 explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000119
120 // conversions
121 template <class Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000122 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000123
124 // observer
125
Marshall Clow832b3042013-07-31 19:32:19 +0000126 duration time_since_epoch() const; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127
128 // arithmetic
129
130 time_point& operator+=(const duration& d);
131 time_point& operator-=(const duration& d);
132
133 // special values
134
135 static constexpr time_point min();
136 static constexpr time_point max();
137};
138
139} // chrono
140
141// common_type traits
142template <class Rep1, class Period1, class Rep2, class Period2>
143 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
144
145template <class Clock, class Duration1, class Duration2>
146 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
147
148namespace chrono {
149
150// duration arithmetic
151template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000152 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000153 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
154 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
155template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000156 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
158 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
159template <class Rep1, class Period, class Rep2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000160 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161 duration<typename common_type<Rep1, Rep2>::type, Period>
162 operator*(const duration<Rep1, Period>& d, const Rep2& s);
163template <class Rep1, class Period, class Rep2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000164 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165 duration<typename common_type<Rep1, Rep2>::type, Period>
166 operator*(const Rep1& s, const duration<Rep2, Period>& d);
167template <class Rep1, class Period, class Rep2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000168 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 duration<typename common_type<Rep1, Rep2>::type, Period>
170 operator/(const duration<Rep1, Period>& d, const Rep2& s);
171template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000172 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000173 typename common_type<Rep1, Rep2>::type
174 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
175
176// duration comparisons
177template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000178 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000179 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
180template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000181 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000182 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
183template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000184 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
186template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000187 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000188 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
189template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000190 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000191 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
192template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000193 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000194 bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
195
196// duration_cast
197template <class ToDuration, class Rep, class Period>
198 ToDuration duration_cast(const duration<Rep, Period>& d);
199
Marshall Clow223df2e2015-11-05 19:33:59 +0000200template <class ToDuration, class Rep, class Period>
201 constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17
202template <class ToDuration, class Rep, class Period>
203 constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17
204template <class ToDuration, class Rep, class Period>
205 constexpr ToDuration round(const duration<Rep, Period>& d); // C++17
206
Marshall Clow832b3042013-07-31 19:32:19 +0000207// time_point arithmetic (all constexpr in C++14)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208template <class Clock, class Duration1, class Rep2, class Period2>
209 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
210 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
211template <class Rep1, class Period1, class Clock, class Duration2>
212 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
213 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
214template <class Clock, class Duration1, class Rep2, class Period2>
215 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
216 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
217template <class Clock, class Duration1, class Duration2>
218 typename common_type<Duration1, Duration2>::type
219 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
220
Marshall Clow832b3042013-07-31 19:32:19 +0000221// time_point comparisons (all constexpr in C++14)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000222template <class Clock, class Duration1, class Duration2>
223 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
224template <class Clock, class Duration1, class Duration2>
225 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
226template <class Clock, class Duration1, class Duration2>
227 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
228template <class Clock, class Duration1, class Duration2>
229 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
230template <class Clock, class Duration1, class Duration2>
231 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
232template <class Clock, class Duration1, class Duration2>
233 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
234
Marshall Clow832b3042013-07-31 19:32:19 +0000235// time_point_cast (constexpr in C++14)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236
237template <class ToDuration, class Clock, class Duration>
238 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
239
Marshall Clow223df2e2015-11-05 19:33:59 +0000240template <class ToDuration, class Clock, class Duration>
241 constexpr time_point<Clock, ToDuration>
242 floor(const time_point<Clock, Duration>& tp); // C++17
243
244template <class ToDuration, class Clock, class Duration>
245 constexpr time_point<Clock, ToDuration>
246 ceil(const time_point<Clock, Duration>& tp); // C++17
247
248template <class ToDuration, class Clock, class Duration>
249 constexpr time_point<Clock, ToDuration>
250 round(const time_point<Clock, Duration>& tp); // C++17
251
252template <class Rep, class Period>
253 constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254// Clocks
255
256class system_clock
257{
258public:
259 typedef microseconds duration;
260 typedef duration::rep rep;
261 typedef duration::period period;
262 typedef chrono::time_point<system_clock> time_point;
Marshall Clow832b3042013-07-31 19:32:19 +0000263 static const bool is_steady = false; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000264
Howard Hinnant756a1762011-05-28 18:34:36 +0000265 static time_point now() noexcept;
266 static time_t to_time_t (const time_point& __t) noexcept;
267 static time_point from_time_t(time_t __t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000268};
269
Howard Hinnantf8f85212010-11-20 19:16:30 +0000270class steady_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000271{
272public:
273 typedef nanoseconds duration;
274 typedef duration::rep rep;
275 typedef duration::period period;
Howard Hinnantf8f85212010-11-20 19:16:30 +0000276 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow832b3042013-07-31 19:32:19 +0000277 static const bool is_steady = true; // constexpr in C++14
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278
Howard Hinnant756a1762011-05-28 18:34:36 +0000279 static time_point now() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000280};
281
Howard Hinnantf8f85212010-11-20 19:16:30 +0000282typedef steady_clock high_resolution_clock;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000283
284} // chrono
285
Marshall Clow3b53fab2017-08-09 15:42:50 +0000286constexpr chrono::hours operator ""h(unsigned long long); // C++14
287constexpr chrono::duration<unspecified , ratio<3600,1>> operator ""h(long double); // C++14
288constexpr chrono::minutes operator ""min(unsigned long long); // C++14
289constexpr chrono::duration<unspecified , ratio<60,1>> operator ""min(long double); // C++14
290constexpr chrono::seconds operator ""s(unsigned long long); // C++14
291constexpr chrono::duration<unspecified > operator ""s(long double); // C++14
292constexpr chrono::milliseconds operator ""ms(unsigned long long); // C++14
293constexpr chrono::duration<unspecified , milli> operator ""ms(long double); // C++14
294constexpr chrono::microseconds operator ""us(unsigned long long); // C++14
295constexpr chrono::duration<unspecified , micro> operator ""us(long double); // C++14
296constexpr chrono::nanoseconds operator ""ns(unsigned long long); // C++14
297constexpr chrono::duration<unspecified , nano> operator ""ns(long double); // C++14
Marshall Clowf1820382013-07-24 21:18:14 +0000298
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299} // std
300*/
301
302#include <__config>
303#include <ctime>
304#include <type_traits>
305#include <ratio>
306#include <limits>
307
Howard Hinnant08e17472011-10-17 20:05:10 +0000308#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000309#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000310#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311
Eric Fiselier018a3d52017-05-31 22:07:49 +0000312_LIBCPP_PUSH_MACROS
313#include <__undef_macros>
314
315
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000316_LIBCPP_BEGIN_NAMESPACE_STD
317
318namespace chrono
319{
320
Eric Fiselierc3589a82017-01-04 23:56:00 +0000321template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000322
Howard Hinnantd8bc09b2010-05-18 17:32:30 +0000323template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000324struct __is_duration : false_type {};
325
326template <class _Rep, class _Period>
327struct __is_duration<duration<_Rep, _Period> > : true_type {};
328
329template <class _Rep, class _Period>
330struct __is_duration<const duration<_Rep, _Period> > : true_type {};
331
332template <class _Rep, class _Period>
333struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
334
335template <class _Rep, class _Period>
336struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
337
338} // chrono
339
340template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000341struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000342 chrono::duration<_Rep2, _Period2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000343{
344 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
345 typename __ratio_gcd<_Period1, _Period2>::type> type;
346};
347
348namespace chrono {
349
350// duration_cast
351
352template <class _FromDuration, class _ToDuration,
353 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
354 bool = _Period::num == 1,
355 bool = _Period::den == 1>
356struct __duration_cast;
357
358template <class _FromDuration, class _ToDuration, class _Period>
359struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
360{
Howard Hinnant473f8382012-07-13 19:17:27 +0000361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362 _ToDuration operator()(const _FromDuration& __fd) const
363 {
364 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
365 }
366};
367
368template <class _FromDuration, class _ToDuration, class _Period>
369struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
370{
Howard Hinnant473f8382012-07-13 19:17:27 +0000371 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000372 _ToDuration operator()(const _FromDuration& __fd) const
373 {
374 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
375 return _ToDuration(static_cast<typename _ToDuration::rep>(
376 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
377 }
378};
379
380template <class _FromDuration, class _ToDuration, class _Period>
381struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
382{
Howard Hinnant473f8382012-07-13 19:17:27 +0000383 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000384 _ToDuration operator()(const _FromDuration& __fd) const
385 {
386 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
387 return _ToDuration(static_cast<typename _ToDuration::rep>(
388 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
389 }
390};
391
392template <class _FromDuration, class _ToDuration, class _Period>
393struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
394{
Howard Hinnant473f8382012-07-13 19:17:27 +0000395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000396 _ToDuration operator()(const _FromDuration& __fd) const
397 {
398 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
399 return _ToDuration(static_cast<typename _ToDuration::rep>(
400 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
401 / static_cast<_Ct>(_Period::den)));
402 }
403};
404
405template <class _ToDuration, class _Rep, class _Period>
406inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000407_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408typename enable_if
409<
410 __is_duration<_ToDuration>::value,
411 _ToDuration
412>::type
413duration_cast(const duration<_Rep, _Period>& __fd)
414{
415 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
416}
417
Howard Hinnant422a53f2010-09-21 21:28:23 +0000418template <class _Rep>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000419struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000420
Marshall Clowa3866e42015-11-30 05:39:30 +0000421#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
422template <class _Rep> _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
423 = treat_as_floating_point<_Rep>::value;
424#endif
425
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426template <class _Rep>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000427struct _LIBCPP_TEMPLATE_VIS duration_values
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428{
429public:
Howard Hinnant473f8382012-07-13 19:17:27 +0000430 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
431 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
432 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433};
434
Marshall Clow223df2e2015-11-05 19:33:59 +0000435#if _LIBCPP_STD_VER > 14
436template <class _ToDuration, class _Rep, class _Period>
437inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
438typename enable_if
439<
440 __is_duration<_ToDuration>::value,
441 _ToDuration
442>::type
443floor(const duration<_Rep, _Period>& __d)
444{
445 _ToDuration __t = duration_cast<_ToDuration>(__d);
446 if (__t > __d)
447 __t = __t - _ToDuration{1};
448 return __t;
449}
450
451template <class _ToDuration, class _Rep, class _Period>
452inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
453typename enable_if
454<
455 __is_duration<_ToDuration>::value,
456 _ToDuration
457>::type
458ceil(const duration<_Rep, _Period>& __d)
459{
460 _ToDuration __t = duration_cast<_ToDuration>(__d);
461 if (__t < __d)
462 __t = __t + _ToDuration{1};
463 return __t;
464}
465
466template <class _ToDuration, class _Rep, class _Period>
467inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
468typename enable_if
469<
470 __is_duration<_ToDuration>::value,
471 _ToDuration
472>::type
473round(const duration<_Rep, _Period>& __d)
474{
475 _ToDuration __lower = floor<_ToDuration>(__d);
476 _ToDuration __upper = __lower + _ToDuration{1};
477 auto __lowerDiff = __d - __lower;
478 auto __upperDiff = __upper - __d;
479 if (__lowerDiff < __upperDiff)
480 return __lower;
481 if (__lowerDiff > __upperDiff)
482 return __upper;
483 return __lower.count() & 1 ? __upper : __lower;
484}
485#endif
486
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000487// duration
488
489template <class _Rep, class _Period>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000490class _LIBCPP_TEMPLATE_VIS duration
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000491{
492 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
493 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
494 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant713f4db2013-08-31 16:51:56 +0000495
496 template <class _R1, class _R2>
497 struct __no_overflow
498 {
499 private:
500 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
501 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
502 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
503 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
504 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
505 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
506 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
507
508 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
509 struct __mul // __overflow == false
510 {
511 static const intmax_t value = _Xp * _Yp;
512 };
513
514 template <intmax_t _Xp, intmax_t _Yp>
515 struct __mul<_Xp, _Yp, true>
516 {
517 static const intmax_t value = 1;
518 };
519
520 public:
521 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
522 typedef ratio<__mul<__n1, __d2, !value>::value,
523 __mul<__n2, __d1, !value>::value> type;
524 };
525
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000526public:
527 typedef _Rep rep;
Marshall Clowcf6e0db2017-03-21 18:38:57 +0000528 typedef typename _Period::type period;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000529private:
530 rep __rep_;
531public:
532
Marshall Clow832b3042013-07-31 19:32:19 +0000533 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier8c570322016-11-18 06:42:17 +0000534#ifndef _LIBCPP_CXX03_LANG
Marshall Clow832b3042013-07-31 19:32:19 +0000535 duration() = default;
Marshall Clow07774732013-07-31 19:39:37 +0000536#else
537 duration() {}
Marshall Clow832b3042013-07-31 19:32:19 +0000538#endif
539
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000540 template <class _Rep2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000541 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000542 explicit duration(const _Rep2& __r,
543 typename enable_if
544 <
545 is_convertible<_Rep2, rep>::value &&
546 (treat_as_floating_point<rep>::value ||
547 !treat_as_floating_point<_Rep2>::value)
548 >::type* = 0)
549 : __rep_(__r) {}
550
551 // conversions
552 template <class _Rep2, class _Period2>
Howard Hinnant473f8382012-07-13 19:17:27 +0000553 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554 duration(const duration<_Rep2, _Period2>& __d,
555 typename enable_if
556 <
Howard Hinnant713f4db2013-08-31 16:51:56 +0000557 __no_overflow<_Period2, period>::value && (
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000558 treat_as_floating_point<rep>::value ||
Howard Hinnant713f4db2013-08-31 16:51:56 +0000559 (__no_overflow<_Period2, period>::type::den == 1 &&
560 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000561 >::type* = 0)
Howard Hinnant0949eed2011-06-30 21:18:19 +0000562 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000563
564 // observer
565
Howard Hinnant473f8382012-07-13 19:17:27 +0000566 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000567
568 // arithmetic
569
Marshall Clowcf6e0db2017-03-21 18:38:57 +0000570 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
571 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
Marshall Clow3df90c92017-01-04 23:03:24 +0000572 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
574 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
575 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000576
Marshall Clow3df90c92017-01-04 23:03:24 +0000577 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579
Marshall Clow3df90c92017-01-04 23:03:24 +0000580 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
581 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
582 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584
585 // special values
586
Howard Hinnant473f8382012-07-13 19:17:27 +0000587 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
588 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
589 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590};
591
592typedef duration<long long, nano> nanoseconds;
593typedef duration<long long, micro> microseconds;
594typedef duration<long long, milli> milliseconds;
595typedef duration<long long > seconds;
596typedef duration< long, ratio< 60> > minutes;
597typedef duration< long, ratio<3600> > hours;
598
599// Duration ==
600
601template <class _LhsDuration, class _RhsDuration>
602struct __duration_eq
603{
Howard Hinnant473f8382012-07-13 19:17:27 +0000604 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantb9d9fb42013-06-28 18:09:35 +0000605 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000606 {
607 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
608 return _Ct(__lhs).count() == _Ct(__rhs).count();
609 }
610};
611
612template <class _LhsDuration>
613struct __duration_eq<_LhsDuration, _LhsDuration>
614{
Howard Hinnant473f8382012-07-13 19:17:27 +0000615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantb9d9fb42013-06-28 18:09:35 +0000616 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000617 {return __lhs.count() == __rhs.count();}
618};
619
620template <class _Rep1, class _Period1, class _Rep2, class _Period2>
621inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000622_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000623bool
624operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
625{
626 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
627}
628
629// Duration !=
630
631template <class _Rep1, class _Period1, class _Rep2, class _Period2>
632inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000633_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000634bool
635operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
636{
637 return !(__lhs == __rhs);
638}
639
640// Duration <
641
642template <class _LhsDuration, class _RhsDuration>
643struct __duration_lt
644{
Howard Hinnant473f8382012-07-13 19:17:27 +0000645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantb9d9fb42013-06-28 18:09:35 +0000646 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000647 {
648 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
649 return _Ct(__lhs).count() < _Ct(__rhs).count();
650 }
651};
652
653template <class _LhsDuration>
654struct __duration_lt<_LhsDuration, _LhsDuration>
655{
Howard Hinnant473f8382012-07-13 19:17:27 +0000656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantb9d9fb42013-06-28 18:09:35 +0000657 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000658 {return __lhs.count() < __rhs.count();}
659};
660
661template <class _Rep1, class _Period1, class _Rep2, class _Period2>
662inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000663_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000664bool
665operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
666{
667 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
668}
669
670// Duration >
671
672template <class _Rep1, class _Period1, class _Rep2, class _Period2>
673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000674_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000675bool
676operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
677{
678 return __rhs < __lhs;
679}
680
681// Duration <=
682
683template <class _Rep1, class _Period1, class _Rep2, class _Period2>
684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000685_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000686bool
687operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
688{
689 return !(__rhs < __lhs);
690}
691
692// Duration >=
693
694template <class _Rep1, class _Period1, class _Rep2, class _Period2>
695inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000696_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000697bool
698operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
699{
700 return !(__lhs < __rhs);
701}
702
703// Duration +
704
705template <class _Rep1, class _Period1, class _Rep2, class _Period2>
706inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000707_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000708typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
709operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
710{
Howard Hinnant473f8382012-07-13 19:17:27 +0000711 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
712 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000713}
714
715// Duration -
716
717template <class _Rep1, class _Period1, class _Rep2, class _Period2>
718inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000719_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000720typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
721operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
722{
Howard Hinnant473f8382012-07-13 19:17:27 +0000723 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
724 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000725}
726
727// Duration *
728
729template <class _Rep1, class _Period, class _Rep2>
730inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000731_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000732typename enable_if
733<
734 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
735 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
736>::type
737operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
738{
739 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnant473f8382012-07-13 19:17:27 +0000740 typedef duration<_Cr, _Period> _Cd;
741 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000742}
743
744template <class _Rep1, class _Period, class _Rep2>
745inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000746_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000747typename enable_if
748<
749 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
750 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
751>::type
752operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
753{
754 return __d * __s;
755}
756
757// Duration /
758
759template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
760struct __duration_divide_result
761{
762};
763
764template <class _Duration, class _Rep2,
765 bool = is_convertible<_Rep2,
766 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
767struct __duration_divide_imp
768{
769};
770
771template <class _Rep1, class _Period, class _Rep2>
772struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
773{
774 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
775};
776
777template <class _Rep1, class _Period, class _Rep2>
778struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
779 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
780{
781};
782
783template <class _Rep1, class _Period, class _Rep2>
784inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000785_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000786typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
787operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
788{
789 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnant473f8382012-07-13 19:17:27 +0000790 typedef duration<_Cr, _Period> _Cd;
791 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000792}
793
794template <class _Rep1, class _Period1, class _Rep2, class _Period2>
795inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000796_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000797typename common_type<_Rep1, _Rep2>::type
798operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
799{
800 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
801 return _Ct(__lhs).count() / _Ct(__rhs).count();
802}
803
804// Duration %
805
806template <class _Rep1, class _Period, class _Rep2>
807inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000808_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000809typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
810operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
811{
812 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnant473f8382012-07-13 19:17:27 +0000813 typedef duration<_Cr, _Period> _Cd;
814 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000815}
816
817template <class _Rep1, class _Period1, class _Rep2, class _Period2>
818inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27 +0000819_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000820typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
821operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
822{
Howard Hinnant473f8382012-07-13 19:17:27 +0000823 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
824 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
825 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000826}
827
828//////////////////////////////////////////////////////////
829///////////////////// time_point /////////////////////////
830//////////////////////////////////////////////////////////
831
832template <class _Clock, class _Duration = typename _Clock::duration>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000833class _LIBCPP_TEMPLATE_VIS time_point
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000834{
835 static_assert(__is_duration<_Duration>::value,
836 "Second template parameter of time_point must be a std::chrono::duration");
837public:
838 typedef _Clock clock;
839 typedef _Duration duration;
840 typedef typename duration::rep rep;
841 typedef typename duration::period period;
842private:
843 duration __d_;
844
845public:
Marshall Clow832b3042013-07-31 19:32:19 +0000846 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
847 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000848
849 // conversions
850 template <class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000851 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000852 time_point(const time_point<clock, _Duration2>& t,
853 typename enable_if
854 <
855 is_convertible<_Duration2, duration>::value
856 >::type* = 0)
857 : __d_(t.time_since_epoch()) {}
858
859 // observer
860
Marshall Clow832b3042013-07-31 19:32:19 +0000861 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000862
863 // arithmetic
864
Howard Hinnant171771a2013-07-08 21:06:38 +0000865 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
866 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000867
868 // special values
869
Howard Hinnant473f8382012-07-13 19:17:27 +0000870 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
871 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000872};
873
874} // chrono
875
876template <class _Clock, class _Duration1, class _Duration2>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000877struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000878 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000879{
880 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
881};
882
883namespace chrono {
884
885template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow832b3042013-07-31 19:32:19 +0000886inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000887time_point<_Clock, _ToDuration>
888time_point_cast(const time_point<_Clock, _Duration>& __t)
889{
Howard Hinnant0949eed2011-06-30 21:18:19 +0000890 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000891}
892
Marshall Clow223df2e2015-11-05 19:33:59 +0000893#if _LIBCPP_STD_VER > 14
894template <class _ToDuration, class _Clock, class _Duration>
895inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
896typename enable_if
897<
898 __is_duration<_ToDuration>::value,
899 time_point<_Clock, _ToDuration>
900>::type
901floor(const time_point<_Clock, _Duration>& __t)
902{
903 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
904}
905
906template <class _ToDuration, class _Clock, class _Duration>
907inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
908typename enable_if
909<
910 __is_duration<_ToDuration>::value,
911 time_point<_Clock, _ToDuration>
912>::type
913ceil(const time_point<_Clock, _Duration>& __t)
914{
915 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
916}
917
918template <class _ToDuration, class _Clock, class _Duration>
919inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
920typename enable_if
921<
922 __is_duration<_ToDuration>::value,
923 time_point<_Clock, _ToDuration>
924>::type
925round(const time_point<_Clock, _Duration>& __t)
926{
927 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
928}
929
930template <class _Rep, class _Period>
931inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
932typename enable_if
933<
934 numeric_limits<_Rep>::is_signed,
935 duration<_Rep, _Period>
936>::type
937abs(duration<_Rep, _Period> __d)
938{
939 return __d >= __d.zero() ? __d : -__d;
940}
941#endif
942
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000943// time_point ==
944
945template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000946inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000947bool
948operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
949{
950 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
951}
952
953// time_point !=
954
955template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000956inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000957bool
958operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
959{
960 return !(__lhs == __rhs);
961}
962
963// time_point <
964
965template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000966inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000967bool
968operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
969{
970 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
971}
972
973// time_point >
974
975template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000976inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000977bool
978operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
979{
980 return __rhs < __lhs;
981}
982
983// time_point <=
984
985template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000986inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000987bool
988operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
989{
990 return !(__rhs < __lhs);
991}
992
993// time_point >=
994
995template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +0000996inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000997bool
998operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
999{
1000 return !(__lhs < __rhs);
1001}
1002
1003// time_point operator+(time_point x, duration y);
1004
1005template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow832b3042013-07-31 19:32:19 +00001006inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001007time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1008operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1009{
1010 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow832b3042013-07-31 19:32:19 +00001011 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001012}
1013
1014// time_point operator+(duration x, time_point y);
1015
1016template <class _Rep1, class _Period1, class _Clock, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +00001017inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001018time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1019operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1020{
1021 return __rhs + __lhs;
1022}
1023
1024// time_point operator-(time_point x, duration y);
1025
1026template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow832b3042013-07-31 19:32:19 +00001027inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001028time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1029operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1030{
Marshall Clowdb7fa112016-11-14 18:22:19 +00001031 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1032 return _Ret(__lhs.time_since_epoch() -__rhs);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001033}
1034
1035// duration operator-(time_point x, time_point y);
1036
1037template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow832b3042013-07-31 19:32:19 +00001038inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001039typename common_type<_Duration1, _Duration2>::type
1040operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1041{
1042 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1043}
1044
1045//////////////////////////////////////////////////////////
1046/////////////////////// clocks ///////////////////////////
1047//////////////////////////////////////////////////////////
1048
Howard Hinnant83eade62013-03-06 23:30:19 +00001049class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001050{
1051public:
1052 typedef microseconds duration;
1053 typedef duration::rep rep;
1054 typedef duration::period period;
1055 typedef chrono::time_point<system_clock> time_point;
Marshall Clow832b3042013-07-31 19:32:19 +00001056 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001057
Howard Hinnant756a1762011-05-28 18:34:36 +00001058 static time_point now() _NOEXCEPT;
1059 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1060 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001061};
1062
Jonathan Roelofsc5780652014-09-02 21:14:38 +00001063#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant83eade62013-03-06 23:30:19 +00001064class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001065{
1066public:
1067 typedef nanoseconds duration;
1068 typedef duration::rep rep;
1069 typedef duration::period period;
Howard Hinnantf8f85212010-11-20 19:16:30 +00001070 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow832b3042013-07-31 19:32:19 +00001071 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001072
Howard Hinnant756a1762011-05-28 18:34:36 +00001073 static time_point now() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001074};
1075
Howard Hinnantf8f85212010-11-20 19:16:30 +00001076typedef steady_clock high_resolution_clock;
Jonathan Roelofsc5780652014-09-02 21:14:38 +00001077#else
1078typedef system_clock high_resolution_clock;
1079#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001080
1081} // chrono
1082
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00001083#if _LIBCPP_STD_VER > 11
1084// Suffixes for duration literals [time.duration.literals]
1085inline namespace literals
Marshall Clowf1820382013-07-24 21:18:14 +00001086{
1087 inline namespace chrono_literals
1088 {
1089
Marshall Clow3b53fab2017-08-09 15:42:50 +00001090 constexpr chrono::hours operator""h(unsigned long long __h)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001091 {
1092 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1093 }
Marshall Clowf1820382013-07-24 21:18:14 +00001094
Marshall Clow3b53fab2017-08-09 15:42:50 +00001095 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001096 {
1097 return chrono::duration<long double, ratio<3600,1>>(__h);
1098 }
Marshall Clowf1820382013-07-24 21:18:14 +00001099
1100
Marshall Clow3b53fab2017-08-09 15:42:50 +00001101 constexpr chrono::minutes operator""min(unsigned long long __m)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001102 {
1103 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1104 }
Marshall Clowf1820382013-07-24 21:18:14 +00001105
Marshall Clow3b53fab2017-08-09 15:42:50 +00001106 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001107 {
1108 return chrono::duration<long double, ratio<60,1>> (__m);
1109 }
Marshall Clowf1820382013-07-24 21:18:14 +00001110
1111
Marshall Clow3b53fab2017-08-09 15:42:50 +00001112 constexpr chrono::seconds operator""s(unsigned long long __s)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001113 {
1114 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1115 }
Marshall Clowf1820382013-07-24 21:18:14 +00001116
Marshall Clow3b53fab2017-08-09 15:42:50 +00001117 constexpr chrono::duration<long double> operator""s(long double __s)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001118 {
1119 return chrono::duration<long double> (__s);
1120 }
Marshall Clowf1820382013-07-24 21:18:14 +00001121
1122
Marshall Clow3b53fab2017-08-09 15:42:50 +00001123 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001124 {
1125 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1126 }
Marshall Clowf1820382013-07-24 21:18:14 +00001127
Marshall Clow3b53fab2017-08-09 15:42:50 +00001128 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001129 {
1130 return chrono::duration<long double, milli>(__ms);
1131 }
Marshall Clowf1820382013-07-24 21:18:14 +00001132
1133
Marshall Clow3b53fab2017-08-09 15:42:50 +00001134 constexpr chrono::microseconds operator""us(unsigned long long __us)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001135 {
1136 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1137 }
Marshall Clowf1820382013-07-24 21:18:14 +00001138
Marshall Clow3b53fab2017-08-09 15:42:50 +00001139 constexpr chrono::duration<long double, micro> operator""us(long double __us)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001140 {
1141 return chrono::duration<long double, micro> (__us);
1142 }
1143
Marshall Clowf1820382013-07-24 21:18:14 +00001144
Marshall Clow3b53fab2017-08-09 15:42:50 +00001145 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001146 {
1147 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1148 }
Marshall Clowf1820382013-07-24 21:18:14 +00001149
Marshall Clow3b53fab2017-08-09 15:42:50 +00001150 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
Howard Hinnantab61b2c2013-08-07 19:39:48 +00001151 {
1152 return chrono::duration<long double, nano> (__ns);
1153 }
Marshall Clowf1820382013-07-24 21:18:14 +00001154
1155}}
Marshall Clow8d9dd7a2013-10-05 21:18:32 +00001156
1157namespace chrono { // hoist the literals into namespace std::chrono
1158 using namespace literals::chrono_literals;
1159}
1160
Marshall Clowf1820382013-07-24 21:18:14 +00001161#endif
1162
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001163_LIBCPP_END_NAMESPACE_STD
1164
Eric Fiselier018a3d52017-05-31 22:07:49 +00001165_LIBCPP_POP_MACROS
1166
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001167#endif // _LIBCPP_CHRONO