blob: 911a95ec72c74b697d3cc11841d8a08c4da016a8 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
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>
23ToDuration
24duration_cast(const duration<Rep, Period>& fd);
25
26template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {};
27
28template <class Rep>
29struct duration_values
30{
31public:
32 static Rep zero();
33 static Rep max();
34 static Rep min();
35};
36
37// duration
38
39template <class Rep, class Period = ratio<1>>
40class duration
41{
42 static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration");
43 static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio");
44 static_assert(Period::num > 0, "duration period must be positive");
45public:
46 typedef Rep rep;
47 typedef Period period;
48
49 duration() = default;
50 template <class Rep2>
51 explicit duration(const Rep2& r,
52 typename enable_if
53 <
54 is_convertible<Rep2, rep>::value &&
55 (treat_as_floating_point<rep>::value ||
56 !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value)
57 >::type* = 0);
58
59 // conversions
60 template <class Rep2, class Period2>
61 duration(const duration<Rep2, Period2>& d,
62 typename enable_if
63 <
64 treat_as_floating_point<rep>::value ||
65 ratio_divide<Period2, period>::type::den == 1
66 >::type* = 0);
67
68 // observer
69
70 rep count() const;
71
72 // arithmetic
73
74 duration operator+() const;
75 duration operator-() const;
76 duration& operator++();
77 duration operator++(int);
78 duration& operator--();
79 duration operator--(int);
80
81 duration& operator+=(const duration& d);
82 duration& operator-=(const duration& d);
83
84 duration& operator*=(const rep& rhs);
85 duration& operator/=(const rep& rhs);
86
87 // special values
88
89 static duration zero();
90 static duration min();
91 static duration max();
92};
93
94typedef duration<long long, nano> nanoseconds;
95typedef duration<long long, micro> microseconds;
96typedef duration<long long, milli> milliseconds;
97typedef duration<long long > seconds;
98typedef duration< long, ratio< 60> > minutes;
99typedef duration< long, ratio<3600> > hours;
100
101template <class Clock, class Duration = typename Clock::duration>
102class time_point
103{
104public:
105 typedef Clock clock;
106 typedef Duration duration;
107 typedef typename duration::rep rep;
108 typedef typename duration::period period;
109private:
110 duration d_; // exposition only
111
112public:
113 time_point(); // has value "epoch"
114 explicit time_point(const duration& d); // same as time_point() + d
115
116 // conversions
117 template <class Duration2>
118 time_point(const time_point<clock, Duration2>& t);
119
120 // observer
121
122 duration time_since_epoch() const;
123
124 // arithmetic
125
126 time_point& operator+=(const duration& d);
127 time_point& operator-=(const duration& d);
128
129 // special values
130
131 static constexpr time_point min();
132 static constexpr time_point max();
133};
134
135} // chrono
136
137// common_type traits
138template <class Rep1, class Period1, class Rep2, class Period2>
139 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
140
141template <class Clock, class Duration1, class Duration2>
142 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
143
144namespace chrono {
145
146// duration arithmetic
147template <class Rep1, class Period1, class Rep2, class Period2>
148 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
149 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
150template <class Rep1, class Period1, class Rep2, class Period2>
151 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
152 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
153template <class Rep1, class Period, class Rep2>
154 duration<typename common_type<Rep1, Rep2>::type, Period>
155 operator*(const duration<Rep1, Period>& d, const Rep2& s);
156template <class Rep1, class Period, class Rep2>
157 duration<typename common_type<Rep1, Rep2>::type, Period>
158 operator*(const Rep1& s, const duration<Rep2, Period>& d);
159template <class Rep1, class Period, class Rep2>
160 duration<typename common_type<Rep1, Rep2>::type, Period>
161 operator/(const duration<Rep1, Period>& d, const Rep2& s);
162template <class Rep1, class Period1, class Rep2, class Period2>
163 typename common_type<Rep1, Rep2>::type
164 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
165
166// duration comparisons
167template <class Rep1, class Period1, class Rep2, class Period2>
168 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
169template <class Rep1, class Period1, class Rep2, class Period2>
170 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
171template <class Rep1, class Period1, class Rep2, class Period2>
172 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
173template <class Rep1, class Period1, class Rep2, class Period2>
174 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
175template <class Rep1, class Period1, class Rep2, class Period2>
176 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
177template <class Rep1, class Period1, class Rep2, class Period2>
178 bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
179
180// duration_cast
181template <class ToDuration, class Rep, class Period>
182 ToDuration duration_cast(const duration<Rep, Period>& d);
183
184// time_point arithmetic
185template <class Clock, class Duration1, class Rep2, class Period2>
186 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
187 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
188template <class Rep1, class Period1, class Clock, class Duration2>
189 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
190 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
191template <class Clock, class Duration1, class Rep2, class Period2>
192 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
193 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
194template <class Clock, class Duration1, class Duration2>
195 typename common_type<Duration1, Duration2>::type
196 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
197
198// time_point comparisons
199template <class Clock, class Duration1, class Duration2>
200 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
201template <class Clock, class Duration1, class Duration2>
202 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
203template <class Clock, class Duration1, class Duration2>
204 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
205template <class Clock, class Duration1, class Duration2>
206 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
207template <class Clock, class Duration1, class Duration2>
208 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
209template <class Clock, class Duration1, class Duration2>
210 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
211
212// time_point_cast
213
214template <class ToDuration, class Clock, class Duration>
215 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
216
217// Clocks
218
219class system_clock
220{
221public:
222 typedef microseconds duration;
223 typedef duration::rep rep;
224 typedef duration::period period;
225 typedef chrono::time_point<system_clock> time_point;
226 static const bool is_monotonic = false;
227
228 static time_point now();
229 static time_t to_time_t (const time_point& __t);
230 static time_point from_time_t(time_t __t);
231};
232
233class monotonic_clock
234{
235public:
236 typedef nanoseconds duration;
237 typedef duration::rep rep;
238 typedef duration::period period;
239 typedef chrono::time_point<monotonic_clock, duration> time_point;
240 static const bool is_monotonic = true;
241
242 static time_point now();
243};
244
245typedef monotonic_clock high_resolution_clock;
246
247} // chrono
248
249} // std
250*/
251
252#include <__config>
253#include <ctime>
254#include <type_traits>
255#include <ratio>
256#include <limits>
257
258#pragma GCC system_header
259
260_LIBCPP_BEGIN_NAMESPACE_STD
261
262namespace chrono
263{
264
265template <class _Rep, class _Period = ratio<1> > class duration;
266
267template <class T>
268struct __is_duration : false_type {};
269
270template <class _Rep, class _Period>
271struct __is_duration<duration<_Rep, _Period> > : true_type {};
272
273template <class _Rep, class _Period>
274struct __is_duration<const duration<_Rep, _Period> > : true_type {};
275
276template <class _Rep, class _Period>
277struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
278
279template <class _Rep, class _Period>
280struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
281
282} // chrono
283
284template <class _Rep1, class _Period1, class _Rep2, class _Period2>
285struct common_type<chrono::duration<_Rep1, _Period1>, chrono::duration<_Rep2, _Period2> >
286{
287 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
288 typename __ratio_gcd<_Period1, _Period2>::type> type;
289};
290
291namespace chrono {
292
293// duration_cast
294
295template <class _FromDuration, class _ToDuration,
296 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
297 bool = _Period::num == 1,
298 bool = _Period::den == 1>
299struct __duration_cast;
300
301template <class _FromDuration, class _ToDuration, class _Period>
302struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
303{
304 _LIBCPP_INLINE_VISIBILITY
305 _ToDuration operator()(const _FromDuration& __fd) const
306 {
307 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
308 }
309};
310
311template <class _FromDuration, class _ToDuration, class _Period>
312struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
313{
314 _LIBCPP_INLINE_VISIBILITY
315 _ToDuration operator()(const _FromDuration& __fd) const
316 {
317 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
318 return _ToDuration(static_cast<typename _ToDuration::rep>(
319 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
320 }
321};
322
323template <class _FromDuration, class _ToDuration, class _Period>
324struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
325{
326 _LIBCPP_INLINE_VISIBILITY
327 _ToDuration operator()(const _FromDuration& __fd) const
328 {
329 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
330 return _ToDuration(static_cast<typename _ToDuration::rep>(
331 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
332 }
333};
334
335template <class _FromDuration, class _ToDuration, class _Period>
336struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
337{
338 _LIBCPP_INLINE_VISIBILITY
339 _ToDuration operator()(const _FromDuration& __fd) const
340 {
341 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
342 return _ToDuration(static_cast<typename _ToDuration::rep>(
343 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
344 / static_cast<_Ct>(_Period::den)));
345 }
346};
347
348template <class _ToDuration, class _Rep, class _Period>
349inline _LIBCPP_INLINE_VISIBILITY
350typename enable_if
351<
352 __is_duration<_ToDuration>::value,
353 _ToDuration
354>::type
355duration_cast(const duration<_Rep, _Period>& __fd)
356{
357 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
358}
359
360template <class _Rep> struct treat_as_floating_point : is_floating_point<_Rep> {};
361
362template <class _Rep>
363struct duration_values
364{
365public:
366 _LIBCPP_INLINE_VISIBILITY static _Rep zero() {return _Rep(0);}
367 _LIBCPP_INLINE_VISIBILITY static _Rep max() {return numeric_limits<_Rep>::max();}
368 _LIBCPP_INLINE_VISIBILITY static _Rep min() {return numeric_limits<_Rep>::lowest();}
369};
370
371// duration
372
373template <class _Rep, class _Period>
374class duration
375{
376 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
377 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
378 static_assert(_Period::num > 0, "duration period must be positive");
379public:
380 typedef _Rep rep;
381 typedef _Period period;
382private:
383 rep __rep_;
384public:
385
386 _LIBCPP_INLINE_VISIBILITY duration() {} // = default;
387 template <class _Rep2>
388 _LIBCPP_INLINE_VISIBILITY
389 explicit duration(const _Rep2& __r,
390 typename enable_if
391 <
392 is_convertible<_Rep2, rep>::value &&
393 (treat_as_floating_point<rep>::value ||
394 !treat_as_floating_point<_Rep2>::value)
395 >::type* = 0)
396 : __rep_(__r) {}
397
398 // conversions
399 template <class _Rep2, class _Period2>
400 _LIBCPP_INLINE_VISIBILITY
401 duration(const duration<_Rep2, _Period2>& __d,
402 typename enable_if
403 <
404 treat_as_floating_point<rep>::value ||
405 (ratio_divide<_Period2, period>::type::den == 1 &&
406 !treat_as_floating_point<_Rep2>::value)
407 >::type* = 0)
408 : __rep_(_STD::chrono::duration_cast<duration>(__d).count()) {}
409
410 // observer
411
412 _LIBCPP_INLINE_VISIBILITY rep count() const {return __rep_;}
413
414 // arithmetic
415
416 _LIBCPP_INLINE_VISIBILITY duration operator+() const {return *this;}
417 _LIBCPP_INLINE_VISIBILITY duration operator-() const {return duration(-__rep_);}
418 _LIBCPP_INLINE_VISIBILITY duration& operator++() {++__rep_; return *this;}
419 _LIBCPP_INLINE_VISIBILITY duration operator++(int) {return duration(__rep_++);}
420 _LIBCPP_INLINE_VISIBILITY duration& operator--() {--__rep_; return *this;}
421 _LIBCPP_INLINE_VISIBILITY duration operator--(int) {return duration(__rep_--);}
422
423 _LIBCPP_INLINE_VISIBILITY duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
424 _LIBCPP_INLINE_VISIBILITY duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
425
426 _LIBCPP_INLINE_VISIBILITY duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
427 _LIBCPP_INLINE_VISIBILITY duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
428 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
429 _LIBCPP_INLINE_VISIBILITY duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
430
431 // special values
432
433 _LIBCPP_INLINE_VISIBILITY static duration zero() {return duration(duration_values<rep>::zero());}
434 _LIBCPP_INLINE_VISIBILITY static duration min() {return duration(duration_values<rep>::min());}
435 _LIBCPP_INLINE_VISIBILITY static duration max() {return duration(duration_values<rep>::max());}
436};
437
438typedef duration<long long, nano> nanoseconds;
439typedef duration<long long, micro> microseconds;
440typedef duration<long long, milli> milliseconds;
441typedef duration<long long > seconds;
442typedef duration< long, ratio< 60> > minutes;
443typedef duration< long, ratio<3600> > hours;
444
445// Duration ==
446
447template <class _LhsDuration, class _RhsDuration>
448struct __duration_eq
449{
450 _LIBCPP_INLINE_VISIBILITY
451 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs)
452 {
453 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
454 return _Ct(__lhs).count() == _Ct(__rhs).count();
455 }
456};
457
458template <class _LhsDuration>
459struct __duration_eq<_LhsDuration, _LhsDuration>
460{
461 _LIBCPP_INLINE_VISIBILITY
462 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs)
463 {return __lhs.count() == __rhs.count();}
464};
465
466template <class _Rep1, class _Period1, class _Rep2, class _Period2>
467inline _LIBCPP_INLINE_VISIBILITY
468bool
469operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
470{
471 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
472}
473
474// Duration !=
475
476template <class _Rep1, class _Period1, class _Rep2, class _Period2>
477inline _LIBCPP_INLINE_VISIBILITY
478bool
479operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
480{
481 return !(__lhs == __rhs);
482}
483
484// Duration <
485
486template <class _LhsDuration, class _RhsDuration>
487struct __duration_lt
488{
489 _LIBCPP_INLINE_VISIBILITY
490 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs)
491 {
492 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
493 return _Ct(__lhs).count() < _Ct(__rhs).count();
494 }
495};
496
497template <class _LhsDuration>
498struct __duration_lt<_LhsDuration, _LhsDuration>
499{
500 _LIBCPP_INLINE_VISIBILITY
501 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs)
502 {return __lhs.count() < __rhs.count();}
503};
504
505template <class _Rep1, class _Period1, class _Rep2, class _Period2>
506inline _LIBCPP_INLINE_VISIBILITY
507bool
508operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
509{
510 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
511}
512
513// Duration >
514
515template <class _Rep1, class _Period1, class _Rep2, class _Period2>
516inline _LIBCPP_INLINE_VISIBILITY
517bool
518operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
519{
520 return __rhs < __lhs;
521}
522
523// Duration <=
524
525template <class _Rep1, class _Period1, class _Rep2, class _Period2>
526inline _LIBCPP_INLINE_VISIBILITY
527bool
528operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
529{
530 return !(__rhs < __lhs);
531}
532
533// Duration >=
534
535template <class _Rep1, class _Period1, class _Rep2, class _Period2>
536inline _LIBCPP_INLINE_VISIBILITY
537bool
538operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
539{
540 return !(__lhs < __rhs);
541}
542
543// Duration +
544
545template <class _Rep1, class _Period1, class _Rep2, class _Period2>
546inline _LIBCPP_INLINE_VISIBILITY
547typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
548operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
549{
550 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs;
551 __r += __rhs;
552 return __r;
553}
554
555// Duration -
556
557template <class _Rep1, class _Period1, class _Rep2, class _Period2>
558inline _LIBCPP_INLINE_VISIBILITY
559typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
560operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
561{
562 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs;
563 __r -= __rhs;
564 return __r;
565}
566
567// Duration *
568
569template <class _Rep1, class _Period, class _Rep2>
570inline _LIBCPP_INLINE_VISIBILITY
571typename enable_if
572<
573 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
574 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
575>::type
576operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
577{
578 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
579 duration<_Cr, _Period> __r = __d;
580 __r *= static_cast<_Cr>(__s);
581 return __r;
582}
583
584template <class _Rep1, class _Period, class _Rep2>
585inline _LIBCPP_INLINE_VISIBILITY
586typename enable_if
587<
588 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
589 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
590>::type
591operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
592{
593 return __d * __s;
594}
595
596// Duration /
597
598template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
599struct __duration_divide_result
600{
601};
602
603template <class _Duration, class _Rep2,
604 bool = is_convertible<_Rep2,
605 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
606struct __duration_divide_imp
607{
608};
609
610template <class _Rep1, class _Period, class _Rep2>
611struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
612{
613 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
614};
615
616template <class _Rep1, class _Period, class _Rep2>
617struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
618 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
619{
620};
621
622template <class _Rep1, class _Period, class _Rep2>
623inline _LIBCPP_INLINE_VISIBILITY
624typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
625operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
626{
627 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
628 duration<_Cr, _Period> __r = __d;
629 __r /= static_cast<_Cr>(__s);
630 return __r;
631}
632
633template <class _Rep1, class _Period1, class _Rep2, class _Period2>
634inline _LIBCPP_INLINE_VISIBILITY
635typename common_type<_Rep1, _Rep2>::type
636operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
637{
638 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
639 return _Ct(__lhs).count() / _Ct(__rhs).count();
640}
641
642// Duration %
643
644template <class _Rep1, class _Period, class _Rep2>
645inline _LIBCPP_INLINE_VISIBILITY
646typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
647operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
648{
649 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
650 duration<_Cr, _Period> __r = __d;
651 __r %= static_cast<_Cr>(__s);
652 return __r;
653}
654
655template <class _Rep1, class _Period1, class _Rep2, class _Period2>
656inline _LIBCPP_INLINE_VISIBILITY
657typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
658operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
659{
660 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type __r = __lhs;
661 __r %= __rhs;
662 return __r;
663}
664
665//////////////////////////////////////////////////////////
666///////////////////// time_point /////////////////////////
667//////////////////////////////////////////////////////////
668
669template <class _Clock, class _Duration = typename _Clock::duration>
670class time_point
671{
672 static_assert(__is_duration<_Duration>::value,
673 "Second template parameter of time_point must be a std::chrono::duration");
674public:
675 typedef _Clock clock;
676 typedef _Duration duration;
677 typedef typename duration::rep rep;
678 typedef typename duration::period period;
679private:
680 duration __d_;
681
682public:
683 _LIBCPP_INLINE_VISIBILITY time_point() : __d_(duration::zero()) {}
684 _LIBCPP_INLINE_VISIBILITY explicit time_point(const duration& __d) : __d_(__d) {}
685
686 // conversions
687 template <class _Duration2>
688 _LIBCPP_INLINE_VISIBILITY
689 time_point(const time_point<clock, _Duration2>& t,
690 typename enable_if
691 <
692 is_convertible<_Duration2, duration>::value
693 >::type* = 0)
694 : __d_(t.time_since_epoch()) {}
695
696 // observer
697
698 _LIBCPP_INLINE_VISIBILITY duration time_since_epoch() const {return __d_;}
699
700 // arithmetic
701
702 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d;}
703 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d;}
704
705 // special values
706
707 _LIBCPP_INLINE_VISIBILITY static time_point min() {return time_point(duration::min());}
708 _LIBCPP_INLINE_VISIBILITY static time_point max() {return time_point(duration::max());}
709};
710
711} // chrono
712
713template <class _Clock, class _Duration1, class _Duration2>
714struct common_type<chrono::time_point<_Clock, _Duration1>, chrono::time_point<_Clock, _Duration2> >
715{
716 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
717};
718
719namespace chrono {
720
721template <class _ToDuration, class _Clock, class _Duration>
722inline _LIBCPP_INLINE_VISIBILITY
723time_point<_Clock, _ToDuration>
724time_point_cast(const time_point<_Clock, _Duration>& __t)
725{
726 return time_point<_Clock, _ToDuration>(_STD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
727}
728
729// time_point ==
730
731template <class _Clock, class _Duration1, class _Duration2>
732inline _LIBCPP_INLINE_VISIBILITY
733bool
734operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
735{
736 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
737}
738
739// time_point !=
740
741template <class _Clock, class _Duration1, class _Duration2>
742inline _LIBCPP_INLINE_VISIBILITY
743bool
744operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
745{
746 return !(__lhs == __rhs);
747}
748
749// time_point <
750
751template <class _Clock, class _Duration1, class _Duration2>
752inline _LIBCPP_INLINE_VISIBILITY
753bool
754operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
755{
756 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
757}
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 __rhs < __lhs;
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 !(__rhs < __lhs);
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 < __rhs);
787}
788
789// time_point operator+(time_point x, duration y);
790
791template <class _Clock, class _Duration1, class _Rep2, class _Period2>
792inline _LIBCPP_INLINE_VISIBILITY
793time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
794operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
795{
796 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
797 _Tr __r(__lhs.time_since_epoch());
798 __r += __rhs;
799 return __r;
800}
801
802// time_point operator+(duration x, time_point y);
803
804template <class _Rep1, class _Period1, class _Clock, class _Duration2>
805inline _LIBCPP_INLINE_VISIBILITY
806time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
807operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
808{
809 return __rhs + __lhs;
810}
811
812// time_point operator-(time_point x, duration y);
813
814template <class _Clock, class _Duration1, class _Rep2, class _Period2>
815inline _LIBCPP_INLINE_VISIBILITY
816time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
817operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
818{
819 return __lhs + (-__rhs);
820}
821
822// duration operator-(time_point x, time_point y);
823
824template <class _Clock, class _Duration1, class _Duration2>
825inline _LIBCPP_INLINE_VISIBILITY
826typename common_type<_Duration1, _Duration2>::type
827operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
828{
829 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
830}
831
832//////////////////////////////////////////////////////////
833/////////////////////// clocks ///////////////////////////
834//////////////////////////////////////////////////////////
835
836class system_clock
837{
838public:
839 typedef microseconds duration;
840 typedef duration::rep rep;
841 typedef duration::period period;
842 typedef chrono::time_point<system_clock> time_point;
843 static const bool is_monotonic = false;
844
845 static time_point now();
846 static time_t to_time_t (const time_point& __t);
847 static time_point from_time_t(time_t __t);
848};
849
850class monotonic_clock
851{
852public:
853 typedef nanoseconds duration;
854 typedef duration::rep rep;
855 typedef duration::period period;
856 typedef chrono::time_point<monotonic_clock, duration> time_point;
857 static const bool is_monotonic = true;
858
859 static time_point now();
860};
861
862typedef monotonic_clock high_resolution_clock;
863
864} // chrono
865
866_LIBCPP_END_NAMESPACE_STD
867
868#endif // _LIBCPP_CHRONO