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> inline 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>
423 _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
424 = treat_as_floating_point<_Rep>::value;
427 template <class _Rep>
428 struct _LIBCPP_TEMPLATE_VIS duration_values
431 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep zero() {return _Rep(0);}
432 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep max() {return numeric_limits<_Rep>::max();}
433 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR _Rep min() {return numeric_limits<_Rep>::lowest();}
436 #if _LIBCPP_STD_VER > 14
437 template <class _ToDuration, class _Rep, class _Period>
438 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
441 __is_duration<_ToDuration>::value,
444 floor(const duration<_Rep, _Period>& __d)
446 _ToDuration __t = duration_cast<_ToDuration>(__d);
448 __t = __t - _ToDuration{1};
452 template <class _ToDuration, class _Rep, class _Period>
453 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
456 __is_duration<_ToDuration>::value,
459 ceil(const duration<_Rep, _Period>& __d)
461 _ToDuration __t = duration_cast<_ToDuration>(__d);
463 __t = __t + _ToDuration{1};
467 template <class _ToDuration, class _Rep, class _Period>
468 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
471 __is_duration<_ToDuration>::value,
474 round(const duration<_Rep, _Period>& __d)
476 _ToDuration __lower = floor<_ToDuration>(__d);
477 _ToDuration __upper = __lower + _ToDuration{1};
478 auto __lowerDiff = __d - __lower;
479 auto __upperDiff = __upper - __d;
480 if (__lowerDiff < __upperDiff)
482 if (__lowerDiff > __upperDiff)
484 return __lower.count() & 1 ? __upper : __lower;
490 template <class _Rep, class _Period>
491 class _LIBCPP_TEMPLATE_VIS duration
493 static_assert(!__is_duration<_Rep>::value, "A duration representation can not be a duration");
494 static_assert(__is_ratio<_Period>::value, "Second template parameter of duration must be a std::ratio");
495 static_assert(_Period::num > 0, "duration period must be positive");
497 template <class _R1, class _R2>
501 static const intmax_t __gcd_n1_n2 = __static_gcd<_R1::num, _R2::num>::value;
502 static const intmax_t __gcd_d1_d2 = __static_gcd<_R1::den, _R2::den>::value;
503 static const intmax_t __n1 = _R1::num / __gcd_n1_n2;
504 static const intmax_t __d1 = _R1::den / __gcd_d1_d2;
505 static const intmax_t __n2 = _R2::num / __gcd_n1_n2;
506 static const intmax_t __d2 = _R2::den / __gcd_d1_d2;
507 static const intmax_t max = -((intmax_t(1) << (sizeof(intmax_t) * CHAR_BIT - 1)) + 1);
509 template <intmax_t _Xp, intmax_t _Yp, bool __overflow>
510 struct __mul // __overflow == false
512 static const intmax_t value = _Xp * _Yp;
515 template <intmax_t _Xp, intmax_t _Yp>
516 struct __mul<_Xp, _Yp, true>
518 static const intmax_t value = 1;
522 static const bool value = (__n1 <= max / __d2) && (__n2 <= max / __d1);
523 typedef ratio<__mul<__n1, __d2, !value>::value,
524 __mul<__n2, __d1, !value>::value> type;
529 typedef typename _Period::type period;
534 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
535 #ifndef _LIBCPP_CXX03_LANG
536 duration() = default;
541 template <class _Rep2>
542 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
543 explicit duration(const _Rep2& __r,
546 is_convertible<_Rep2, rep>::value &&
547 (treat_as_floating_point<rep>::value ||
548 !treat_as_floating_point<_Rep2>::value)
553 template <class _Rep2, class _Period2>
554 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
555 duration(const duration<_Rep2, _Period2>& __d,
558 __no_overflow<_Period2, period>::value && (
559 treat_as_floating_point<rep>::value ||
560 (__no_overflow<_Period2, period>::type::den == 1 &&
561 !treat_as_floating_point<_Rep2>::value))
563 : __rep_(_VSTD::chrono::duration_cast<duration>(__d).count()) {}
567 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR rep count() const {return __rep_;}
571 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator+() const {return typename common_type<duration>::type(*this);}
572 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename common_type<duration>::type operator-() const {return typename common_type<duration>::type(-__rep_);}
573 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator++() {++__rep_; return *this;}
574 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator++(int) {return duration(__rep_++);}
575 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator--() {--__rep_; return *this;}
576 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration operator--(int) {return duration(__rep_--);}
578 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator+=(const duration& __d) {__rep_ += __d.count(); return *this;}
579 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator-=(const duration& __d) {__rep_ -= __d.count(); 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 rep& rhs) {__rep_ %= rhs; return *this;}
584 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14 duration& operator%=(const duration& rhs) {__rep_ %= rhs.count(); return *this;}
588 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration zero() {return duration(duration_values<rep>::zero());}
589 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration min() {return duration(duration_values<rep>::min());}
590 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR duration max() {return duration(duration_values<rep>::max());}
593 typedef duration<long long, nano> nanoseconds;
594 typedef duration<long long, micro> microseconds;
595 typedef duration<long long, milli> milliseconds;
596 typedef duration<long long > seconds;
597 typedef duration< long, ratio< 60> > minutes;
598 typedef duration< long, ratio<3600> > hours;
602 template <class _LhsDuration, class _RhsDuration>
605 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
606 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
608 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
609 return _Ct(__lhs).count() == _Ct(__rhs).count();
613 template <class _LhsDuration>
614 struct __duration_eq<_LhsDuration, _LhsDuration>
616 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
617 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
618 {return __lhs.count() == __rhs.count();}
621 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
622 inline _LIBCPP_INLINE_VISIBILITY
625 operator==(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
627 return __duration_eq<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
632 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
633 inline _LIBCPP_INLINE_VISIBILITY
636 operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
638 return !(__lhs == __rhs);
643 template <class _LhsDuration, class _RhsDuration>
646 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
647 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) const
649 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct;
650 return _Ct(__lhs).count() < _Ct(__rhs).count();
654 template <class _LhsDuration>
655 struct __duration_lt<_LhsDuration, _LhsDuration>
657 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
658 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) const
659 {return __lhs.count() < __rhs.count();}
662 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
663 inline _LIBCPP_INLINE_VISIBILITY
666 operator< (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
668 return __duration_lt<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >()(__lhs, __rhs);
673 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
674 inline _LIBCPP_INLINE_VISIBILITY
677 operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
679 return __rhs < __lhs;
684 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
685 inline _LIBCPP_INLINE_VISIBILITY
688 operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
690 return !(__rhs < __lhs);
695 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
696 inline _LIBCPP_INLINE_VISIBILITY
699 operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
701 return !(__lhs < __rhs);
706 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
707 inline _LIBCPP_INLINE_VISIBILITY
709 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
710 operator+(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
712 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
713 return _Cd(_Cd(__lhs).count() + _Cd(__rhs).count());
718 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
719 inline _LIBCPP_INLINE_VISIBILITY
721 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
722 operator-(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
724 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
725 return _Cd(_Cd(__lhs).count() - _Cd(__rhs).count());
730 template <class _Rep1, class _Period, class _Rep2>
731 inline _LIBCPP_INLINE_VISIBILITY
735 is_convertible<_Rep2, typename common_type<_Rep1, _Rep2>::type>::value,
736 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
738 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
740 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
741 typedef duration<_Cr, _Period> _Cd;
742 return _Cd(_Cd(__d).count() * static_cast<_Cr>(__s));
745 template <class _Rep1, class _Period, class _Rep2>
746 inline _LIBCPP_INLINE_VISIBILITY
750 is_convertible<_Rep1, typename common_type<_Rep1, _Rep2>::type>::value,
751 duration<typename common_type<_Rep1, _Rep2>::type, _Period>
753 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d)
760 template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value>
761 struct __duration_divide_result
765 template <class _Duration, class _Rep2,
766 bool = is_convertible<_Rep2,
767 typename common_type<typename _Duration::rep, _Rep2>::type>::value>
768 struct __duration_divide_imp
772 template <class _Rep1, class _Period, class _Rep2>
773 struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true>
775 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type;
778 template <class _Rep1, class _Period, class _Rep2>
779 struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false>
780 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2>
784 template <class _Rep1, class _Period, class _Rep2>
785 inline _LIBCPP_INLINE_VISIBILITY
787 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
788 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
790 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
791 typedef duration<_Cr, _Period> _Cd;
792 return _Cd(_Cd(__d).count() / static_cast<_Cr>(__s));
795 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
796 inline _LIBCPP_INLINE_VISIBILITY
798 typename common_type<_Rep1, _Rep2>::type
799 operator/(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
801 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Ct;
802 return _Ct(__lhs).count() / _Ct(__rhs).count();
807 template <class _Rep1, class _Period, class _Rep2>
808 inline _LIBCPP_INLINE_VISIBILITY
810 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type
811 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s)
813 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
814 typedef duration<_Cr, _Period> _Cd;
815 return _Cd(_Cd(__d).count() % static_cast<_Cr>(__s));
818 template <class _Rep1, class _Period1, class _Rep2, class _Period2>
819 inline _LIBCPP_INLINE_VISIBILITY
821 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type
822 operator%(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
824 typedef typename common_type<_Rep1, _Rep2>::type _Cr;
825 typedef typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type _Cd;
826 return _Cd(static_cast<_Cr>(_Cd(__lhs).count()) % static_cast<_Cr>(_Cd(__rhs).count()));
829 //////////////////////////////////////////////////////////
830 ///////////////////// time_point /////////////////////////
831 //////////////////////////////////////////////////////////
833 template <class _Clock, class _Duration = typename _Clock::duration>
834 class _LIBCPP_TEMPLATE_VIS time_point
836 static_assert(__is_duration<_Duration>::value,
837 "Second template parameter of time_point must be a std::chrono::duration");
839 typedef _Clock clock;
840 typedef _Duration duration;
841 typedef typename duration::rep rep;
842 typedef typename duration::period period;
847 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 time_point() : __d_(duration::zero()) {}
848 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 explicit time_point(const duration& __d) : __d_(__d) {}
851 template <class _Duration2>
852 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
853 time_point(const time_point<clock, _Duration2>& t,
856 is_convertible<_Duration2, duration>::value
858 : __d_(t.time_since_epoch()) {}
862 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 duration time_since_epoch() const {return __d_;}
866 _LIBCPP_INLINE_VISIBILITY time_point& operator+=(const duration& __d) {__d_ += __d; return *this;}
867 _LIBCPP_INLINE_VISIBILITY time_point& operator-=(const duration& __d) {__d_ -= __d; return *this;}
871 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point min() {return time_point(duration::min());}
872 _LIBCPP_INLINE_VISIBILITY static _LIBCPP_CONSTEXPR time_point max() {return time_point(duration::max());}
877 template <class _Clock, class _Duration1, class _Duration2>
878 struct _LIBCPP_TEMPLATE_VIS common_type<chrono::time_point<_Clock, _Duration1>,
879 chrono::time_point<_Clock, _Duration2> >
881 typedef chrono::time_point<_Clock, typename common_type<_Duration1, _Duration2>::type> type;
886 template <class _ToDuration, class _Clock, class _Duration>
887 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
888 time_point<_Clock, _ToDuration>
889 time_point_cast(const time_point<_Clock, _Duration>& __t)
891 return time_point<_Clock, _ToDuration>(_VSTD::chrono::duration_cast<_ToDuration>(__t.time_since_epoch()));
894 #if _LIBCPP_STD_VER > 14
895 template <class _ToDuration, class _Clock, class _Duration>
896 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
899 __is_duration<_ToDuration>::value,
900 time_point<_Clock, _ToDuration>
902 floor(const time_point<_Clock, _Duration>& __t)
904 return time_point<_Clock, _ToDuration>{floor<_ToDuration>(__t.time_since_epoch())};
907 template <class _ToDuration, class _Clock, class _Duration>
908 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
911 __is_duration<_ToDuration>::value,
912 time_point<_Clock, _ToDuration>
914 ceil(const time_point<_Clock, _Duration>& __t)
916 return time_point<_Clock, _ToDuration>{ceil<_ToDuration>(__t.time_since_epoch())};
919 template <class _ToDuration, class _Clock, class _Duration>
920 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
923 __is_duration<_ToDuration>::value,
924 time_point<_Clock, _ToDuration>
926 round(const time_point<_Clock, _Duration>& __t)
928 return time_point<_Clock, _ToDuration>{round<_ToDuration>(__t.time_since_epoch())};
931 template <class _Rep, class _Period>
932 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR
935 numeric_limits<_Rep>::is_signed,
936 duration<_Rep, _Period>
938 abs(duration<_Rep, _Period> __d)
940 return __d >= __d.zero() ? __d : -__d;
946 template <class _Clock, class _Duration1, class _Duration2>
947 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
949 operator==(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
951 return __lhs.time_since_epoch() == __rhs.time_since_epoch();
956 template <class _Clock, class _Duration1, class _Duration2>
957 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
959 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
961 return !(__lhs == __rhs);
966 template <class _Clock, class _Duration1, class _Duration2>
967 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
969 operator<(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
971 return __lhs.time_since_epoch() < __rhs.time_since_epoch();
976 template <class _Clock, class _Duration1, class _Duration2>
977 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
979 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
981 return __rhs < __lhs;
986 template <class _Clock, class _Duration1, class _Duration2>
987 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
989 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
991 return !(__rhs < __lhs);
996 template <class _Clock, class _Duration1, class _Duration2>
997 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
999 operator>=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1001 return !(__lhs < __rhs);
1004 // time_point operator+(time_point x, duration y);
1006 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
1007 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1008 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1009 operator+(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1011 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Tr;
1012 return _Tr (__lhs.time_since_epoch() + __rhs);
1015 // time_point operator+(duration x, time_point y);
1017 template <class _Rep1, class _Period1, class _Clock, class _Duration2>
1018 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1019 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type>
1020 operator+(const duration<_Rep1, _Period1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1022 return __rhs + __lhs;
1025 // time_point operator-(time_point x, duration y);
1027 template <class _Clock, class _Duration1, class _Rep2, class _Period2>
1028 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1029 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type>
1030 operator-(const time_point<_Clock, _Duration1>& __lhs, const duration<_Rep2, _Period2>& __rhs)
1032 typedef time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> _Ret;
1033 return _Ret(__lhs.time_since_epoch() -__rhs);
1036 // duration operator-(time_point x, time_point y);
1038 template <class _Clock, class _Duration1, class _Duration2>
1039 inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
1040 typename common_type<_Duration1, _Duration2>::type
1041 operator-(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs)
1043 return __lhs.time_since_epoch() - __rhs.time_since_epoch();
1046 //////////////////////////////////////////////////////////
1047 /////////////////////// clocks ///////////////////////////
1048 //////////////////////////////////////////////////////////
1050 class _LIBCPP_TYPE_VIS system_clock
1053 typedef microseconds duration;
1054 typedef duration::rep rep;
1055 typedef duration::period period;
1056 typedef chrono::time_point<system_clock> time_point;
1057 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = false;
1059 static time_point now() _NOEXCEPT;
1060 static time_t to_time_t (const time_point& __t) _NOEXCEPT;
1061 static time_point from_time_t(time_t __t) _NOEXCEPT;
1064 #ifndef _LIBCPP_HAS_NO_MONOTONIC_CLOCK
1065 class _LIBCPP_TYPE_VIS steady_clock
1068 typedef nanoseconds duration;
1069 typedef duration::rep rep;
1070 typedef duration::period period;
1071 typedef chrono::time_point<steady_clock, duration> time_point;
1072 static _LIBCPP_CONSTEXPR_AFTER_CXX11 const bool is_steady = true;
1074 static time_point now() _NOEXCEPT;
1077 typedef steady_clock high_resolution_clock;
1079 typedef system_clock high_resolution_clock;
1084 #if _LIBCPP_STD_VER > 11
1085 // Suffixes for duration literals [time.duration.literals]
1086 inline namespace literals
1088 inline namespace chrono_literals
1091 constexpr chrono::hours operator""h(unsigned long long __h)
1093 return chrono::hours(static_cast<chrono::hours::rep>(__h));
1096 constexpr chrono::duration<long double, ratio<3600,1>> operator""h(long double __h)
1098 return chrono::duration<long double, ratio<3600,1>>(__h);
1102 constexpr chrono::minutes operator""min(unsigned long long __m)
1104 return chrono::minutes(static_cast<chrono::minutes::rep>(__m));
1107 constexpr chrono::duration<long double, ratio<60,1>> operator""min(long double __m)
1109 return chrono::duration<long double, ratio<60,1>> (__m);
1113 constexpr chrono::seconds operator""s(unsigned long long __s)
1115 return chrono::seconds(static_cast<chrono::seconds::rep>(__s));
1118 constexpr chrono::duration<long double> operator""s(long double __s)
1120 return chrono::duration<long double> (__s);
1124 constexpr chrono::milliseconds operator""ms(unsigned long long __ms)
1126 return chrono::milliseconds(static_cast<chrono::milliseconds::rep>(__ms));
1129 constexpr chrono::duration<long double, milli> operator""ms(long double __ms)
1131 return chrono::duration<long double, milli>(__ms);
1135 constexpr chrono::microseconds operator""us(unsigned long long __us)
1137 return chrono::microseconds(static_cast<chrono::microseconds::rep>(__us));
1140 constexpr chrono::duration<long double, micro> operator""us(long double __us)
1142 return chrono::duration<long double, micro> (__us);
1146 constexpr chrono::nanoseconds operator""ns(unsigned long long __ns)
1148 return chrono::nanoseconds(static_cast<chrono::nanoseconds::rep>(__ns));
1151 constexpr chrono::duration<long double, nano> operator""ns(long double __ns)
1153 return chrono::duration<long double, nano> (__ns);
1158 namespace chrono { // hoist the literals into namespace std::chrono
1159 using namespace literals::chrono_literals;
1164 _LIBCPP_END_NAMESPACE_STD
1168 #endif // _LIBCPP_CHRONO