blob: 809f773b6c6b14801f0932ed9d0043fbfa287664 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +000023constexpr
Howard Hinnantc51e1022010-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 Clowf1bf62f2018-01-02 17:17:01 +000029template <class Rep> inline constexpr bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +000030 = treat_as_floating_point<Rep>::value; // C++17
31
Howard Hinnantc51e1022010-05-11 19:42:16 +000032template <class Rep>
33struct duration_values
34{
35public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +000036 static constexpr Rep zero();
37 static constexpr Rep max();
38 static constexpr Rep min();
Howard Hinnantc51e1022010-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 Clow76200b22017-03-21 18:38:57 +000051 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +000052
Howard Hinnantcf3143c2012-07-13 19:17:27 +000053 constexpr duration() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +000054 template <class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000055 constexpr explicit duration(const Rep2& r,
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +000065 constexpr duration(const duration<Rep2, Period2>& d,
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +000074 constexpr rep count() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000075
76 // arithmetic
77
Marshall Clow76200b22017-03-21 18:38:57 +000078 constexpr common_type<duration>::type operator+() const;
79 constexpr common_type<duration>::type operator-() const;
Marshall Clow5c87ad02017-01-04 23:03:24 +000080 constexpr duration& operator++();
81 constexpr duration operator++(int);
82 constexpr duration& operator--();
83 constexpr duration operator--(int);
Howard Hinnantc51e1022010-05-11 19:42:16 +000084
Marshall Clow5c87ad02017-01-04 23:03:24 +000085 constexpr duration& operator+=(const duration& d);
86 constexpr duration& operator-=(const duration& d);
Howard Hinnantc51e1022010-05-11 19:42:16 +000087
88 duration& operator*=(const rep& rhs);
89 duration& operator/=(const rep& rhs);
90
91 // special values
92
Howard Hinnantcf3143c2012-07-13 19:17:27 +000093 static constexpr duration zero();
94 static constexpr duration min();
95 static constexpr duration max();
Howard Hinnantc51e1022010-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 Clow5c459c92013-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 Hinnantc51e1022010-05-11 19:42:16 +0000119
120 // conversions
121 template <class Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000122 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124 // observer
125
Marshall Clow5c459c92013-07-31 19:32:19 +0000126 duration time_since_epoch() const; // constexpr in C++14
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000152 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000156 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000160 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000164 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000168 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000172 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000178 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000181 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000184 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000187 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000190 constexpr
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000193 constexpr
Howard Hinnantc51e1022010-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 Clowf2eabaf2015-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 Clow5c459c92013-07-31 19:32:19 +0000207// time_point arithmetic (all constexpr in C++14)
Howard Hinnantc51e1022010-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 Clow5c459c92013-07-31 19:32:19 +0000221// time_point comparisons (all constexpr in C++14)
Howard Hinnantc51e1022010-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 Clow5c459c92013-07-31 19:32:19 +0000235// time_point_cast (constexpr in C++14)
Howard Hinnantc51e1022010-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 Clowf2eabaf2015-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 Hinnantc51e1022010-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 Clow5c459c92013-07-31 19:32:19 +0000263 static const bool is_steady = false; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000264
Howard Hinnantaa54ac42011-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 Hinnantc51e1022010-05-11 19:42:16 +0000268};
269
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000270class steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000271{
272public:
273 typedef nanoseconds duration;
274 typedef duration::rep rep;
275 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000276 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000277 static const bool is_steady = true; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000278
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000279 static time_point now() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280};
281
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000282typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
284} // chrono
285
Marshall Clow2ac805a2017-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 Cloweb41e5c2013-07-24 21:18:14 +0000298
Howard Hinnantc51e1022010-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 Hinnantaaaa52b2011-10-17 20:05:10 +0000308#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000309#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000310#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311
Eric Fiselierf4433a32017-05-31 22:07:49 +0000312_LIBCPP_PUSH_MACROS
313#include <__undef_macros>
314
315
Howard Hinnantc51e1022010-05-11 19:42:16 +0000316_LIBCPP_BEGIN_NAMESPACE_STD
317
318namespace chrono
319{
320
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000321template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000322
Howard Hinnantf39463b2010-05-18 17:32:30 +0000323template <class _Tp>
Howard Hinnantc51e1022010-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 Fiselierb5eb1bf2017-01-04 23:56:00 +0000341struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000342 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000371 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000383 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-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 Hinnantcf3143c2012-07-13 19:17:27 +0000407_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-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 Hinnant874ad9a2010-09-21 21:28:23 +0000418template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000419struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420
Marshall Clow59581f02015-11-30 05:39:30 +0000421#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000422template <class _Rep>
423_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
Marshall Clow59581f02015-11-30 05:39:30 +0000424 = treat_as_floating_point<_Rep>::value;
425#endif
426
Howard Hinnantc51e1022010-05-11 19:42:16 +0000427template <class _Rep>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000428struct _LIBCPP_TEMPLATE_VIS duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429{
430public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000431 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
432 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
433 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000434};
435
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000436#if _LIBCPP_STD_VER > 14
437template <class _ToDuration, class _Rep, class _Period>
438inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
439typename enable_if
440<
441 __is_duration<_ToDuration>::value,
442 _ToDuration
443>::type
444floor(const duration<_Rep, _Period>& __d)
445{
446 _ToDuration __t = duration_cast<_ToDuration>(__d);
447 if (__t > __d)
448 __t = __t - _ToDuration{1};
449 return __t;
450}
451
452template <class _ToDuration, class _Rep, class _Period>
453inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
454typename enable_if
455<
456 __is_duration<_ToDuration>::value,
457 _ToDuration
458>::type
459ceil(const duration<_Rep, _Period>& __d)
460{
461 _ToDuration __t = duration_cast<_ToDuration>(__d);
462 if (__t < __d)
463 __t = __t + _ToDuration{1};
464 return __t;
465}
466
467template <class _ToDuration, class _Rep, class _Period>
468inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
469typename enable_if
470<
471 __is_duration<_ToDuration>::value,
472 _ToDuration
473>::type
474round(const duration<_Rep, _Period>& __d)
475{
476 _ToDuration __lower = floor<_ToDuration>(__d);
477 _ToDuration __upper = __lower + _ToDuration{1};
478 auto __lowerDiff = __d - __lower;
479 auto __upperDiff = __upper - __d;
480 if (__lowerDiff < __upperDiff)
481 return __lower;
482 if (__lowerDiff > __upperDiff)
483 return __upper;
484 return __lower.count() & 1 ? __upper : __lower;
485}
486#endif
487
Howard Hinnantc51e1022010-05-11 19:42:16 +0000488// duration
489
490template <class _Rep, class _Period>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000491class _LIBCPP_TEMPLATE_VIS duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000492{
493 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
494 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
495 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant0a51e152013-08-31 16:51:56 +0000496
497 template <class _R1, class _R2>
498 struct __no_overflow
499 {
500 private:
501 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
502 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
503 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
504 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
505 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
506 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
507 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
508
509 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
510 struct __mul // __overflow == false
511 {
512 static const intmax_t value = _Xp * _Yp;
513 };
514
515 template <intmax_t _Xp, intmax_t _Yp>
516 struct __mul<_Xp, _Yp, true>
517 {
518 static const intmax_t value = 1;
519 };
520
521 public:
522 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
523 typedef ratio<__mul<__n1, __d2, !value>::value,
524 __mul<__n2, __d1, !value>::value> type;
525 };
526
Howard Hinnantc51e1022010-05-11 19:42:16 +0000527public:
528 typedef _Rep rep;
Marshall Clow76200b22017-03-21 18:38:57 +0000529 typedef typename _Period::type period;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530private:
531 rep __rep_;
532public:
533
Marshall Clow5c459c92013-07-31 19:32:19 +0000534 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Eric Fiselier07b2b552016-11-18 06:42:17 +0000535#ifndef _LIBCPP_CXX03_LANG
Marshall Clow5c459c92013-07-31 19:32:19 +0000536 duration() = default;
Marshall Clow3dbcf132013-07-31 19:39:37 +0000537#else
538 duration() {}
Marshall Clow5c459c92013-07-31 19:32:19 +0000539#endif
540
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000542 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 explicit duration(const _Rep2& __r,
544 typename enable_if
545 <
546 is_convertible<_Rep2, rep>::value &&
547 (treat_as_floating_point<rep>::value ||
548 !treat_as_floating_point<_Rep2>::value)
549 >::type* = 0)
550 : __rep_(__r) {}
551
552 // conversions
553 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000554 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555 duration(const duration<_Rep2, _Period2>& __d,
556 typename enable_if
557 <
Howard Hinnant0a51e152013-08-31 16:51:56 +0000558 __no_overflow<_Period2, period>::value && (
Howard Hinnantc51e1022010-05-11 19:42:16 +0000559 treat_as_floating_point<rep>::value ||
Howard Hinnant0a51e152013-08-31 16:51:56 +0000560 (__no_overflow<_Period2, period>::type::den == 1 &&
561 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000563 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000564
565 // observer
566
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000568
569 // arithmetic
570
Marshall Clow76200b22017-03-21 18:38:57 +0000571 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
572 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
Marshall Clow5c87ad02017-01-04 23:03:24 +0000573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
574 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
575 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
576 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000577
Marshall Clow5c87ad02017-01-04 23:03:24 +0000578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
579 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580
Marshall Clow5c87ad02017-01-04 23:03:24 +0000581 _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 rep& rhs) {__rep_ %= rhs; return *this;}
584 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000585
586 // special values
587
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000588 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
589 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
590 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591};
592
593typedef duration<long long, nano> nanoseconds;
594typedef duration<long long, micro> microseconds;
595typedef duration<long long, milli> milliseconds;
596typedef duration<long long > seconds;
597typedef duration< long, ratio< 60> > minutes;
598typedef duration< long, ratio<3600> > hours;
599
600// Duration ==
601
602template <class _LhsDuration, class _RhsDuration>
603struct __duration_eq
604{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000605 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000606 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 {
608 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
609 return _Ct(__lhs).count() == _Ct(__rhs).count();
610 }
611};
612
613template <class _LhsDuration>
614struct __duration_eq<_LhsDuration, _LhsDuration>
615{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000616 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000617 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000618 {return __lhs.count() == __rhs.count();}
619};
620
621template <class _Rep1, class _Period1, class _Rep2, class _Period2>
622inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000623_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624bool
625operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
626{
627 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
628}
629
630// Duration !=
631
632template <class _Rep1, class _Period1, class _Rep2, class _Period2>
633inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000634_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000635bool
636operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
637{
638 return !(__lhs == __rhs);
639}
640
641// Duration <
642
643template <class _LhsDuration, class _RhsDuration>
644struct __duration_lt
645{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000647 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 {
649 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
650 return _Ct(__lhs).count() < _Ct(__rhs).count();
651 }
652};
653
654template <class _LhsDuration>
655struct __duration_lt<_LhsDuration, _LhsDuration>
656{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000657 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000658 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000659 {return __lhs.count() < __rhs.count();}
660};
661
662template <class _Rep1, class _Period1, class _Rep2, class _Period2>
663inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000664_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665bool
666operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
667{
668 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
669}
670
671// Duration >
672
673template <class _Rep1, class _Period1, class _Rep2, class _Period2>
674inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000675_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676bool
677operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
678{
679 return __rhs < __lhs;
680}
681
682// Duration <=
683
684template <class _Rep1, class _Period1, class _Rep2, class _Period2>
685inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000686_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000687bool
688operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
689{
690 return !(__rhs < __lhs);
691}
692
693// Duration >=
694
695template <class _Rep1, class _Period1, class _Rep2, class _Period2>
696inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000697_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000698bool
699operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
700{
701 return !(__lhs < __rhs);
702}
703
704// Duration +
705
706template <class _Rep1, class _Period1, class _Rep2, class _Period2>
707inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000708_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000709typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
710operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
711{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000712 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
713 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714}
715
716// Duration -
717
718template <class _Rep1, class _Period1, class _Rep2, class _Period2>
719inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000720_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
722operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
723{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000724 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
725 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726}
727
728// Duration *
729
730template <class _Rep1, class _Period, class _Rep2>
731inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000732_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733typename enable_if
734<
735 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
736 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
737>::type
738operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
739{
740 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000741 typedef duration<_Cr, _Period> _Cd;
742 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000743}
744
745template <class _Rep1, class _Period, class _Rep2>
746inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000747_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000748typename enable_if
749<
750 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
751 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
752>::type
753operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
754{
755 return __d * __s;
756}
757
758// Duration /
759
760template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
761struct __duration_divide_result
762{
763};
764
765template <class _Duration, class _Rep2,
766 bool = is_convertible<_Rep2,
767 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
768struct __duration_divide_imp
769{
770};
771
772template <class _Rep1, class _Period, class _Rep2>
773struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
774{
775 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
776};
777
778template <class _Rep1, class _Period, class _Rep2>
779struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
780 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
781{
782};
783
784template <class _Rep1, class _Period, class _Rep2>
785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000786_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
788operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
789{
790 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000791 typedef duration<_Cr, _Period> _Cd;
792 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000793}
794
795template <class _Rep1, class _Period1, class _Rep2, class _Period2>
796inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000797_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000798typename common_type<_Rep1, _Rep2>::type
799operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
800{
801 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
802 return _Ct(__lhs).count() / _Ct(__rhs).count();
803}
804
805// Duration %
806
807template <class _Rep1, class _Period, class _Rep2>
808inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000809_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
811operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
812{
813 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000814 typedef duration<_Cr, _Period> _Cd;
815 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816}
817
818template <class _Rep1, class _Period1, class _Rep2, class _Period2>
819inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000820_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000821typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
822operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
823{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000824 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
825 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
826 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827}
828
829//////////////////////////////////////////////////////////
830///////////////////// time_point /////////////////////////
831//////////////////////////////////////////////////////////
832
833template <class _Clock, class _Duration = typename _Clock::duration>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000834class _LIBCPP_TEMPLATE_VIS time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +0000835{
836 static_assert(__is_duration<_Duration>::value,
837 "Second template parameter of time_point must be a std::chrono::duration");
838public:
839 typedef _Clock clock;
840 typedef _Duration duration;
841 typedef typename duration::rep rep;
842 typedef typename duration::period period;
843private:
844 duration __d_;
845
846public:
Marshall Clow5c459c92013-07-31 19:32:19 +0000847 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
848 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000849
850 // conversions
851 template <class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000852 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000853 time_point(const time_point<clock, _Duration2>& t,
854 typename enable_if
855 <
856 is_convertible<_Duration2, duration>::value
857 >::type* = 0)
858 : __d_(t.time_since_epoch()) {}
859
860 // observer
861
Marshall Clow5c459c92013-07-31 19:32:19 +0000862 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000863
864 // arithmetic
865
Howard Hinnantecc7ba92013-07-08 21:06:38 +0000866 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
867 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868
869 // special values
870
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000871 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
872 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000873};
874
875} // chrono
876
877template <class _Clock, class _Duration1, class _Duration2>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000878struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000879 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000880{
881 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
882};
883
884namespace chrono {
885
886template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow5c459c92013-07-31 19:32:19 +0000887inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888time_point<_Clock, _ToDuration>
889time_point_cast(const time_point<_Clock, _Duration>& __t)
890{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000891 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000892}
893
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000894#if _LIBCPP_STD_VER > 14
895template <class _ToDuration, class _Clock, class _Duration>
896inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
897typename enable_if
898<
899 __is_duration<_ToDuration>::value,
900 time_point<_Clock, _ToDuration>
901>::type
902floor(const time_point<_Clock, _Duration>& __t)
903{
904 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
905}
906
907template <class _ToDuration, class _Clock, class _Duration>
908inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
909typename enable_if
910<
911 __is_duration<_ToDuration>::value,
912 time_point<_Clock, _ToDuration>
913>::type
914ceil(const time_point<_Clock, _Duration>& __t)
915{
916 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
917}
918
919template <class _ToDuration, class _Clock, class _Duration>
920inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
921typename enable_if
922<
923 __is_duration<_ToDuration>::value,
924 time_point<_Clock, _ToDuration>
925>::type
926round(const time_point<_Clock, _Duration>& __t)
927{
928 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
929}
930
931template <class _Rep, class _Period>
932inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
933typename enable_if
934<
935 numeric_limits<_Rep>::is_signed,
936 duration<_Rep, _Period>
937>::type
938abs(duration<_Rep, _Period> __d)
939{
940 return __d >= __d.zero() ? __d : -__d;
941}
942#endif
943
Howard Hinnantc51e1022010-05-11 19:42:16 +0000944// time_point ==
945
946template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000947inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000948bool
949operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
950{
951 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
952}
953
954// time_point !=
955
956template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000957inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000958bool
959operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
960{
961 return !(__lhs == __rhs);
962}
963
964// time_point <
965
966template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000967inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000968bool
969operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
970{
971 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
972}
973
974// time_point >
975
976template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000977inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000978bool
979operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
980{
981 return __rhs < __lhs;
982}
983
984// time_point <=
985
986template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000987inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000988bool
989operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
990{
991 return !(__rhs < __lhs);
992}
993
994// time_point >=
995
996template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000997inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998bool
999operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1000{
1001 return !(__lhs < __rhs);
1002}
1003
1004// time_point operator+(time_point x, duration y);
1005
1006template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001007inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001008time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1009operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1010{
1011 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow5c459c92013-07-31 19:32:19 +00001012 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013}
1014
1015// time_point operator+(duration x, time_point y);
1016
1017template <class _Rep1, class _Period1, class _Clock, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001018inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001019time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1020operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1021{
1022 return __rhs + __lhs;
1023}
1024
1025// time_point operator-(time_point x, duration y);
1026
1027template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001028inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001029time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1030operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1031{
Marshall Clow78dbe462016-11-14 18:22:19 +00001032 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1033 return _Ret(__lhs.time_since_epoch() -__rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001034}
1035
1036// duration operator-(time_point x, time_point y);
1037
1038template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001039inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001040typename common_type<_Duration1, _Duration2>::type
1041operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1042{
1043 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1044}
1045
1046//////////////////////////////////////////////////////////
1047/////////////////////// clocks ///////////////////////////
1048//////////////////////////////////////////////////////////
1049
Howard Hinnant8331b762013-03-06 23:30:19 +00001050class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001051{
1052public:
1053 typedef microseconds duration;
1054 typedef duration::rep rep;
1055 typedef duration::period period;
1056 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001057 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001058
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001059 static time_point now() _NOEXCEPT;
1060 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1061 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001062};
1063
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001064#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant8331b762013-03-06 23:30:19 +00001065class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066{
1067public:
1068 typedef nanoseconds duration;
1069 typedef duration::rep rep;
1070 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001071 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001072 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001074 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075};
1076
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001077typedef steady_clock high_resolution_clock;
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001078#else
1079typedef system_clock high_resolution_clock;
1080#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001081
1082} // chrono
1083
Marshall Clowac868372013-10-05 21:18:32 +00001084#if _LIBCPP_STD_VER > 11
1085// Suffixes for duration literals [time.duration.literals]
1086inline namespace literals
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001087{
1088 inline namespace chrono_literals
1089 {
1090
Marshall Clow2ac805a2017-08-09 15:42:50 +00001091 constexpr chrono::hours operator""h(unsigned long long __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001092 {
1093 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1094 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001095
Marshall Clow2ac805a2017-08-09 15:42:50 +00001096 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
Howard Hinnant5c167562013-08-07 19:39:48 +00001097 {
1098 return chrono::duration<long double, ratio<3600,1>>(__h);
1099 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001100
1101
Marshall Clow2ac805a2017-08-09 15:42:50 +00001102 constexpr chrono::minutes operator""min(unsigned long long __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001103 {
1104 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1105 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001106
Marshall Clow2ac805a2017-08-09 15:42:50 +00001107 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
Howard Hinnant5c167562013-08-07 19:39:48 +00001108 {
1109 return chrono::duration<long double, ratio<60,1>> (__m);
1110 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001111
1112
Marshall Clow2ac805a2017-08-09 15:42:50 +00001113 constexpr chrono::seconds operator""s(unsigned long long __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001114 {
1115 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1116 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001117
Marshall Clow2ac805a2017-08-09 15:42:50 +00001118 constexpr chrono::duration<long double> operator""s(long double __s)
Howard Hinnant5c167562013-08-07 19:39:48 +00001119 {
1120 return chrono::duration<long double> (__s);
1121 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001122
1123
Marshall Clow2ac805a2017-08-09 15:42:50 +00001124 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001125 {
1126 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1127 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001128
Marshall Clow2ac805a2017-08-09 15:42:50 +00001129 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
Howard Hinnant5c167562013-08-07 19:39:48 +00001130 {
1131 return chrono::duration<long double, milli>(__ms);
1132 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001133
1134
Marshall Clow2ac805a2017-08-09 15:42:50 +00001135 constexpr chrono::microseconds operator""us(unsigned long long __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001136 {
1137 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1138 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001139
Marshall Clow2ac805a2017-08-09 15:42:50 +00001140 constexpr chrono::duration<long double, micro> operator""us(long double __us)
Howard Hinnant5c167562013-08-07 19:39:48 +00001141 {
1142 return chrono::duration<long double, micro> (__us);
1143 }
1144
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001145
Marshall Clow2ac805a2017-08-09 15:42:50 +00001146 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001147 {
1148 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1149 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001150
Marshall Clow2ac805a2017-08-09 15:42:50 +00001151 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
Howard Hinnant5c167562013-08-07 19:39:48 +00001152 {
1153 return chrono::duration<long double, nano> (__ns);
1154 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001155
1156}}
Marshall Clowac868372013-10-05 21:18:32 +00001157
1158namespace chrono { // hoist the literals into namespace std::chrono
1159 using namespace literals::chrono_literals;
1160}
1161
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001162#endif
1163
Howard Hinnantc51e1022010-05-11 19:42:16 +00001164_LIBCPP_END_NAMESPACE_STD
1165
Eric Fiselierf4433a32017-05-31 22:07:49 +00001166_LIBCPP_POP_MACROS
1167
Howard Hinnantc51e1022010-05-11 19:42:16 +00001168#endif // _LIBCPP_CHRONO