2 //===---------------------------- chrono ----------------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_CHRONO
12 #define _LIBCPP_CHRONO
22 template <class ToDuration, class Rep, class Period>
25 duration_cast(const duration<Rep, Period>& fd);
27 template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {};
29 template <class Rep> constexpr bool treat_as_floating_point_v
30 = treat_as_floating_point<Rep>::value; // C++17
33 struct duration_values
36 static constexpr Rep zero();
37 static constexpr Rep max();
38 static constexpr Rep min();
43 template <class Rep, class Period = ratio<1>>
46 static_assert(!__is_duration<Rep>::value, "A duration representation can not be a duration");
47 static_assert(__is_ratio<Period>::value, "Second template parameter of duration must be a std::ratio");
48 static_assert(Period::num > 0, "duration period must be positive");
51 typedef typename _Period::type period;
53 constexpr duration() = default;
55 constexpr explicit duration(const Rep2& r,
58 is_convertible<Rep2, rep>::value &&
59 (treat_as_floating_point<rep>::value ||
60 !treat_as_floating_point<rep>::value && !treat_as_floating_point<Rep2>::value)
64 template <class Rep2, class Period2>
65 constexpr duration(const duration<Rep2, Period2>& d,
68 treat_as_floating_point<rep>::value ||
69 ratio_divide<Period2, period>::type::den == 1
74 constexpr rep count() const;
78 constexpr common_type<duration>::type operator+() const;
79 constexpr common_type<duration>::type operator-() const;
80 constexpr duration& operator++();
81 constexpr duration operator++(int);
82 constexpr duration& operator--();
83 constexpr duration operator--(int);
85 constexpr duration& operator+=(const duration& d);
86 constexpr duration& operator-=(const duration& d);
88 duration& operator*=(const rep& rhs);
89 duration& operator/=(const rep& rhs);
93 static constexpr duration zero();
94 static constexpr duration min();
95 static constexpr duration max();
98 typedef duration<long long, nano> nanoseconds;
99 typedef duration<long long, micro> microseconds;
100 typedef duration<long long, milli> milliseconds;
101 typedef duration<long long > seconds;
102 typedef duration< long, ratio< 60> > minutes;
103 typedef duration< long, ratio<3600> > hours;
105 template <class Clock, class Duration = typename Clock::duration>
110 typedef Duration duration;
111 typedef typename duration::rep rep;
112 typedef typename duration::period period;
114 duration d_; // exposition only
117 time_point(); // has value "epoch" // constexpr in C++14
118 explicit time_point(const duration& d); // same as time_point() + d // constexpr in C++14
121 template <class Duration2>
122 time_point(const time_point<clock, Duration2>& t); // constexpr in C++14
126 duration time_since_epoch() const; // constexpr in C++14
130 time_point& operator+=(const duration& d);
131 time_point& operator-=(const duration& d);
135 static constexpr time_point min();
136 static constexpr time_point max();
141 // common_type traits
142 template <class Rep1, class Period1, class Rep2, class Period2>
143 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>;
145 template <class Clock, class Duration1, class Duration2>
146 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>;
150 // duration arithmetic
151 template <class Rep1, class Period1, class Rep2, class Period2>
153 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
154 operator+(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
155 template <class Rep1, class Period1, class Rep2, class Period2>
157 typename common_type<duration<Rep1, Period1>, duration<Rep2, Period2>>::type
158 operator-(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
159 template <class Rep1, class Period, class Rep2>
161 duration<typename common_type<Rep1, Rep2>::type, Period>
162 operator*(const duration<Rep1, Period>& d, const Rep2& s);
163 template <class Rep1, class Period, class Rep2>
165 duration<typename common_type<Rep1, Rep2>::type, Period>
166 operator*(const Rep1& s, const duration<Rep2, Period>& d);
167 template <class Rep1, class Period, class Rep2>
169 duration<typename common_type<Rep1, Rep2>::type, Period>
170 operator/(const duration<Rep1, Period>& d, const Rep2& s);
171 template <class Rep1, class Period1, class Rep2, class Period2>
173 typename common_type<Rep1, Rep2>::type
174 operator/(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
176 // duration comparisons
177 template <class Rep1, class Period1, class Rep2, class Period2>
179 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
180 template <class Rep1, class Period1, class Rep2, class Period2>
182 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
183 template <class Rep1, class Period1, class Rep2, class Period2>
185 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
186 template <class Rep1, class Period1, class Rep2, class Period2>
188 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
189 template <class Rep1, class Period1, class Rep2, class Period2>
191 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
192 template <class Rep1, class Period1, class Rep2, class Period2>
194 bool operator>=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs);
197 template <class ToDuration, class Rep, class Period>
198 ToDuration duration_cast(const duration<Rep, Period>& d);
200 template <class ToDuration, class Rep, class Period>
201 constexpr ToDuration floor(const duration<Rep, Period>& d); // C++17
202 template <class ToDuration, class Rep, class Period>
203 constexpr ToDuration ceil(const duration<Rep, Period>& d); // C++17
204 template <class ToDuration, class Rep, class Period>
205 constexpr ToDuration round(const duration<Rep, Period>& d); // C++17
207 // time_point arithmetic (all constexpr in C++14)
208 template <class Clock, class Duration1, class Rep2, class Period2>
209 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
210 operator+(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
211 template <class Rep1, class Period1, class Clock, class Duration2>
212 time_point<Clock, typename common_type<duration<Rep1, Period1>, Duration2>::type>
213 operator+(const duration<Rep1, Period1>& lhs, const time_point<Clock, Duration2>& rhs);
214 template <class Clock, class Duration1, class Rep2, class Period2>
215 time_point<Clock, typename common_type<Duration1, duration<Rep2, Period2>>::type>
216 operator-(const time_point<Clock, Duration1>& lhs, const duration<Rep2, Period2>& rhs);
217 template <class Clock, class Duration1, class Duration2>
218 typename common_type<Duration1, Duration2>::type
219 operator-(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
221 // time_point comparisons (all constexpr in C++14)
222 template <class Clock, class Duration1, class Duration2>
223 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
224 template <class Clock, class Duration1, class Duration2>
225 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
226 template <class Clock, class Duration1, class Duration2>
227 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
228 template <class Clock, class Duration1, class Duration2>
229 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
230 template <class Clock, class Duration1, class Duration2>
231 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
232 template <class Clock, class Duration1, class Duration2>
233 bool operator>=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs);
235 // time_point_cast (constexpr in C++14)
237 template <class ToDuration, class Clock, class Duration>
238 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t);
240 template <class ToDuration, class Clock, class Duration>
241 constexpr time_point<Clock, ToDuration>
242 floor(const time_point<Clock, Duration>& tp); // C++17
244 template <class ToDuration, class Clock, class Duration>
245 constexpr time_point<Clock, ToDuration>
246 ceil(const time_point<Clock, Duration>& tp); // C++17
248 template <class ToDuration, class Clock, class Duration>
249 constexpr time_point<Clock, ToDuration>
250 round(const time_point<Clock, Duration>& tp); // C++17
252 template <class Rep, class Period>
253 constexpr duration<Rep, Period> abs(duration<Rep, Period> d); // C++17
259 typedef microseconds duration;
260 typedef duration::rep rep;
261 typedef duration::period period;
262 typedef chrono::time_point<system_clock> time_point;
263 static const bool is_steady = false; // constexpr in C++14
265 static time_point now() noexcept;
266 static time_t to_time_t (const time_point& __t) noexcept;
267 static time_point from_time_t(time_t __t) noexcept;
273 typedef nanoseconds duration;
274 typedef duration::rep rep;
275 typedef duration::period period;
276 typedef chrono::time_point<steady_clock, duration> time_point;
277 static const bool is_steady = true; // constexpr in C++14
279 static time_point now() noexcept;
282 typedef steady_clock high_resolution_clock;
286 constexpr chrono::hours operator "" h(unsigned long long); // C++14
287 constexpr chrono::duration<unspecified , ratio<3600,1>> operator "" h(long double); // C++14
288 constexpr chrono::minutes operator "" min(unsigned long long); // C++14
289 constexpr chrono::duration<unspecified , ratio<60,1>> operator "" min(long double); // C++14
290 constexpr chrono::seconds operator "" s(unsigned long long); // C++14
291 constexpr chrono::duration<unspecified > operator "" s(long double); // C++14
292 constexpr chrono::milliseconds operator "" ms(unsigned long long); // C++14
293 constexpr chrono::duration<unspecified , milli> operator "" ms(long double); // C++14
294 constexpr chrono::microseconds operator "" us(unsigned long long); // C++14
295 constexpr chrono::duration<unspecified , micro> operator "" us(long double); // C++14
296 constexpr chrono::nanoseconds operator "" ns(unsigned long long); // C++14
297 constexpr chrono::duration<unspecified , nano> operator "" ns(long double); // C++14
304 #include <type_traits>
308 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
309 #pragma GCC system_header
313 #include <__undef_macros>
316 _LIBCPP_BEGIN_NAMESPACE_STD
321 template <class _Rep, class _Period = ratio<1> > class _LIBCPP_TEMPLATE_VIS duration;
324 struct __is_duration : false_type {};
326 template <class _Rep, class _Period>
327 struct __is_duration<duration<_Rep, _Period> > : true_type {};
329 template <class _Rep, class _Period>
330 struct __is_duration<const duration<_Rep, _Period> > : true_type {};
332 template <class _Rep, class _Period>
333 struct __is_duration<volatile duration<_Rep, _Period> > : true_type {};
335 template <class _Rep, class _Period>
336 struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {};
340 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
341 struct _LIBCPP_TEMPLATE_VIS common_type<chrono::duration<_Rep1, _Period1>,
342 chrono::duration<_Rep2, _Period2> >
344 typedef chrono::duration<typename common_type<_Rep1, _Rep2>::type,
345 typename __ratio_gcd<_Period1, _Period2>::type> type;
352 template <class _FromDuration, class _ToDuration,
353 class _Period = typename ratio_divide<typename _FromDuration::period, typename _ToDuration::period>::type,
354 bool = _Period::num == 1,
355 bool = _Period::den == 1>
356 struct __duration_cast;
358 template <class _FromDuration, class _ToDuration, class _Period>
359 struct __duration_cast<_FromDuration, _ToDuration, _Period, true, true>
361 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
362 _ToDuration operator()(const _FromDuration& __fd) const
364 return _ToDuration(static_cast<typename _ToDuration::rep>(__fd.count()));
368 template <class _FromDuration, class _ToDuration, class _Period>
369 struct __duration_cast<_FromDuration, _ToDuration, _Period, true, false>
371 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
372 _ToDuration operator()(const _FromDuration& __fd) const
374 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
375 return _ToDuration(static_cast<typename _ToDuration::rep>(
376 static_cast<_Ct>(__fd.count()) / static_cast<_Ct>(_Period::den)));
380 template <class _FromDuration, class _ToDuration, class _Period>
381 struct __duration_cast<_FromDuration, _ToDuration, _Period, false, true>
383 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
384 _ToDuration operator()(const _FromDuration& __fd) const
386 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
387 return _ToDuration(static_cast<typename _ToDuration::rep>(
388 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)));
392 template <class _FromDuration, class _ToDuration, class _Period>
393 struct __duration_cast<_FromDuration, _ToDuration, _Period, false, false>
395 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
396 _ToDuration operator()(const _FromDuration& __fd) const
398 typedef typename common_type<typename _ToDuration::rep, typename _FromDuration::rep, intmax_t>::type _Ct;
399 return _ToDuration(static_cast<typename _ToDuration::rep>(
400 static_cast<_Ct>(__fd.count()) * static_cast<_Ct>(_Period::num)
401 / static_cast<_Ct>(_Period::den)));
405 template <class _ToDuration, class _Rep, class _Period>
406 inline _LIBCPP_INLINE_VISIBILITY
410 __is_duration<_ToDuration>::value,
413 duration_cast(const duration<_Rep, _Period>& __fd)
415 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd);
418 template <class _Rep>
419 struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
421 #if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
422 template <class _Rep> _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
423 = treat_as_floating_point<_Rep>::value;
426 template <class _Rep>
427 struct _LIBCPP_TEMPLATE_VIS duration_values
430 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
431 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
432 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
435 #if _LIBCPP_STD_VER > 14
436 template <class _ToDuration, class _Rep, class _Period>
437 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
440 __is_duration<_ToDuration>::value,
443 floor(const duration<_Rep, _Period>& __d)
445 _ToDuration __t = duration_cast<_ToDuration>(__d);
447 __t = __t - _ToDuration{1};
451 template <class _ToDuration, class _Rep, class _Period>
452 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
455 __is_duration<_ToDuration>::value,
458 ceil(const duration<_Rep, _Period>& __d)
460 _ToDuration __t = duration_cast<_ToDuration>(__d);
462 __t = __t + _ToDuration{1};
466 template <class _ToDuration, class _Rep, class _Period>
467 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
470 __is_duration<_ToDuration>::value,
473 round(const duration<_Rep, _Period>& __d)
475 _ToDuration __lower = floor<_ToDuration>(__d);
476 _ToDuration __upper = __lower + _ToDuration{1};
477 auto __lowerDiff = __d - __lower;
478 auto __upperDiff = __upper - __d;
479 if (__lowerDiff < __upperDiff)
481 if (__lowerDiff > __upperDiff)
483 return __lower.count() & 1 ? __upper : __lower;
489 template <class _Rep, class _Period>
490 class _LIBCPP_TEMPLATE_VIS duration
492 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
493 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
494 static_assert(_Period::num > 0, "duration period must be positive");
496 template <class _R1, class _R2>
500 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
501 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
502 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
503 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
504 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
505 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
506 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
508 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
509 struct __mul // __overflow == false
511 static const intmax_t value = _Xp * _Yp;
514 template <intmax_t _Xp, intmax_t _Yp>
515 struct __mul<_Xp, _Yp, true>
517 static const intmax_t value = 1;
521 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
522 typedef ratio<__mul<__n1, __d2, !value>::value,
523 __mul<__n2, __d1, !value>::value> type;
528 typedef typename _Period::type period;
533 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
534 #ifndef _LIBCPP_CXX03_LANG
535 duration() = default;
540 template <class _Rep2>
541 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
542 explicit duration(const _Rep2& __r,
545 is_convertible<_Rep2, rep>::value &&
546 (treat_as_floating_point<rep>::value ||
547 !treat_as_floating_point<_Rep2>::value)
552 template <class _Rep2, class _Period2>
553 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
554 duration(const duration<_Rep2, _Period2>& __d,
557 __no_overflow<_Period2, period>::value && (
558 treat_as_floating_point<rep>::value ||
559 (__no_overflow<_Period2, period>::type::den == 1 &&
560 !treat_as_floating_point<_Rep2>::value))
562 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
566 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
570 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
571 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
572 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
574 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
575 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
577 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); return *this;}
580 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator*=(const rep& rhs) {__rep_ *= rhs; return *this;}
581 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator/=(const rep& rhs) {__rep_ /= rhs; return *this;}
582 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const rep& rhs) {__rep_ %= rhs; return *this;}
583 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
587 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
588 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
589 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
592 typedef duration<long long, nano> nanoseconds;
593 typedef duration<long long, micro> microseconds;
594 typedef duration<long long, milli> milliseconds;
595 typedef duration<long long > seconds;
596 typedef duration< long, ratio< 60> > minutes;
597 typedef duration< long, ratio<3600> > hours;
601 template <class _LhsDuration, class _RhsDuration>
604 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
605 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
607 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
608 return _Ct(__lhs).count() == _Ct(__rhs).count();
612 template <class _LhsDuration>
613 struct __duration_eq<_LhsDuration, _LhsDuration>
615 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
616 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
617 {return __lhs.count() == __rhs.count();}
620 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
621 inline _LIBCPP_INLINE_VISIBILITY
624 operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
626 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
631 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
632 inline _LIBCPP_INLINE_VISIBILITY
635 operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
637 return !(__lhs == __rhs);
642 template <class _LhsDuration, class _RhsDuration>
645 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
646 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
648 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
649 return _Ct(__lhs).count() < _Ct(__rhs).count();
653 template <class _LhsDuration>
654 struct __duration_lt<_LhsDuration, _LhsDuration>
656 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
657 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
658 {return __lhs.count() < __rhs.count();}
661 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
662 inline _LIBCPP_INLINE_VISIBILITY
665 operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
667 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
672 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
673 inline _LIBCPP_INLINE_VISIBILITY
676 operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
678 return __rhs < __lhs;
683 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
684 inline _LIBCPP_INLINE_VISIBILITY
687 operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
689 return !(__rhs < __lhs);
694 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
695 inline _LIBCPP_INLINE_VISIBILITY
698 operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
700 return !(__lhs < __rhs);
705 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
706 inline _LIBCPP_INLINE_VISIBILITY
708 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
709 operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
711 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
712 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
717 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
718 inline _LIBCPP_INLINE_VISIBILITY
720 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
721 operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
723 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
724 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
729 template <class _Rep1, class _Period, class _Rep2>
730 inline _LIBCPP_INLINE_VISIBILITY
734 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
735 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
737 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
739 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
740 typedef duration<_Cr, _Period> _Cd;
741 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
744 template <class _Rep1, class _Period, class _Rep2>
745 inline _LIBCPP_INLINE_VISIBILITY
749 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
750 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
752 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
759 template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
760 struct __duration_divide_result
764 template <class _Duration, class _Rep2,
765 bool = is_convertible<_Rep2,
766 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
767 struct __duration_divide_imp
771 template <class _Rep1, class _Period, class _Rep2>
772 struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
774 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
777 template <class _Rep1, class _Period, class _Rep2>
778 struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
779 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
783 template <class _Rep1, class _Period, class _Rep2>
784 inline _LIBCPP_INLINE_VISIBILITY
786 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
787 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
789 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
790 typedef duration<_Cr, _Period> _Cd;
791 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
794 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
795 inline _LIBCPP_INLINE_VISIBILITY
797 typename common_type<_Rep1, _Rep2>::type
798 operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
800 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
801 return _Ct(__lhs).count() / _Ct(__rhs).count();
806 template <class _Rep1, class _Period, class _Rep2>
807 inline _LIBCPP_INLINE_VISIBILITY
809 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
810 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
812 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
813 typedef duration<_Cr, _Period> _Cd;
814 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
817 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
818 inline _LIBCPP_INLINE_VISIBILITY
820 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
821 operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
823 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
824 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
825 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
828 //////////////////////////////////////////////////////////
829 ///////////////////// time_point /////////////////////////
830 //////////////////////////////////////////////////////////
832 template <class _Clock, class _Duration = typename _Clock::duration>
833 class _LIBCPP_TEMPLATE_VIS time_point
835 static_assert(__is_duration<_Duration>::value,
836 "Second template parameter of time_point must be a std::chrono::duration");
838 typedef _Clock clock;
839 typedef _Duration duration;
840 typedef typename duration::rep rep;
841 typedef typename duration::period period;
846 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
847 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
850 template <class _Duration2>
851 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
852 time_point(const time_point<clock, _Duration2>& t,
855 is_convertible<_Duration2, duration>::value
857 : __d_(t.time_since_epoch()) {}
861 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
865 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
866 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
870 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
871 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
876 template <class _Clock, class _Duration1, class _Duration2>
877 struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
878 chrono::time_point<_Clock, _Duration2> >
880 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
885 template <class _ToDuration, class _Clock, class _Duration>
886 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
887 time_point<_Clock, _ToDuration>
888 time_point_cast(const time_point<_Clock, _Duration>& __t)
890 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
893 #if _LIBCPP_STD_VER > 14
894 template <class _ToDuration, class _Clock, class _Duration>
895 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
898 __is_duration<_ToDuration>::value,
899 time_point<_Clock, _ToDuration>
901 floor(const time_point<_Clock, _Duration>& __t)
903 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
906 template <class _ToDuration, class _Clock, class _Duration>
907 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
910 __is_duration<_ToDuration>::value,
911 time_point<_Clock, _ToDuration>
913 ceil(const time_point<_Clock, _Duration>& __t)
915 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
918 template <class _ToDuration, class _Clock, class _Duration>
919 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
922 __is_duration<_ToDuration>::value,
923 time_point<_Clock, _ToDuration>
925 round(const time_point<_Clock, _Duration>& __t)
927 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
930 template <class _Rep, class _Period>
931 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
934 numeric_limits<_Rep>::is_signed,
935 duration<_Rep, _Period>
937 abs(duration<_Rep, _Period> __d)
939 return __d >= __d.zero() ? __d : -__d;
945 template <class _Clock, class _Duration1, class _Duration2>
946 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
948 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
950 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
955 template <class _Clock, class _Duration1, class _Duration2>
956 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
958 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
960 return !(__lhs == __rhs);
965 template <class _Clock, class _Duration1, class _Duration2>
966 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
968 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
970 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
975 template <class _Clock, class _Duration1, class _Duration2>
976 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
978 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
980 return __rhs < __lhs;
985 template <class _Clock, class _Duration1, class _Duration2>
986 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
988 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
990 return !(__rhs < __lhs);
995 template <class _Clock, class _Duration1, class _Duration2>
996 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
998 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1000 return !(__lhs < __rhs);
1003 // time_point operator+(time_point x, duration y);
1005 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
1006 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1007 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1008 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1010 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
1011 return _Tr (__lhs.time_since_epoch() + __rhs);
1014 // time_point operator+(duration x, time_point y);
1016 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
1017 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1018 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1019 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1021 return __rhs + __lhs;
1024 // time_point operator-(time_point x, duration y);
1026 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
1027 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1028 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1029 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1031 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1032 return _Ret(__lhs.time_since_epoch() -__rhs);
1035 // duration operator-(time_point x, time_point y);
1037 template <class _Clock, class _Duration1, class _Duration2>
1038 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1039 typename common_type<_Duration1, _Duration2>::type
1040 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1042 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1045 //////////////////////////////////////////////////////////
1046 /////////////////////// clocks ///////////////////////////
1047 //////////////////////////////////////////////////////////
1049 class _LIBCPP_TYPE_VIS system_clock
1052 typedef microseconds duration;
1053 typedef duration::rep rep;
1054 typedef duration::period period;
1055 typedef chrono::time_point<system_clock> time_point;
1056 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
1058 static time_point now() _NOEXCEPT;
1059 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1060 static time_point from_time_t(time_t __t) _NOEXCEPT;
1063 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1064 class _LIBCPP_TYPE_VIS steady_clock
1067 typedef nanoseconds duration;
1068 typedef duration::rep rep;
1069 typedef duration::period period;
1070 typedef chrono::time_point<steady_clock, duration> time_point;
1071 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
1073 static time_point now() _NOEXCEPT;
1076 typedef steady_clock high_resolution_clock;
1078 typedef system_clock high_resolution_clock;
1083 #if _LIBCPP_STD_VER > 11
1084 // Suffixes for duration literals [time.duration.literals]
1085 inline namespace literals
1087 inline namespace chrono_literals
1090 constexpr chrono::hours operator"" h(unsigned long long __h)
1092 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1095 constexpr chrono::duration<long double, ratio<3600,1>> operator"" h(long double __h)
1097 return chrono::duration<long double, ratio<3600,1>>(__h);
1101 constexpr chrono::minutes operator"" min(unsigned long long __m)
1103 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1106 constexpr chrono::duration<long double, ratio<60,1>> operator"" min(long double __m)
1108 return chrono::duration<long double, ratio<60,1>> (__m);
1112 constexpr chrono::seconds operator"" s(unsigned long long __s)
1114 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1117 constexpr chrono::duration<long double> operator"" s(long double __s)
1119 return chrono::duration<long double> (__s);
1123 constexpr chrono::milliseconds operator"" ms(unsigned long long __ms)
1125 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1128 constexpr chrono::duration<long double, milli> operator"" ms(long double __ms)
1130 return chrono::duration<long double, milli>(__ms);
1134 constexpr chrono::microseconds operator"" us(unsigned long long __us)
1136 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1139 constexpr chrono::duration<long double, micro> operator"" us(long double __us)
1141 return chrono::duration<long double, micro> (__us);
1145 constexpr chrono::nanoseconds operator"" ns(unsigned long long __ns)
1147 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1150 constexpr chrono::duration<long double, nano> operator"" ns(long double __ns)
1152 return chrono::duration<long double, nano> (__ns);
1157 namespace chrono { // hoist the literals into namespace std::chrono
1158 using namespace literals::chrono_literals;
1163 _LIBCPP_END_NAMESPACE_STD
1167 #endif // _LIBCPP_CHRONO