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