blob: 3b96e8164e5f6fbbce33055d8cc7a4db32d94d9a [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:
114 time_point(); // has value "epoch"
115 explicit time_point(const duration& d); // same as time_point() + d
116
117 // conversions
118 template <class Duration2>
119 time_point(const time_point<clock, Duration2>& t);
120
121 // observer
122
123 duration time_since_epoch() const;
124
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
197// time_point arithmetic
198template <class Clock, class Duration1, class Rep2, class Period2>
199 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
200 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
201template <class Rep1, class Period1, class Clock, class Duration2>
202 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
203 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
204template <class Clock, class Duration1, class Rep2, class Period2>
205 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
206 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
207template <class Clock, class Duration1, class Duration2>
208 typename common_type<Duration1, Duration2>::type
209 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
210
211// time_point comparisons
212template <class Clock, class Duration1, class Duration2>
213 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
214template <class Clock, class Duration1, class Duration2>
215 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
216template <class Clock, class Duration1, class Duration2>
217 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
218template <class Clock, class Duration1, class Duration2>
219 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
220template <class Clock, class Duration1, class Duration2>
221 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
222template <class Clock, class Duration1, class Duration2>
223 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
224
225// time_point_cast
226
227template <class ToDuration, class Clock, class Duration>
228 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
229
230// Clocks
231
232class system_clock
233{
234public:
235 typedef microseconds duration;
236 typedef duration::rep rep;
237 typedef duration::period period;
238 typedef chrono::time_point<system_clock> time_point;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000239 static const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000241 static time_point now() noexcept;
242 static time_t to_time_t (const time_point& __t) noexcept;
243 static time_point from_time_t(time_t __t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244};
245
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000246class steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000247{
248public:
249 typedef nanoseconds duration;
250 typedef duration::rep rep;
251 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000252 typedef chrono::time_point<steady_clock, duration> time_point;
253 static const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000255 static time_point now() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256};
257
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000258typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259
260} // chrono
261
262} // std
263*/
264
265#include <__config>
266#include <ctime>
267#include <type_traits>
268#include <ratio>
269#include <limits>
270
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000271#include <__undef_min_max>
272
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000274#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000275#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000276
277_LIBCPP_BEGIN_NAMESPACE_STD
278
279namespace chrono
280{
281
Howard Hinnant8331b762013-03-06 23:30:19 +0000282template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS duration;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283
Howard Hinnantf39463b2010-05-18 17:32:30 +0000284template <class _Tp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285struct __is_duration : false_type {};
286
287template <class _Rep, class _Period>
288struct __is_duration<duration<_Rep, _Period> > : true_type {};
289
290template <class _Rep, class _Period>
291struct __is_duration<const duration<_Rep, _Period> > : true_type {};
292
293template <class _Rep, class _Period>
294struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
295
296template <class _Rep, class _Period>
297struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
298
299} // chrono
300
301template <class _Rep1, class _Period1, class _Rep2, class _Period2>
Howard Hinnant8331b762013-03-06 23:30:19 +0000302struct _LIBCPP_TYPE_VIS common_type<chrono::duration<_Rep1, _Period1>,
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000303 chrono::duration<_Rep2, _Period2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304{
305 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
306 typename __ratio_gcd<_Period1, _Period2>::type> type;
307};
308
309namespace chrono {
310
311// duration_cast
312
313template <class _FromDuration, class _ToDuration,
314 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
315 bool = _Period::num == 1,
316 bool = _Period::den == 1>
317struct __duration_cast;
318
319template <class _FromDuration, class _ToDuration, class _Period>
320struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
321{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000323 _ToDuration operator()(const _FromDuration& __fd) const
324 {
325 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
326 }
327};
328
329template <class _FromDuration, class _ToDuration, class _Period>
330struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
331{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000332 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000333 _ToDuration operator()(const _FromDuration& __fd) const
334 {
335 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
336 return _ToDuration(static_cast<typename _ToDuration::rep>(
337 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
338 }
339};
340
341template <class _FromDuration, class _ToDuration, class _Period>
342struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
343{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000344 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000345 _ToDuration operator()(const _FromDuration& __fd) const
346 {
347 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
348 return _ToDuration(static_cast<typename _ToDuration::rep>(
349 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
350 }
351};
352
353template <class _FromDuration, class _ToDuration, class _Period>
354struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
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 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
360 return _ToDuration(static_cast<typename _ToDuration::rep>(
361 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
362 / static_cast<_Ct>(_Period::den)));
363 }
364};
365
366template <class _ToDuration, class _Rep, class _Period>
367inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000368_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000369typename enable_if
370<
371 __is_duration<_ToDuration>::value,
372 _ToDuration
373>::type
374duration_cast(const duration<_Rep, _Period>& __fd)
375{
376 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
377}
378
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000379template <class _Rep>
Howard Hinnant8331b762013-03-06 23:30:19 +0000380struct _LIBCPP_TYPE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381
382template <class _Rep>
Howard Hinnant8331b762013-03-06 23:30:19 +0000383struct _LIBCPP_TYPE_VIS duration_values
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384{
385public:
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000386 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
387 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
388 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000389};
390
391// duration
392
393template <class _Rep, class _Period>
Howard Hinnant8331b762013-03-06 23:30:19 +0000394class _LIBCPP_TYPE_VIS duration
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395{
396 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
397 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
398 static_assert(_Period::num > 0, "duration period must be positive");
399public:
400 typedef _Rep rep;
401 typedef _Period period;
402private:
403 rep __rep_;
404public:
405
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000406 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration() {} // = default;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000407 template <class _Rep2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000408 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 explicit duration(const _Rep2& __r,
410 typename enable_if
411 <
412 is_convertible<_Rep2, rep>::value &&
413 (treat_as_floating_point<rep>::value ||
414 !treat_as_floating_point<_Rep2>::value)
415 >::type* = 0)
416 : __rep_(__r) {}
417
418 // conversions
419 template <class _Rep2, class _Period2>
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000420 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000421 duration(const duration<_Rep2, _Period2>& __d,
422 typename enable_if
423 <
424 treat_as_floating_point<rep>::value ||
425 (ratio_divide<_Period2, period>::type::den == 1 &&
426 !treat_as_floating_point<_Rep2>::value)
427 >::type* = 0)
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000428 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000429
430 // observer
431
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000432 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433
434 // arithmetic
435
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000436 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;}
437 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;}
439 _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);}
440 _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;}
441 _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);}
442
443 _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
444 _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
445
446 _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
447 _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
448 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
449 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
450
451 // special values
452
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000453 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
454 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
455 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000456};
457
458typedef duration<long long, nano> nanoseconds;
459typedef duration<long long, micro> microseconds;
460typedef duration<long long, milli> milliseconds;
461typedef duration<long long > seconds;
462typedef duration< long, ratio< 60> > minutes;
463typedef duration< long, ratio<3600> > hours;
464
465// Duration ==
466
467template <class _LhsDuration, class _RhsDuration>
468struct __duration_eq
469{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000470 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000471 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs)
472 {
473 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
474 return _Ct(__lhs).count() == _Ct(__rhs).count();
475 }
476};
477
478template <class _LhsDuration>
479struct __duration_eq<_LhsDuration, _LhsDuration>
480{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000481 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000482 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs)
483 {return __lhs.count() == __rhs.count();}
484};
485
486template <class _Rep1, class _Period1, class _Rep2, class _Period2>
487inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000488_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489bool
490operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
491{
492 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
493}
494
495// Duration !=
496
497template <class _Rep1, class _Period1, class _Rep2, class _Period2>
498inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000499_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000500bool
501operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
502{
503 return !(__lhs == __rhs);
504}
505
506// Duration <
507
508template <class _LhsDuration, class _RhsDuration>
509struct __duration_lt
510{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000511 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs)
513 {
514 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
515 return _Ct(__lhs).count() < _Ct(__rhs).count();
516 }
517};
518
519template <class _LhsDuration>
520struct __duration_lt<_LhsDuration, _LhsDuration>
521{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000522 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000523 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs)
524 {return __lhs.count() < __rhs.count();}
525};
526
527template <class _Rep1, class _Period1, class _Rep2, class _Period2>
528inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000529_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530bool
531operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
532{
533 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
534}
535
536// Duration >
537
538template <class _Rep1, class _Period1, class _Rep2, class _Period2>
539inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000540_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541bool
542operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
543{
544 return __rhs < __lhs;
545}
546
547// Duration <=
548
549template <class _Rep1, class _Period1, class _Rep2, class _Period2>
550inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000551_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000552bool
553operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
554{
555 return !(__rhs < __lhs);
556}
557
558// Duration >=
559
560template <class _Rep1, class _Period1, class _Rep2, class _Period2>
561inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000562_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563bool
564operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
565{
566 return !(__lhs < __rhs);
567}
568
569// Duration +
570
571template <class _Rep1, class _Period1, class _Rep2, class _Period2>
572inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000573_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
575operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
576{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000577 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
578 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579}
580
581// Duration -
582
583template <class _Rep1, class _Period1, class _Rep2, class _Period2>
584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000585_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000586typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
587operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
588{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000589 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
590 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000591}
592
593// Duration *
594
595template <class _Rep1, class _Period, class _Rep2>
596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000597_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598typename enable_if
599<
600 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
601 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
602>::type
603operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
604{
605 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000606 typedef duration<_Cr, _Period> _Cd;
607 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608}
609
610template <class _Rep1, class _Period, class _Rep2>
611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000612_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000613typename enable_if
614<
615 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
616 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
617>::type
618operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
619{
620 return __d * __s;
621}
622
623// Duration /
624
625template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
626struct __duration_divide_result
627{
628};
629
630template <class _Duration, class _Rep2,
631 bool = is_convertible<_Rep2,
632 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
633struct __duration_divide_imp
634{
635};
636
637template <class _Rep1, class _Period, class _Rep2>
638struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
639{
640 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
641};
642
643template <class _Rep1, class _Period, class _Rep2>
644struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
645 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
646{
647};
648
649template <class _Rep1, class _Period, class _Rep2>
650inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000651_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
653operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
654{
655 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000656 typedef duration<_Cr, _Period> _Cd;
657 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658}
659
660template <class _Rep1, class _Period1, class _Rep2, class _Period2>
661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000662_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663typename common_type<_Rep1, _Rep2>::type
664operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
665{
666 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
667 return _Ct(__lhs).count() / _Ct(__rhs).count();
668}
669
670// Duration %
671
672template <class _Rep1, class _Period, class _Rep2>
673inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000674_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000675typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
676operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
677{
678 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000679 typedef duration<_Cr, _Period> _Cd;
680 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681}
682
683template <class _Rep1, class _Period1, class _Rep2, class _Period2>
684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000685_LIBCPP_CONSTEXPR
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
687operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
688{
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000689 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
690 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
691 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000692}
693
694//////////////////////////////////////////////////////////
695///////////////////// time_point /////////////////////////
696//////////////////////////////////////////////////////////
697
698template <class _Clock, class _Duration = typename _Clock::duration>
Howard Hinnant8331b762013-03-06 23:30:19 +0000699class _LIBCPP_TYPE_VIS time_point
Howard Hinnantc51e1022010-05-11 19:42:16 +0000700{
701 static_assert(__is_duration<_Duration>::value,
702 "Second template parameter of time_point must be a std::chrono::duration");
703public:
704 typedef _Clock clock;
705 typedef _Duration duration;
706 typedef typename duration::rep rep;
707 typedef typename duration::period period;
708private:
709 duration __d_;
710
711public:
712 _LIBCPP_INLINE_VISIBILITY time_point() : __d_(duration::zero()) {}
713 _LIBCPP_INLINE_VISIBILITY explicit time_point(const duration& __d) : __d_(__d) {}
714
715 // conversions
716 template <class _Duration2>
717 _LIBCPP_INLINE_VISIBILITY
718 time_point(const time_point<clock, _Duration2>& t,
719 typename enable_if
720 <
721 is_convertible<_Duration2, duration>::value
722 >::type* = 0)
723 : __d_(t.time_since_epoch()) {}
724
725 // observer
726
727 _LIBCPP_INLINE_VISIBILITY duration time_since_epoch() const {return __d_;}
728
729 // arithmetic
730
Argyrios Kyrtzidisaf904652012-10-13 02:03:45 +0000731 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
732 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000733
734 // special values
735
Howard Hinnantcf3143c2012-07-13 19:17:27 +0000736 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
737 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000738};
739
740} // chrono
741
742template <class _Clock, class _Duration1, class _Duration2>
Howard Hinnant8331b762013-03-06 23:30:19 +0000743struct _LIBCPP_TYPE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
Howard Hinnant874ad9a2010-09-21 21:28:23 +0000744 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745{
746 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
747};
748
749namespace chrono {
750
751template <class _ToDuration, class _Clock, class _Duration>
752inline _LIBCPP_INLINE_VISIBILITY
753time_point<_Clock, _ToDuration>
754time_point_cast(const time_point<_Clock, _Duration>& __t)
755{
Howard Hinnantb1ad5a82011-06-30 21:18:19 +0000756 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000757}
758
759// time_point ==
760
761template <class _Clock, class _Duration1, class _Duration2>
762inline _LIBCPP_INLINE_VISIBILITY
763bool
764operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
765{
766 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
767}
768
769// time_point !=
770
771template <class _Clock, class _Duration1, class _Duration2>
772inline _LIBCPP_INLINE_VISIBILITY
773bool
774operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
775{
776 return !(__lhs == __rhs);
777}
778
779// time_point <
780
781template <class _Clock, class _Duration1, class _Duration2>
782inline _LIBCPP_INLINE_VISIBILITY
783bool
784operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
785{
786 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
787}
788
789// time_point >
790
791template <class _Clock, class _Duration1, class _Duration2>
792inline _LIBCPP_INLINE_VISIBILITY
793bool
794operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
795{
796 return __rhs < __lhs;
797}
798
799// time_point <=
800
801template <class _Clock, class _Duration1, class _Duration2>
802inline _LIBCPP_INLINE_VISIBILITY
803bool
804operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
805{
806 return !(__rhs < __lhs);
807}
808
809// time_point >=
810
811template <class _Clock, class _Duration1, class _Duration2>
812inline _LIBCPP_INLINE_VISIBILITY
813bool
814operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
815{
816 return !(__lhs < __rhs);
817}
818
819// time_point operator+(time_point x, duration y);
820
821template <class _Clock, class _Duration1, class _Rep2, class _Period2>
822inline _LIBCPP_INLINE_VISIBILITY
823time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
824operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
825{
826 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
827 _Tr __r(__lhs.time_since_epoch());
828 __r += __rhs;
829 return __r;
830}
831
832// time_point operator+(duration x, time_point y);
833
834template <class _Rep1, class _Period1, class _Clock, class _Duration2>
835inline _LIBCPP_INLINE_VISIBILITY
836time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
837operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
838{
839 return __rhs + __lhs;
840}
841
842// time_point operator-(time_point x, duration y);
843
844template <class _Clock, class _Duration1, class _Rep2, class _Period2>
845inline _LIBCPP_INLINE_VISIBILITY
846time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
847operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
848{
849 return __lhs + (-__rhs);
850}
851
852// duration operator-(time_point x, time_point y);
853
854template <class _Clock, class _Duration1, class _Duration2>
855inline _LIBCPP_INLINE_VISIBILITY
856typename common_type<_Duration1, _Duration2>::type
857operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
858{
859 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
860}
861
862//////////////////////////////////////////////////////////
863/////////////////////// clocks ///////////////////////////
864//////////////////////////////////////////////////////////
865
Howard Hinnant8331b762013-03-06 23:30:19 +0000866class _LIBCPP_TYPE_VIS system_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000867{
868public:
869 typedef microseconds duration;
870 typedef duration::rep rep;
871 typedef duration::period period;
872 typedef chrono::time_point<system_clock> time_point;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000873 static const bool is_steady = false;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000874
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000875 static time_point now() _NOEXCEPT;
876 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
877 static time_point from_time_t(time_t __t) _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878};
879
Howard Hinnant8331b762013-03-06 23:30:19 +0000880class _LIBCPP_TYPE_VIS steady_clock
Howard Hinnantc51e1022010-05-11 19:42:16 +0000881{
882public:
883 typedef nanoseconds duration;
884 typedef duration::rep rep;
885 typedef duration::period period;
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000886 typedef chrono::time_point<steady_clock, duration> time_point;
887 static const bool is_steady = true;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000888
Howard Hinnantaa54ac42011-05-28 18:34:36 +0000889 static time_point now() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000890};
891
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000892typedef steady_clock high_resolution_clock;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000893
894} // chrono
895
896_LIBCPP_END_NAMESPACE_STD
897
898#endif // _LIBCPP_CHRONO