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 17 namespace std 18 { 19 namespace chrono 20 { 21 22 template <class ToDuration, class Rep, class Period> 23 constexpr 24 ToDuration 25 duration_cast(const duration<Rep, Period>& fd); 26 27 template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {}; 28 29 template <class Rep> 30 struct duration_values 31 { 32 public: 33 static constexpr Rep zero(); 34 static constexpr Rep max(); 35 static constexpr Rep min(); 36 }; 37 38 // duration 39 40 template <class Rep, class Period = ratio<1>> 41 class 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"); 46 public: 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 95 typedef duration<long long, nano> nanoseconds; 96 typedef duration<long long, micro> microseconds; 97 typedef duration<long long, milli> milliseconds; 98 typedef duration<long long > seconds; 99 typedef duration< long, ratio< 60> > minutes; 100 typedef duration< long, ratio<3600> > hours; 101 102 template <class Clock, class Duration = typename Clock::duration> 103 class time_point 104 { 105 public: 106 typedef Clock clock; 107 typedef Duration duration; 108 typedef typename duration::rep rep; 109 typedef typename duration::period period; 110 private: 111 duration d_; // exposition only 112 113 public: 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 139 template <class Rep1, class Period1, class Rep2, class Period2> 140 struct common_type<chrono::duration<Rep1, Period1>, chrono::duration<Rep2, Period2>>; 141 142 template <class Clock, class Duration1, class Duration2> 143 struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>>; 144 145 namespace chrono { 146 147 // duration arithmetic 148 template <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); 152 template <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); 156 template <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); 160 template <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); 164 template <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); 168 template <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 174 template <class Rep1, class Period1, class Rep2, class Period2> 175 constexpr 176 bool operator==(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); 177 template <class Rep1, class Period1, class Rep2, class Period2> 178 constexpr 179 bool operator!=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); 180 template <class Rep1, class Period1, class Rep2, class Period2> 181 constexpr 182 bool operator< (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); 183 template <class Rep1, class Period1, class Rep2, class Period2> 184 constexpr 185 bool operator<=(const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); 186 template <class Rep1, class Period1, class Rep2, class Period2> 187 constexpr 188 bool operator> (const duration<Rep1, Period1>& lhs, const duration<Rep2, Period2>& rhs); 189 template <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 194 template <class ToDuration, class Rep, class Period> 195 ToDuration duration_cast(const duration<Rep, Period>& d); 196 197 // time_point arithmetic 198 template <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); 201 template <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); 204 template <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); 207 template <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 212 template <class Clock, class Duration1, class Duration2> 213 bool operator==(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); 214 template <class Clock, class Duration1, class Duration2> 215 bool operator!=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); 216 template <class Clock, class Duration1, class Duration2> 217 bool operator< (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); 218 template <class Clock, class Duration1, class Duration2> 219 bool operator<=(const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); 220 template <class Clock, class Duration1, class Duration2> 221 bool operator> (const time_point<Clock, Duration1>& lhs, const time_point<Clock, Duration2>& rhs); 222 template <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 227 template <class ToDuration, class Clock, class Duration> 228 time_point<Clock, ToDuration> time_point_cast(const time_point<Clock, Duration>& t); 229 230 // Clocks 231 232 class system_clock 233 { 234 public: 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 246 class steady_clock 247 { 248 public: 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 258 typedef 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 279 namespace chrono 280 { 281 282 template <class _Rep, class _Period = ratio<1> > class _LIBCPP_VISIBLE duration; 283 284 template <class _Tp> 285 struct __is_duration : false_type {}; 286 287 template <class _Rep, class _Period> 288 struct __is_duration<duration<_Rep, _Period> > : true_type {}; 289 290 template <class _Rep, class _Period> 291 struct __is_duration<const duration<_Rep, _Period> > : true_type {}; 292 293 template <class _Rep, class _Period> 294 struct __is_duration<volatile duration<_Rep, _Period> > : true_type {}; 295 296 template <class _Rep, class _Period> 297 struct __is_duration<const volatile duration<_Rep, _Period> > : true_type {}; 298 299 } // chrono 300 301 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 302 struct _LIBCPP_VISIBLE 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 309 namespace chrono { 310 311 // duration_cast 312 313 template <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> 317 struct __duration_cast; 318 319 template <class _FromDuration, class _ToDuration, class _Period> 320 struct __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 329 template <class _FromDuration, class _ToDuration, class _Period> 330 struct __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 341 template <class _FromDuration, class _ToDuration, class _Period> 342 struct __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 353 template <class _FromDuration, class _ToDuration, class _Period> 354 struct __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 366 template <class _ToDuration, class _Rep, class _Period> 367 inline _LIBCPP_INLINE_VISIBILITY 368 _LIBCPP_CONSTEXPR 369 typename enable_if 370 < 371 __is_duration<_ToDuration>::value, 372 _ToDuration 373 >::type 374 duration_cast(const duration<_Rep, _Period>& __fd) 375 { 376 return __duration_cast<duration<_Rep, _Period>, _ToDuration>()(__fd); 377 } 378 379 template <class _Rep> 380 struct _LIBCPP_VISIBLE treat_as_floating_point : is_floating_point<_Rep> {}; 381 382 template <class _Rep> 383 struct _LIBCPP_VISIBLE duration_values 384 { 385 public: 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 393 template <class _Rep, class _Period> 394 class _LIBCPP_VISIBLE 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"); 399 public: 400 typedef _Rep rep; 401 typedef _Period period; 402 private: 403 rep __rep_; 404 public: 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 458 typedef duration<long long, nano> nanoseconds; 459 typedef duration<long long, micro> microseconds; 460 typedef duration<long long, milli> milliseconds; 461 typedef duration<long long > seconds; 462 typedef duration< long, ratio< 60> > minutes; 463 typedef duration< long, ratio<3600> > hours; 464 465 // Duration == 466 467 template <class _LhsDuration, class _RhsDuration> 468 struct __duration_eq 469 { 470 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 471 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) 472 { 473 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; 474 return _Ct(__lhs).count() == _Ct(__rhs).count(); 475 } 476 }; 477 478 template <class _LhsDuration> 479 struct __duration_eq<_LhsDuration, _LhsDuration> 480 { 481 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 482 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) 483 {return __lhs.count() == __rhs.count();} 484 }; 485 486 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 487 inline _LIBCPP_INLINE_VISIBILITY 488 _LIBCPP_CONSTEXPR 489 bool 490 operator==(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 497 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 498 inline _LIBCPP_INLINE_VISIBILITY 499 _LIBCPP_CONSTEXPR 500 bool 501 operator!=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) 502 { 503 return !(__lhs == __rhs); 504 } 505 506 // Duration < 507 508 template <class _LhsDuration, class _RhsDuration> 509 struct __duration_lt 510 { 511 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 512 bool operator()(const _LhsDuration& __lhs, const _RhsDuration& __rhs) 513 { 514 typedef typename common_type<_LhsDuration, _RhsDuration>::type _Ct; 515 return _Ct(__lhs).count() < _Ct(__rhs).count(); 516 } 517 }; 518 519 template <class _LhsDuration> 520 struct __duration_lt<_LhsDuration, _LhsDuration> 521 { 522 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR 523 bool operator()(const _LhsDuration& __lhs, const _LhsDuration& __rhs) 524 {return __lhs.count() < __rhs.count();} 525 }; 526 527 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 528 inline _LIBCPP_INLINE_VISIBILITY 529 _LIBCPP_CONSTEXPR 530 bool 531 operator< (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 538 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 539 inline _LIBCPP_INLINE_VISIBILITY 540 _LIBCPP_CONSTEXPR 541 bool 542 operator> (const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) 543 { 544 return __rhs < __lhs; 545 } 546 547 // Duration <= 548 549 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 550 inline _LIBCPP_INLINE_VISIBILITY 551 _LIBCPP_CONSTEXPR 552 bool 553 operator<=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) 554 { 555 return !(__rhs < __lhs); 556 } 557 558 // Duration >= 559 560 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 561 inline _LIBCPP_INLINE_VISIBILITY 562 _LIBCPP_CONSTEXPR 563 bool 564 operator>=(const duration<_Rep1, _Period1>& __lhs, const duration<_Rep2, _Period2>& __rhs) 565 { 566 return !(__lhs < __rhs); 567 } 568 569 // Duration + 570 571 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 572 inline _LIBCPP_INLINE_VISIBILITY 573 _LIBCPP_CONSTEXPR 574 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type 575 operator+(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 583 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 584 inline _LIBCPP_INLINE_VISIBILITY 585 _LIBCPP_CONSTEXPR 586 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type 587 operator-(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 595 template <class _Rep1, class _Period, class _Rep2> 596 inline _LIBCPP_INLINE_VISIBILITY 597 _LIBCPP_CONSTEXPR 598 typename 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 603 operator*(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 610 template <class _Rep1, class _Period, class _Rep2> 611 inline _LIBCPP_INLINE_VISIBILITY 612 _LIBCPP_CONSTEXPR 613 typename 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 618 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) 619 { 620 return __d * __s; 621 } 622 623 // Duration / 624 625 template <class _Duration, class _Rep, bool = __is_duration<_Rep>::value> 626 struct __duration_divide_result 627 { 628 }; 629 630 template <class _Duration, class _Rep2, 631 bool = is_convertible<_Rep2, 632 typename common_type<typename _Duration::rep, _Rep2>::type>::value> 633 struct __duration_divide_imp 634 { 635 }; 636 637 template <class _Rep1, class _Period, class _Rep2> 638 struct __duration_divide_imp<duration<_Rep1, _Period>, _Rep2, true> 639 { 640 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> type; 641 }; 642 643 template <class _Rep1, class _Period, class _Rep2> 644 struct __duration_divide_result<duration<_Rep1, _Period>, _Rep2, false> 645 : __duration_divide_imp<duration<_Rep1, _Period>, _Rep2> 646 { 647 }; 648 649 template <class _Rep1, class _Period, class _Rep2> 650 inline _LIBCPP_INLINE_VISIBILITY 651 _LIBCPP_CONSTEXPR 652 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type 653 operator/(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 660 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 661 inline _LIBCPP_INLINE_VISIBILITY 662 _LIBCPP_CONSTEXPR 663 typename common_type<_Rep1, _Rep2>::type 664 operator/(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 672 template <class _Rep1, class _Period, class _Rep2> 673 inline _LIBCPP_INLINE_VISIBILITY 674 _LIBCPP_CONSTEXPR 675 typename __duration_divide_result<duration<_Rep1, _Period>, _Rep2>::type 676 operator%(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 683 template <class _Rep1, class _Period1, class _Rep2, class _Period2> 684 inline _LIBCPP_INLINE_VISIBILITY 685 _LIBCPP_CONSTEXPR 686 typename common_type<duration<_Rep1, _Period1>, duration<_Rep2, _Period2> >::type 687 operator%(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 698 template <class _Clock, class _Duration = typename _Clock::duration> 699 class _LIBCPP_VISIBLE time_point 700 { 701 static_assert(__is_duration<_Duration>::value, 702 "Second template parameter of time_point must be a std::chrono::duration"); 703 public: 704 typedef _Clock clock; 705 typedef _Duration duration; 706 typedef typename duration::rep rep; 707 typedef typename duration::period period; 708 private: 709 duration __d_; 710 711 public: 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 742 template <class _Clock, class _Duration1, class _Duration2> 743 struct _LIBCPP_VISIBLE 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 749 namespace chrono { 750 751 template <class _ToDuration, class _Clock, class _Duration> 752 inline _LIBCPP_INLINE_VISIBILITY 753 time_point<_Clock, _ToDuration> 754 time_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 761 template <class _Clock, class _Duration1, class _Duration2> 762 inline _LIBCPP_INLINE_VISIBILITY 763 bool 764 operator==(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 771 template <class _Clock, class _Duration1, class _Duration2> 772 inline _LIBCPP_INLINE_VISIBILITY 773 bool 774 operator!=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) 775 { 776 return !(__lhs == __rhs); 777 } 778 779 // time_point < 780 781 template <class _Clock, class _Duration1, class _Duration2> 782 inline _LIBCPP_INLINE_VISIBILITY 783 bool 784 operator<(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 791 template <class _Clock, class _Duration1, class _Duration2> 792 inline _LIBCPP_INLINE_VISIBILITY 793 bool 794 operator>(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) 795 { 796 return __rhs < __lhs; 797 } 798 799 // time_point <= 800 801 template <class _Clock, class _Duration1, class _Duration2> 802 inline _LIBCPP_INLINE_VISIBILITY 803 bool 804 operator<=(const time_point<_Clock, _Duration1>& __lhs, const time_point<_Clock, _Duration2>& __rhs) 805 { 806 return !(__rhs < __lhs); 807 } 808 809 // time_point >= 810 811 template <class _Clock, class _Duration1, class _Duration2> 812 inline _LIBCPP_INLINE_VISIBILITY 813 bool 814 operator>=(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 821 template <class _Clock, class _Duration1, class _Rep2, class _Period2> 822 inline _LIBCPP_INLINE_VISIBILITY 823 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> 824 operator+(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 834 template <class _Rep1, class _Period1, class _Clock, class _Duration2> 835 inline _LIBCPP_INLINE_VISIBILITY 836 time_point<_Clock, typename common_type<duration<_Rep1, _Period1>, _Duration2>::type> 837 operator+(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 844 template <class _Clock, class _Duration1, class _Rep2, class _Period2> 845 inline _LIBCPP_INLINE_VISIBILITY 846 time_point<_Clock, typename common_type<_Duration1, duration<_Rep2, _Period2> >::type> 847 operator-(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 854 template <class _Clock, class _Duration1, class _Duration2> 855 inline _LIBCPP_INLINE_VISIBILITY 856 typename common_type<_Duration1, _Duration2>::type 857 operator-(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 866 class _LIBCPP_VISIBLE system_clock 867 { 868 public: 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 880 class _LIBCPP_VISIBLE steady_clock 881 { 882 public: 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 892 typedef steady_clock high_resolution_clock; 893 894 } // chrono 895 896 _LIBCPP_END_NAMESPACE_STD 897 898 #endif // _LIBCPP_CHRONO 899