blob: aac05870f5540f58e67f516c7a0cd12a7bb28c2e [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
29template <class Rep>
30struct duration_values
31{
32public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +000033 static constexpr Rep zero();
34 static constexpr Rep max();
35 static constexpr Rep min();
Howard Hinnantc51e1022010-05-11 19:42:16 +000036};
37
38// duration
39
40template <class Rep, class Period = ratio<1>>
41class duration
42{
43 static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration");
44 static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio");
45 static_assert(Period::num > 0, "duration period must be positive");
46public:
47 typedef Rep rep;
48 typedef Period period;
49
Howard Hinnantcf3143c2012-07-13 19:17:27 +000050 constexpr duration() = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +000051 template <class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000052 constexpr explicit duration(const Rep2& r,
Howard Hinnantc51e1022010-05-11 19:42:16 +000053 typename enable_if
54 <
55 is_convertible<Rep2, rep>::value &&
56 (treat_as_floating_point<rep>::value ||
57 !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value)
58 >::type* = 0);
59
60 // conversions
61 template <class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +000062 constexpr duration(const duration<Rep2, Period2>& d,
Howard Hinnantc51e1022010-05-11 19:42:16 +000063 typename enable_if
64 <
65 treat_as_floating_point<rep>::value ||
66 ratio_divide<Period2, period>::type::den == 1
67 >::type* = 0);
68
69 // observer
70
Howard Hinnantcf3143c2012-07-13 19:17:27 +000071 constexpr rep count() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000072
73 // arithmetic
74
Howard Hinnantcf3143c2012-07-13 19:17:27 +000075 constexpr duration operator+() const;
76 constexpr duration operator-() const;
Howard Hinnantc51e1022010-05-11 19:42:16 +000077 duration& operator++();
78 duration operator++(int);
79 duration& operator--();
80 duration operator--(int);
81
82 duration& operator+=(const duration& d);
83 duration& operator-=(const duration& d);
84
85 duration& operator*=(const rep& rhs);
86 duration& operator/=(const rep& rhs);
87
88 // special values
89
Howard Hinnantcf3143c2012-07-13 19:17:27 +000090 static constexpr duration zero();
91 static constexpr duration min();
92 static constexpr duration max();
Howard Hinnantc51e1022010-05-11 19:42:16 +000093};
94
95typedef duration<long long, nano> nanoseconds;
96typedef duration<long long, micro> microseconds;
97typedef duration<long long, milli> milliseconds;
98typedef duration<long long > seconds;
99typedef duration< long, ratio< 60> > minutes;
100typedef duration< long, ratio<3600> > hours;
101
102template <class Clock, class Duration = typename Clock::duration>
103class time_point
104{
105public:
106 typedef Clock clock;
107 typedef Duration duration;
108 typedef typename duration::rep rep;
109 typedef typename duration::period period;
110private:
111 duration d_; // exposition only
112
113public:
Marshall Clow5c459c92013-07-31 19:32:19 +0000114 time_point(); // has value "epoch" // constexpr in C++14
115 explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
117 // conversions
118 template <class Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000119 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120
121 // observer
122
Marshall Clow5c459c92013-07-31 19:32:19 +0000123 duration time_since_epoch() const; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000124
125 // arithmetic
126
127 time_point& operator+=(const duration& d);
128 time_point& operator-=(const duration& d);
129
130 // special values
131
132 static constexpr time_point min();
133 static constexpr time_point max();
134};
135
136} // chrono
137
138// common_type traits
139template <class Rep1, class Period1, class Rep2, class Period2>
140 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
141
142template <class Clock, class Duration1, class Duration2>
143 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
144
145namespace chrono {
146
147// duration arithmetic
148template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000149 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
151 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
152template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000153 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
155 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
156template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000157 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000158 duration<typename common_type<Rep1, Rep2>::type, Period>
159 operator*(const duration<Rep1, Period>& d, const Rep2& s);
160template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000161 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000162 duration<typename common_type<Rep1, Rep2>::type, Period>
163 operator*(const Rep1& s, const duration<Rep2, Period>& d);
164template <class Rep1, class Period, class Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000165 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 duration<typename common_type<Rep1, Rep2>::type, Period>
167 operator/(const duration<Rep1, Period>& d, const Rep2& s);
168template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000169 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 typename common_type<Rep1, Rep2>::type
171 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
172
173// duration comparisons
174template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000175 constexpr
Howard Hinnantc51e1022010-05-11 19:42:16 +0000176 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
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);
192
193// duration_cast
194template <class ToDuration, class Rep, class Period>
195 ToDuration duration_cast(const duration<Rep, Period>& d);
196
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000197template <class ToDuration, class Rep, class Period>
198 constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17
199template <class ToDuration, class Rep, class Period>
200 constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17
201template <class ToDuration, class Rep, class Period>
202 constexpr ToDuration round(const duration<Rep, Period>& d); // C++17
203
Marshall Clow5c459c92013-07-31 19:32:19 +0000204// time_point arithmetic (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205template <class Clock, class Duration1, class Rep2, class Period2>
206 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
207 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
208template <class Rep1, class Period1, class Clock, class Duration2>
209 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
210 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
211template <class Clock, class Duration1, class Rep2, class Period2>
212 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
213 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
214template <class Clock, class Duration1, class Duration2>
215 typename common_type<Duration1, Duration2>::type
216 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
217
Marshall Clow5c459c92013-07-31 19:32:19 +0000218// time_point comparisons (all constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219template <class Clock, class Duration1, class Duration2>
220 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
221template <class Clock, class Duration1, class Duration2>
222 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
223template <class Clock, class Duration1, class Duration2>
224 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
225template <class Clock, class Duration1, class Duration2>
226 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
227template <class Clock, class Duration1, class Duration2>
228 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
229template <class Clock, class Duration1, class Duration2>
230 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
231
Marshall Clow5c459c92013-07-31 19:32:19 +0000232// time_point_cast (constexpr in C++14)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233
234template <class ToDuration, class Clock, class Duration>
235 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
236
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000237template <class ToDuration, class Clock, class Duration>
238 constexpr time_point<Clock, ToDuration>
239 floor(const time_point<Clock, Duration>& tp); // C++17
240
241template <class ToDuration, class Clock, class Duration>
242 constexpr time_point<Clock, ToDuration>
243 ceil(const time_point<Clock, Duration>& tp); // C++17
244
245template <class ToDuration, class Clock, class Duration>
246 constexpr time_point<Clock, ToDuration>
247 round(const time_point<Clock, Duration>& tp); // C++17
248
249template <class Rep, class Period>
250 constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251// Clocks
252
253class system_clock
254{
255public:
256 typedef microseconds duration;
257 typedef duration::rep rep;
258 typedef duration::period period;
259 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000260 static const bool is_steady = false; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000262 static time_point now() noexcept;
263 static time_t to_time_t (const time_point& __t) noexcept;
264 static time_point from_time_t(time_t __t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000265};
266
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000267class steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000268{
269public:
270 typedef nanoseconds duration;
271 typedef duration::rep rep;
272 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000273 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +0000274 static const bool is_steady = true; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000275
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000276 static time_point now() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277};
278
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000279typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280
281} // chrono
282
Marshall Cloweb41e5c2013-07-24 21:18:14 +0000283constexpr chrono::hours operator "" h(unsigned long long); // C++14
284constexpr chrono::duration<unspecified , ratio<3600,1>> operator "" h(long double); // C++14
285constexpr chrono::minutes operator "" min(unsigned long long); // C++14
286constexpr chrono::duration<unspecified , ratio<60,1>> operator "" min(long double); // C++14
287constexpr chrono::seconds operator "" s(unsigned long long); // C++14
288constexpr chrono::duration<unspecified > operator "" s(long double); // C++14
289constexpr chrono::milliseconds operator "" ms(unsigned long long); // C++14
290constexpr chrono::duration<unspecified , milli> operator "" ms(long double); // C++14
291constexpr chrono::microseconds operator "" us(unsigned long long); // C++14
292constexpr chrono::duration<unspecified , micro> operator "" us(long double); // C++14
293constexpr chrono::nanoseconds operator "" ns(unsigned long long); // C++14
294constexpr chrono::duration<unspecified , nano> operator "" ns(long double); // C++14
295
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296} // std
297*/
298
299#include <__config>
300#include <ctime>
301#include <type_traits>
302#include <ratio>
303#include <limits>
304
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000305#include <__undef_min_max>
306
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000307#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000308#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000309#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000310
311_LIBCPP_BEGIN_NAMESPACE_STD
312
313namespace chrono
314{
315
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000316template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS_ONLY duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000317
Howard Hinnantf39463b2010-05-18 17:32:30 +0000318template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000319struct __is_duration : false_type {};
320
321template <class _Rep, class _Period>
322struct __is_duration<duration<_Rep, _Period> > : true_type {};
323
324template <class _Rep, class _Period>
325struct __is_duration<const duration<_Rep, _Period> > : true_type {};
326
327template <class _Rep, class _Period>
328struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
329
330template <class _Rep, class _Period>
331struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
332
333} // chrono
334
335template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000336struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::duration<_Rep1, _Period1>,
337 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000338{
339 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
340 typename __ratio_gcd<_Period1, _Period2>::type> type;
341};
342
343namespace chrono {
344
345// duration_cast
346
347template <class _FromDuration, class _ToDuration,
348 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
349 bool = _Period::num == 1,
350 bool = _Period::den == 1>
351struct __duration_cast;
352
353template <class _FromDuration, class _ToDuration, class _Period>
354struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
355{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 _ToDuration operator()(const _FromDuration& __fd) const
358 {
359 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
360 }
361};
362
363template <class _FromDuration, class _ToDuration, class _Period>
364struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
365{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000366 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000367 _ToDuration operator()(const _FromDuration& __fd) const
368 {
369 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
370 return _ToDuration(static_cast<typename _ToDuration::rep>(
371 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
372 }
373};
374
375template <class _FromDuration, class _ToDuration, class _Period>
376struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
377{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000378 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 _ToDuration operator()(const _FromDuration& __fd) const
380 {
381 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
382 return _ToDuration(static_cast<typename _ToDuration::rep>(
383 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
384 }
385};
386
387template <class _FromDuration, class _ToDuration, class _Period>
388struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
389{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000390 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000391 _ToDuration operator()(const _FromDuration& __fd) const
392 {
393 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
394 return _ToDuration(static_cast<typename _ToDuration::rep>(
395 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
396 / static_cast<_Ct>(_Period::den)));
397 }
398};
399
400template <class _ToDuration, class _Rep, class _Period>
401inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000402_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403typename enable_if
404<
405 __is_duration<_ToDuration>::value,
406 _ToDuration
407>::type
408duration_cast(const duration<_Rep, _Period>& __fd)
409{
410 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
411}
412
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000413template <class _Rep>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000414struct _LIBCPP_TYPE_VIS_ONLY treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000415
416template <class _Rep>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000417struct _LIBCPP_TYPE_VIS_ONLY duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000418{
419public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000420 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
421 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
422 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000423};
424
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000425#if _LIBCPP_STD_VER > 14
426template <class _ToDuration, class _Rep, class _Period>
427inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
428typename enable_if
429<
430 __is_duration<_ToDuration>::value,
431 _ToDuration
432>::type
433floor(const duration<_Rep, _Period>& __d)
434{
435 _ToDuration __t = duration_cast<_ToDuration>(__d);
436 if (__t > __d)
437 __t = __t - _ToDuration{1};
438 return __t;
439}
440
441template <class _ToDuration, class _Rep, class _Period>
442inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
443typename enable_if
444<
445 __is_duration<_ToDuration>::value,
446 _ToDuration
447>::type
448ceil(const duration<_Rep, _Period>& __d)
449{
450 _ToDuration __t = duration_cast<_ToDuration>(__d);
451 if (__t < __d)
452 __t = __t + _ToDuration{1};
453 return __t;
454}
455
456template <class _ToDuration, class _Rep, class _Period>
457inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
458typename enable_if
459<
460 __is_duration<_ToDuration>::value,
461 _ToDuration
462>::type
463round(const duration<_Rep, _Period>& __d)
464{
465 _ToDuration __lower = floor<_ToDuration>(__d);
466 _ToDuration __upper = __lower + _ToDuration{1};
467 auto __lowerDiff = __d - __lower;
468 auto __upperDiff = __upper - __d;
469 if (__lowerDiff < __upperDiff)
470 return __lower;
471 if (__lowerDiff > __upperDiff)
472 return __upper;
473 return __lower.count() & 1 ? __upper : __lower;
474}
475#endif
476
Howard Hinnantc51e1022010-05-11 19:42:16 +0000477// duration
478
479template <class _Rep, class _Period>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000480class _LIBCPP_TYPE_VIS_ONLY duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481{
482 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
483 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
484 static_assert(_Period::num > 0, "duration period must be positive");
Howard Hinnant0a51e152013-08-31 16:51:56 +0000485
486 template <class _R1, class _R2>
487 struct __no_overflow
488 {
489 private:
490 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
491 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
492 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
493 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
494 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
495 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
496 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
497
498 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
499 struct __mul // __overflow == false
500 {
501 static const intmax_t value = _Xp * _Yp;
502 };
503
504 template <intmax_t _Xp, intmax_t _Yp>
505 struct __mul<_Xp, _Yp, true>
506 {
507 static const intmax_t value = 1;
508 };
509
510 public:
511 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
512 typedef ratio<__mul<__n1, __d2, !value>::value,
513 __mul<__n2, __d1, !value>::value> type;
514 };
515
Howard Hinnantc51e1022010-05-11 19:42:16 +0000516public:
517 typedef _Rep rep;
518 typedef _Period period;
519private:
520 rep __rep_;
521public:
522
Marshall Clow5c459c92013-07-31 19:32:19 +0000523 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
524#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
Marshall Clow5c459c92013-07-31 19:32:19 +0000525 duration() = default;
Marshall Clow3dbcf132013-07-31 19:39:37 +0000526#else
527 duration() {}
Marshall Clow5c459c92013-07-31 19:32:19 +0000528#endif
529
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000531 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 explicit duration(const _Rep2& __r,
533 typename enable_if
534 <
535 is_convertible<_Rep2, rep>::value &&
536 (treat_as_floating_point<rep>::value ||
537 !treat_as_floating_point<_Rep2>::value)
538 >::type* = 0)
539 : __rep_(__r) {}
540
541 // conversions
542 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000543 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000544 duration(const duration<_Rep2, _Period2>& __d,
545 typename enable_if
546 <
Howard Hinnant0a51e152013-08-31 16:51:56 +0000547 __no_overflow<_Period2, period>::value && (
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548 treat_as_floating_point<rep>::value ||
Howard Hinnant0a51e152013-08-31 16:51:56 +0000549 (__no_overflow<_Period2, period>::type::den == 1 &&
550 !treat_as_floating_point<_Rep2>::value))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000552 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000553
554 // observer
555
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000556 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557
558 // arithmetic
559
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000560 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;}
561 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;}
563 _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);}
564 _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;}
565 _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);}
566
567 _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
568 _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
569
570 _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
571 _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
572 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
573 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
574
575 // special values
576
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000577 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
578 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
579 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000580};
581
582typedef duration<long long, nano> nanoseconds;
583typedef duration<long long, micro> microseconds;
584typedef duration<long long, milli> milliseconds;
585typedef duration<long long > seconds;
586typedef duration< long, ratio< 60> > minutes;
587typedef duration< long, ratio<3600> > hours;
588
589// Duration ==
590
591template <class _LhsDuration, class _RhsDuration>
592struct __duration_eq
593{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000594 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000595 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000596 {
597 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
598 return _Ct(__lhs).count() == _Ct(__rhs).count();
599 }
600};
601
602template <class _LhsDuration>
603struct __duration_eq<_LhsDuration, _LhsDuration>
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 _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000607 {return __lhs.count() == __rhs.count();}
608};
609
610template <class _Rep1, class _Period1, class _Rep2, class _Period2>
611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000612_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613bool
614operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
615{
616 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
617}
618
619// Duration !=
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 !(__lhs == __rhs);
628}
629
630// Duration <
631
632template <class _LhsDuration, class _RhsDuration>
633struct __duration_lt
634{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000635 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnant12967fb2013-06-28 18:09:35 +0000636 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637 {
638 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
639 return _Ct(__lhs).count() < _Ct(__rhs).count();
640 }
641};
642
643template <class _LhsDuration>
644struct __duration_lt<_LhsDuration, _LhsDuration>
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 _LhsDuration& __rhs) const
Howard Hinnantc51e1022010-05-11 19:42:16 +0000648 {return __lhs.count() < __rhs.count();}
649};
650
651template <class _Rep1, class _Period1, class _Rep2, class _Period2>
652inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000653_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000654bool
655operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
656{
657 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
658}
659
660// Duration >
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 __rhs < __lhs;
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 !(__lhs < __rhs);
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 +0000698typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
699operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
700{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000701 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
702 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000703}
704
705// Duration -
706
707template <class _Rep1, class _Period1, class _Rep2, class _Period2>
708inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000709_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000710typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
711operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
712{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000713 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
714 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000715}
716
717// Duration *
718
719template <class _Rep1, class _Period, class _Rep2>
720inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000721_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000722typename enable_if
723<
724 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
725 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
726>::type
727operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
728{
729 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000730 typedef duration<_Cr, _Period> _Cd;
731 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732}
733
734template <class _Rep1, class _Period, class _Rep2>
735inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000736_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000737typename enable_if
738<
739 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
740 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
741>::type
742operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
743{
744 return __d * __s;
745}
746
747// Duration /
748
749template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
750struct __duration_divide_result
751{
752};
753
754template <class _Duration, class _Rep2,
755 bool = is_convertible<_Rep2,
756 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
757struct __duration_divide_imp
758{
759};
760
761template <class _Rep1, class _Period, class _Rep2>
762struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
763{
764 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
765};
766
767template <class _Rep1, class _Period, class _Rep2>
768struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
769 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
770{
771};
772
773template <class _Rep1, class _Period, class _Rep2>
774inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000775_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000776typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
777operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
778{
779 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000780 typedef duration<_Cr, _Period> _Cd;
781 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000782}
783
784template <class _Rep1, class _Period1, class _Rep2, class _Period2>
785inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000786_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000787typename common_type<_Rep1, _Rep2>::type
788operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
789{
790 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
791 return _Ct(__lhs).count() / _Ct(__rhs).count();
792}
793
794// Duration %
795
796template <class _Rep1, class _Period, class _Rep2>
797inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000798_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000799typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
800operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
801{
802 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000803 typedef duration<_Cr, _Period> _Cd;
804 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000805}
806
807template <class _Rep1, class _Period1, class _Rep2, class _Period2>
808inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000809_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
811operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
812{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000813 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
814 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
815 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000816}
817
818//////////////////////////////////////////////////////////
819///////////////////// time_point /////////////////////////
820//////////////////////////////////////////////////////////
821
822template <class _Clock, class _Duration = typename _Clock::duration>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000823class _LIBCPP_TYPE_VIS_ONLY time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +0000824{
825 static_assert(__is_duration<_Duration>::value,
826 "Second template parameter of time_point must be a std::chrono::duration");
827public:
828 typedef _Clock clock;
829 typedef _Duration duration;
830 typedef typename duration::rep rep;
831 typedef typename duration::period period;
832private:
833 duration __d_;
834
835public:
Marshall Clow5c459c92013-07-31 19:32:19 +0000836 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
837 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000838
839 // conversions
840 template <class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000841 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000842 time_point(const time_point<clock, _Duration2>& t,
843 typename enable_if
844 <
845 is_convertible<_Duration2, duration>::value
846 >::type* = 0)
847 : __d_(t.time_since_epoch()) {}
848
849 // observer
850
Marshall Clow5c459c92013-07-31 19:32:19 +0000851 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000852
853 // arithmetic
854
Howard Hinnantecc7ba92013-07-08 21:06:38 +0000855 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
856 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000857
858 // special values
859
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000860 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
861 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862};
863
864} // chrono
865
866template <class _Clock, class _Duration1, class _Duration2>
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000867struct _LIBCPP_TYPE_VIS_ONLY common_type<chrono::time_point<_Clock, _Duration1>,
868 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000869{
870 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
871};
872
873namespace chrono {
874
875template <class _ToDuration, class _Clock, class _Duration>
Marshall Clow5c459c92013-07-31 19:32:19 +0000876inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000877time_point<_Clock, _ToDuration>
878time_point_cast(const time_point<_Clock, _Duration>& __t)
879{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000880 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881}
882
Marshall Clowf2eabaf2015-11-05 19:33:59 +0000883#if _LIBCPP_STD_VER > 14
884template <class _ToDuration, class _Clock, class _Duration>
885inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
886typename enable_if
887<
888 __is_duration<_ToDuration>::value,
889 time_point<_Clock, _ToDuration>
890>::type
891floor(const time_point<_Clock, _Duration>& __t)
892{
893 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
894}
895
896template <class _ToDuration, class _Clock, class _Duration>
897inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
898typename enable_if
899<
900 __is_duration<_ToDuration>::value,
901 time_point<_Clock, _ToDuration>
902>::type
903ceil(const time_point<_Clock, _Duration>& __t)
904{
905 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
906}
907
908template <class _ToDuration, class _Clock, class _Duration>
909inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
910typename enable_if
911<
912 __is_duration<_ToDuration>::value,
913 time_point<_Clock, _ToDuration>
914>::type
915round(const time_point<_Clock, _Duration>& __t)
916{
917 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
918}
919
920template <class _Rep, class _Period>
921inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
922typename enable_if
923<
924 numeric_limits<_Rep>::is_signed,
925 duration<_Rep, _Period>
926>::type
927abs(duration<_Rep, _Period> __d)
928{
929 return __d >= __d.zero() ? __d : -__d;
930}
931#endif
932
Howard Hinnantc51e1022010-05-11 19:42:16 +0000933// time_point ==
934
935template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000936inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000937bool
938operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
939{
940 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
941}
942
943// time_point !=
944
945template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000946inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947bool
948operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
949{
950 return !(__lhs == __rhs);
951}
952
953// time_point <
954
955template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000956inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000957bool
958operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
959{
960 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
961}
962
963// time_point >
964
965template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000966inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967bool
968operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
969{
970 return __rhs < __lhs;
971}
972
973// time_point <=
974
975template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000976inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-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 Clow5c459c92013-07-31 19:32:19 +0000986inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987bool
988operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
989{
990 return !(__lhs < __rhs);
991}
992
993// time_point operator+(time_point x, duration y);
994
995template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +0000996inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +0000997time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
998operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
999{
1000 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
Marshall Clow5c459c92013-07-31 19:32:19 +00001001 return _Tr (__lhs.time_since_epoch() + __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001002}
1003
1004// time_point operator+(duration x, time_point y);
1005
1006template <class _Rep1, class _Period1, class _Clock, class _Duration2>
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<duration<_Rep1, _Period1>, _Duration2>::type>
1009operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1010{
1011 return __rhs + __lhs;
1012}
1013
1014// time_point operator-(time_point x, duration y);
1015
1016template <class _Clock, class _Duration1, class _Rep2, class _Period2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001017inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001018time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1019operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1020{
1021 return __lhs + (-__rhs);
1022}
1023
1024// duration operator-(time_point x, time_point y);
1025
1026template <class _Clock, class _Duration1, class _Duration2>
Marshall Clow5c459c92013-07-31 19:32:19 +00001027inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028typename common_type<_Duration1, _Duration2>::type
1029operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1030{
1031 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1032}
1033
1034//////////////////////////////////////////////////////////
1035/////////////////////// clocks ///////////////////////////
1036//////////////////////////////////////////////////////////
1037
Howard Hinnant8331b762013-03-06 23:30:19 +00001038class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001039{
1040public:
1041 typedef microseconds duration;
1042 typedef duration::rep rep;
1043 typedef duration::period period;
1044 typedef chrono::time_point<system_clock> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001045 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001047 static time_point now() _NOEXCEPT;
1048 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1049 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001050};
1051
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001052#ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
Howard Hinnant8331b762013-03-06 23:30:19 +00001053class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +00001054{
1055public:
1056 typedef nanoseconds duration;
1057 typedef duration::rep rep;
1058 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001059 typedef chrono::time_point<steady_clock, duration> time_point;
Marshall Clow5c459c92013-07-31 19:32:19 +00001060 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001061
Howard Hinnantaa54ac42011-05-28 18:34:36 +00001062 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063};
1064
Howard Hinnantc8dbd222010-11-20 19:16:30 +00001065typedef steady_clock high_resolution_clock;
Jonathan Roelofscce96eb2014-09-02 21:14:38 +00001066#else
1067typedef system_clock high_resolution_clock;
1068#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001069
1070} // chrono
1071
Marshall Clowac868372013-10-05 21:18:32 +00001072#if _LIBCPP_STD_VER > 11
1073// Suffixes for duration literals [time.duration.literals]
1074inline namespace literals
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001075{
1076 inline namespace chrono_literals
1077 {
1078
Howard Hinnant5c167562013-08-07 19:39:48 +00001079 constexpr chrono::hours operator"" h(unsigned long long __h)
1080 {
1081 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1082 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001083
Howard Hinnant5c167562013-08-07 19:39:48 +00001084 constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h)
1085 {
1086 return chrono::duration<long double, ratio<3600,1>>(__h);
1087 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001088
1089
Howard Hinnant5c167562013-08-07 19:39:48 +00001090 constexpr chrono::minutes operator"" min(unsigned long long __m)
1091 {
1092 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1093 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001094
Howard Hinnant5c167562013-08-07 19:39:48 +00001095 constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m)
1096 {
1097 return chrono::duration<long double, ratio<60,1>> (__m);
1098 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001099
1100
Howard Hinnant5c167562013-08-07 19:39:48 +00001101 constexpr chrono::seconds operator"" s(unsigned long long __s)
1102 {
1103 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1104 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001105
Howard Hinnant5c167562013-08-07 19:39:48 +00001106 constexpr chrono::duration<long double> operator"" s(long double __s)
1107 {
1108 return chrono::duration<long double> (__s);
1109 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001110
1111
Howard Hinnant5c167562013-08-07 19:39:48 +00001112 constexpr chrono::milliseconds operator"" ms(unsigned long long __ms)
1113 {
1114 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1115 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001116
Howard Hinnant5c167562013-08-07 19:39:48 +00001117 constexpr chrono::duration<long double, milli> operator"" ms(long double __ms)
1118 {
1119 return chrono::duration<long double, milli>(__ms);
1120 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001121
1122
Howard Hinnant5c167562013-08-07 19:39:48 +00001123 constexpr chrono::microseconds operator"" us(unsigned long long __us)
1124 {
1125 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1126 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001127
Howard Hinnant5c167562013-08-07 19:39:48 +00001128 constexpr chrono::duration<long double, micro> operator"" us(long double __us)
1129 {
1130 return chrono::duration<long double, micro> (__us);
1131 }
1132
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001133
Howard Hinnant5c167562013-08-07 19:39:48 +00001134 constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns)
1135 {
1136 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1137 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001138
Howard Hinnant5c167562013-08-07 19:39:48 +00001139 constexpr chrono::duration<long double, nano> operator"" ns(long double __ns)
1140 {
1141 return chrono::duration<long double, nano> (__ns);
1142 }
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001143
1144}}
Marshall Clowac868372013-10-05 21:18:32 +00001145
1146namespace chrono { // hoist the literals into namespace std::chrono
1147 using namespace literals::chrono_literals;
1148}
1149
Marshall Cloweb41e5c2013-07-24 21:18:14 +00001150#endif
1151
Howard Hinnantc51e1022010-05-11 19:42:16 +00001152_LIBCPP_END_NAMESPACE_STD
1153
1154#endif // _LIBCPP_CHRONO