blob: 508c1f37757951483b0c683b47b5334d449a4f0a [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:161// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:014// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:165//
Howard Hinnantb64f8b02010-11-16 22:09:026// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:168//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_CHRONO
12#define _LIBCPP_CHRONO
13
14/*
15 chrono synopsis
16
17namespace std
18{
19namespace chrono
20{
21
22template <class ToDuration, class Rep, class Period>
Howard Hinnant473f8382012-07-13 19:17:2723constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:1624ToDuration
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 Hinnant473f8382012-07-13 19:17:2733 static constexpr Rep zero();
34 static constexpr Rep max();
35 static constexpr Rep min();
Howard Hinnantbc8d3f92010-05-11 19:42:1636};
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 Hinnant473f8382012-07-13 19:17:2750 constexpr duration() = default;
Howard Hinnantbc8d3f92010-05-11 19:42:1651 template <class Rep2>
Howard Hinnant473f8382012-07-13 19:17:2752 constexpr explicit duration(const Rep2& r,
Howard Hinnantbc8d3f92010-05-11 19:42:1653 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 Hinnant473f8382012-07-13 19:17:2762 constexpr duration(const duration<Rep2, Period2>& d,
Howard Hinnantbc8d3f92010-05-11 19:42:1663 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 Hinnant473f8382012-07-13 19:17:2771 constexpr rep count() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1672
73 // arithmetic
74
Howard Hinnant473f8382012-07-13 19:17:2775 constexpr duration operator+() const;
76 constexpr duration operator-() const;
Howard Hinnantbc8d3f92010-05-11 19:42:1677 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 Hinnant473f8382012-07-13 19:17:2790 static constexpr duration zero();
91 static constexpr duration min();
92 static constexpr duration max();
Howard Hinnantbc8d3f92010-05-11 19:42:1693};
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 Hinnant473f8382012-07-13 19:17:27149 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16150 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 Hinnant473f8382012-07-13 19:17:27153 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16154 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 Hinnant473f8382012-07-13 19:17:27157 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16158 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 Hinnant473f8382012-07-13 19:17:27161 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16162 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 Hinnant473f8382012-07-13 19:17:27165 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16166 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 Hinnant473f8382012-07-13 19:17:27169 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16170 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 Hinnant473f8382012-07-13 19:17:27175 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16176 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
177template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27178 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16179 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
180template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27181 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16182 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
183template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27184 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16185 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
186template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27187 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16188 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
189template <class Rep1, class Period1, class Rep2, class Period2>
Howard Hinnant473f8382012-07-13 19:17:27190 constexpr
Howard Hinnantbc8d3f92010-05-11 19:42:16191 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 Hinnantf8f85212010-11-20 19:16:30239 static const bool is_steady = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16240
Howard Hinnant756a1762011-05-28 18:34:36241 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 Hinnantbc8d3f92010-05-11 19:42:16244};
245
Howard Hinnantf8f85212010-11-20 19:16:30246class steady_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16247{
248public:
249 typedef nanoseconds duration;
250 typedef duration::rep rep;
251 typedef duration::period period;
Howard Hinnantf8f85212010-11-20 19:16:30252 typedef chrono::time_point<steady_clock, duration> time_point;
253 static const bool is_steady = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16254
Howard Hinnant756a1762011-05-28 18:34:36255 static time_point now() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16256};
257
Howard Hinnantf8f85212010-11-20 19:16:30258typedef steady_clock high_resolution_clock;
Howard Hinnantbc8d3f92010-05-11 19:42:16259
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 Hinnant66c6f972011-11-29 16:45:27271#include <__undef_min_max>
272
Howard Hinnant08e17472011-10-17 20:05:10273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16274#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10275#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16276
277_LIBCPP_BEGIN_NAMESPACE_STD
278
279namespace chrono
280{
281
Howard Hinnant422a53f2010-09-21 21:28:23282template <class _Rep, class _Period = ratio<1> > class _LIBCPP_VISIBLE duration;
Howard Hinnantbc8d3f92010-05-11 19:42:16283
Howard Hinnantd8bc09b2010-05-18 17:32:30284template <class _Tp>
Howard Hinnantbc8d3f92010-05-11 19:42:16285struct __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 Hinnant422a53f2010-09-21 21:28:23302struct _LIBCPP_VISIBLE common_type<chrono::duration<_Rep1, _Period1>,
303 chrono::duration<_Rep2, _Period2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16304{
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 Hinnant473f8382012-07-13 19:17:27322 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16323 _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 Hinnant473f8382012-07-13 19:17:27332 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16333 _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 Hinnant473f8382012-07-13 19:17:27344 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16345 _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 Hinnant473f8382012-07-13 19:17:27356 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16357 _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 Hinnant473f8382012-07-13 19:17:27368_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16369typename 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 Hinnant422a53f2010-09-21 21:28:23379template <class _Rep>
380struct _LIBCPP_VISIBLE treat_as_floating_point : is_floating_point<_Rep> {};
Howard Hinnantbc8d3f92010-05-11 19:42:16381
382template <class _Rep>
Howard Hinnant422a53f2010-09-21 21:28:23383struct _LIBCPP_VISIBLE duration_values
Howard Hinnantbc8d3f92010-05-11 19:42:16384{
385public:
Howard Hinnant473f8382012-07-13 19:17:27386 _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 Hinnantbc8d3f92010-05-11 19:42:16389};
390
391// duration
392
393template <class _Rep, class _Period>
Howard Hinnant422a53f2010-09-21 21:28:23394class _LIBCPP_VISIBLE duration
Howard Hinnantbc8d3f92010-05-11 19:42:16395{
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 Hinnant473f8382012-07-13 19:17:27406 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration() {} // = default;
Howard Hinnantbc8d3f92010-05-11 19:42:16407 template <class _Rep2>
Howard Hinnant473f8382012-07-13 19:17:27408 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16409 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 Hinnant473f8382012-07-13 19:17:27420 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16421 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 Hinnant0949eed2011-06-30 21:18:19428 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16429
430 // observer
431
Howard Hinnant473f8382012-07-13 19:17:27432 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
Howard Hinnantbc8d3f92010-05-11 19:42:16433
434 // arithmetic
435
Howard Hinnant473f8382012-07-13 19:17:27436 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator+() const {return *this;}
437 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration operator-() const {return duration(-__rep_);}
Howard Hinnantbc8d3f92010-05-11 19:42:16438 _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 Hinnant473f8382012-07-13 19:17:27453 _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 Hinnantbc8d3f92010-05-11 19:42:16456};
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 Hinnant473f8382012-07-13 19:17:27470 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16471 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 Hinnant473f8382012-07-13 19:17:27481 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16482 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 Hinnant473f8382012-07-13 19:17:27488_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16489bool
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 Hinnant473f8382012-07-13 19:17:27499_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16500bool
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 Hinnant473f8382012-07-13 19:17:27511 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16512 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 Hinnant473f8382012-07-13 19:17:27522 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16523 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 Hinnant473f8382012-07-13 19:17:27529_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16530bool
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 Hinnant473f8382012-07-13 19:17:27540_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16541bool
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 Hinnant473f8382012-07-13 19:17:27551_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16552bool
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 Hinnant473f8382012-07-13 19:17:27562_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16563bool
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 Hinnant473f8382012-07-13 19:17:27573_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16574typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
575operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
576{
Howard Hinnant473f8382012-07-13 19:17:27577 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
578 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
Howard Hinnantbc8d3f92010-05-11 19:42:16579}
580
581// Duration -
582
583template <class _Rep1, class _Period1, class _Rep2, class _Period2>
584inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27585_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16586typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
587operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
588{
Howard Hinnant473f8382012-07-13 19:17:27589 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
590 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
Howard Hinnantbc8d3f92010-05-11 19:42:16591}
592
593// Duration *
594
595template <class _Rep1, class _Period, class _Rep2>
596inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27597_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16598typename 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 Hinnant473f8382012-07-13 19:17:27606 typedef duration<_Cr, _Period> _Cd;
607 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16608}
609
610template <class _Rep1, class _Period, class _Rep2>
611inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27612_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16613typename 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 Hinnant473f8382012-07-13 19:17:27651_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16652typename __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 Hinnant473f8382012-07-13 19:17:27656 typedef duration<_Cr, _Period> _Cd;
657 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16658}
659
660template <class _Rep1, class _Period1, class _Rep2, class _Period2>
661inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27662_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16663typename 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 Hinnant473f8382012-07-13 19:17:27674_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16675typename __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 Hinnant473f8382012-07-13 19:17:27679 typedef duration<_Cr, _Period> _Cd;
680 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
Howard Hinnantbc8d3f92010-05-11 19:42:16681}
682
683template <class _Rep1, class _Period1, class _Rep2, class _Period2>
684inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnant473f8382012-07-13 19:17:27685_LIBCPP_CONSTEXPR
Howard Hinnantbc8d3f92010-05-11 19:42:16686typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
687operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
688{
Howard Hinnant473f8382012-07-13 19:17:27689 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 Hinnantbc8d3f92010-05-11 19:42:16692}
693
694//////////////////////////////////////////////////////////
695///////////////////// time_point /////////////////////////
696//////////////////////////////////////////////////////////
697
698template <class _Clock, class _Duration = typename _Clock::duration>
Howard Hinnant422a53f2010-09-21 21:28:23699class _LIBCPP_VISIBLE time_point
Howard Hinnantbc8d3f92010-05-11 19:42:16700{
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 Kyrtzidis1dc6f7a2012-10-13 02:03:45731_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 Hinnantbc8d3f92010-05-11 19:42:16733
734 // special values
735
Howard Hinnant473f8382012-07-13 19:17:27736 _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 Hinnantbc8d3f92010-05-11 19:42:16738};
739
740} // chrono
741
742template <class _Clock, class _Duration1, class _Duration2>
Howard Hinnant422a53f2010-09-21 21:28:23743struct _LIBCPP_VISIBLE common_type<chrono::time_point<_Clock, _Duration1>,
744 chrono::time_point<_Clock, _Duration2> >
Howard Hinnantbc8d3f92010-05-11 19:42:16745{
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 Hinnant0949eed2011-06-30 21:18:19756 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
Howard Hinnantbc8d3f92010-05-11 19:42:16757}
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 Hinnant422a53f2010-09-21 21:28:23866class _LIBCPP_VISIBLE system_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16867{
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 Hinnantf8f85212010-11-20 19:16:30873 static const bool is_steady = false;
Howard Hinnantbc8d3f92010-05-11 19:42:16874
Howard Hinnant756a1762011-05-28 18:34:36875 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 Hinnantbc8d3f92010-05-11 19:42:16878};
879
Howard Hinnantf8f85212010-11-20 19:16:30880class _LIBCPP_VISIBLE steady_clock
Howard Hinnantbc8d3f92010-05-11 19:42:16881{
882public:
883 typedef nanoseconds duration;
884 typedef duration::rep rep;
885 typedef duration::period period;
Howard Hinnantf8f85212010-11-20 19:16:30886 typedef chrono::time_point<steady_clock, duration> time_point;
887 static const bool is_steady = true;
Howard Hinnantbc8d3f92010-05-11 19:42:16888
Howard Hinnant756a1762011-05-28 18:34:36889 static time_point now() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16890};
891
Howard Hinnantf8f85212010-11-20 19:16:30892typedef steady_clock high_resolution_clock;
Howard Hinnantbc8d3f92010-05-11 19:42:16893
894} // chrono
895
896_LIBCPP_END_NAMESPACE_STD
897
898#endif // _LIBCPP_CHRONO