chrono revision 251066
1// -*- C++ -*-
2//===---------------------------- chrono ----------------------------------===//
3//
4//                     The LLVM Compiler Infrastructure
5//
6// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. 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>
23constexpr
24ToDuration
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:
33    static constexpr Rep zero();
34    static constexpr Rep max();
35    static constexpr Rep min();
36};
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
50    constexpr duration() = default;
51    template <class Rep2>
52        constexpr explicit duration(const Rep2& r,
53            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>
62        constexpr duration(const duration<Rep2, Period2>& d,
63            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
71    constexpr rep count() const;
72
73    // arithmetic
74
75    constexpr duration  operator+() const;
76    constexpr duration  operator-() const;
77    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
90    static constexpr duration zero();
91    static constexpr duration min();
92    static constexpr duration max();
93};
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>
149  constexpr
150  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>
153  constexpr
154  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>
157  constexpr
158  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>
161  constexpr
162  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>
165  constexpr
166  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>
169  constexpr
170  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>
175   constexpr
176   bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
177template <class Rep1, class Period1, class Rep2, class Period2>
178   constexpr
179   bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
180template <class Rep1, class Period1, class Rep2, class Period2>
181   constexpr
182   bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
183template <class Rep1, class Period1, class Rep2, class Period2>
184   constexpr
185   bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
186template <class Rep1, class Period1, class Rep2, class Period2>
187   constexpr
188   bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
189template <class Rep1, class Period1, class Rep2, class Period2>
190   constexpr
191   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;
239    static const bool is_steady =            false;
240
241    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;
244};
245
246class steady_clock
247{
248public:
249    typedef nanoseconds                                   duration;
250    typedef duration::rep                                 rep;
251    typedef duration::period                              period;
252    typedef chrono::time_point<steady_clock, duration>    time_point;
253    static const bool is_steady =                         true;
254
255    static time_point now() noexcept;
256};
257
258typedef steady_clock high_resolution_clock;
259
260}  // chrono
261
262}  // std
263*/
264
265#include <__config>
266#include <ctime>
267#include <type_traits>
268#include <ratio>
269#include <limits>
270
271#include <__undef_min_max>
272
273#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
274#pragma GCC system_header
275#endif
276
277_LIBCPP_BEGIN_NAMESPACE_STD
278
279namespace chrono
280{
281
282template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TYPE_VIS duration;
283
284template <class _Tp>
285struct __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>
302struct _LIBCPP_TYPE_VIS common_type<chrono::duration<_Rep1, _Period1>,
303                                   chrono::duration<_Rep2, _Period2> >
304{
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{
322    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
323    _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{
332    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
333    _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{
344    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
345    _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{
356    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
357    _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
368_LIBCPP_CONSTEXPR
369typename 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
379template <class _Rep>
380struct _LIBCPP_TYPE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
381
382template <class _Rep>
383struct _LIBCPP_TYPE_VIS duration_values
384{
385public:
386    _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();}
389};
390
391// duration
392
393template <class _Rep, class _Period>
394class _LIBCPP_TYPE_VIS duration
395{
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
406    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration() {} // = default;
407    template <class _Rep2>
408        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
409        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>
420        _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
421        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)
428                : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
429
430    // observer
431
432    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
433
434    // arithmetic
435
436    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration  operator+() const {return *this;}
437    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR duration  operator-() const {return duration(-__rep_);}
438    _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
453    _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());}
456};
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{
470    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
471    bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
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{
481    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
482    bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
483        {return __lhs.count() == __rhs.count();}
484};
485
486template <class _Rep1, class _Period1, class _Rep2, class _Period2>
487inline _LIBCPP_INLINE_VISIBILITY
488_LIBCPP_CONSTEXPR
489bool
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
499_LIBCPP_CONSTEXPR
500bool
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{
511    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
512    bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
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{
522    _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
523    bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
524        {return __lhs.count() < __rhs.count();}
525};
526
527template <class _Rep1, class _Period1, class _Rep2, class _Period2>
528inline _LIBCPP_INLINE_VISIBILITY
529_LIBCPP_CONSTEXPR
530bool
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
540_LIBCPP_CONSTEXPR
541bool
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
551_LIBCPP_CONSTEXPR
552bool
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
562_LIBCPP_CONSTEXPR
563bool
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
573_LIBCPP_CONSTEXPR
574typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
575operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
576{
577    typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
578    return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
579}
580
581// Duration -
582
583template <class _Rep1, class _Period1, class _Rep2, class _Period2>
584inline _LIBCPP_INLINE_VISIBILITY
585_LIBCPP_CONSTEXPR
586typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
587operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
588{
589    typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
590    return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
591}
592
593// Duration *
594
595template <class _Rep1, class _Period, class _Rep2>
596inline _LIBCPP_INLINE_VISIBILITY
597_LIBCPP_CONSTEXPR
598typename 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;
606    typedef duration<_Cr, _Period> _Cd;
607    return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
608}
609
610template <class _Rep1, class _Period, class _Rep2>
611inline _LIBCPP_INLINE_VISIBILITY
612_LIBCPP_CONSTEXPR
613typename 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
651_LIBCPP_CONSTEXPR
652typename __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;
656    typedef duration<_Cr, _Period> _Cd;
657    return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
658}
659
660template <class _Rep1, class _Period1, class _Rep2, class _Period2>
661inline _LIBCPP_INLINE_VISIBILITY
662_LIBCPP_CONSTEXPR
663typename 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
674_LIBCPP_CONSTEXPR
675typename __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;
679    typedef duration<_Cr, _Period> _Cd;
680    return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
681}
682
683template <class _Rep1, class _Period1, class _Rep2, class _Period2>
684inline _LIBCPP_INLINE_VISIBILITY
685_LIBCPP_CONSTEXPR
686typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
687operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
688{
689    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()));
692}
693
694//////////////////////////////////////////////////////////
695///////////////////// time_point /////////////////////////
696//////////////////////////////////////////////////////////
697
698template <class _Clock, class _Duration = typename _Clock::duration>
699class _LIBCPP_TYPE_VIS time_point
700{
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
731	_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;}
733
734    // special values
735
736    _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());}
738};
739
740} // chrono
741
742template <class _Clock, class _Duration1, class _Duration2>
743struct _LIBCPP_TYPE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
744                                   chrono::time_point<_Clock, _Duration2> >
745{
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{
756    return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.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 __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
866class _LIBCPP_TYPE_VIS system_clock
867{
868public:
869    typedef microseconds                     duration;
870    typedef duration::rep                    rep;
871    typedef duration::period                 period;
872    typedef chrono::time_point<system_clock> time_point;
873    static const bool is_steady =            false;
874
875    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;
878};
879
880class _LIBCPP_TYPE_VIS steady_clock
881{
882public:
883    typedef nanoseconds                                   duration;
884    typedef duration::rep                                 rep;
885    typedef duration::period                              period;
886    typedef chrono::time_point<steady_clock, duration>    time_point;
887    static const bool is_steady =                         true;
888
889    static time_point now() _NOEXCEPT;
890};
891
892typedef steady_clock high_resolution_clock;
893
894} // chrono
895
896_LIBCPP_END_NAMESPACE_STD
897
898#endif  // _LIBCPP_CHRONO
899