1// <chrono> -*- C++ -*- 2 3// Copyright (C) 2008-2015 Free Software Foundation, Inc. 4// 5// This file is part of the GNU ISO C++ Library. This library is free 6// software; you can redistribute it and/or modify it under the 7// terms of the GNU General Public License as published by the 8// Free Software Foundation; either version 3, or (at your option) 9// any later version. 10 11// This library is distributed in the hope that it will be useful, 12// but WITHOUT ANY WARRANTY; without even the implied warranty of 13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14// GNU General Public License for more details. 15 16// Under Section 7 of GPL version 3, you are granted additional 17// permissions described in the GCC Runtime Library Exception, version 18// 3.1, as published by the Free Software Foundation. 19 20// You should have received a copy of the GNU General Public License and 21// a copy of the GCC Runtime Library Exception along with this program; 22// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23// <http://www.gnu.org/licenses/>. 24 25/** @file include/chrono 26 * This is a Standard C++ Library header. 27 */ 28 29#ifndef _GLIBCXX_CHRONO 30#define _GLIBCXX_CHRONO 1 31 32#pragma GCC system_header 33 34#if __cplusplus < 201103L 35# include <bits/c++0x_warning.h> 36#else 37 38#include <ratio> 39#include <type_traits> 40#include <limits> 41#include <ctime> 42#include <bits/parse_numbers.h> // for literals support. 43 44#ifdef _GLIBCXX_USE_C99_STDINT_TR1 45 46namespace std _GLIBCXX_VISIBILITY(default) 47{ 48 /** 49 * @defgroup chrono Time 50 * @ingroup utilities 51 * 52 * Classes and functions for time. 53 * @{ 54 */ 55 56 /** @namespace std::chrono 57 * @brief ISO C++ 2011 entities sub-namespace for time and date. 58 */ 59 namespace chrono 60 { 61 _GLIBCXX_BEGIN_NAMESPACE_VERSION 62 63 template<typename _Rep, typename _Period = ratio<1>> 64 struct duration; 65 66 template<typename _Clock, typename _Dur = typename _Clock::duration> 67 struct time_point; 68 69 _GLIBCXX_END_NAMESPACE_VERSION 70 } 71 72_GLIBCXX_BEGIN_NAMESPACE_VERSION 73 74 // 20.11.4.3 specialization of common_type (for duration, sfinae-friendly) 75 76 template<typename _CT, typename _Period1, typename _Period2> 77 struct __duration_common_type_wrapper 78 { 79 private: 80 typedef __static_gcd<_Period1::num, _Period2::num> __gcd_num; 81 typedef __static_gcd<_Period1::den, _Period2::den> __gcd_den; 82 typedef typename _CT::type __cr; 83 typedef ratio<__gcd_num::value, 84 (_Period1::den / __gcd_den::value) * _Period2::den> __r; 85 public: 86 typedef __success_type<chrono::duration<__cr, __r>> type; 87 }; 88 89 template<typename _Period1, typename _Period2> 90 struct __duration_common_type_wrapper<__failure_type, _Period1, _Period2> 91 { typedef __failure_type type; }; 92 93 template<typename _Rep1, typename _Period1, typename _Rep2, typename _Period2> 94 struct common_type<chrono::duration<_Rep1, _Period1>, 95 chrono::duration<_Rep2, _Period2>> 96 : public __duration_common_type_wrapper<typename __member_type_wrapper< 97 common_type<_Rep1, _Rep2>>::type, _Period1, _Period2>::type 98 { }; 99 100 // 20.11.4.3 specialization of common_type (for time_point, sfinae-friendly) 101 102 template<typename _CT, typename _Clock> 103 struct __timepoint_common_type_wrapper 104 { 105 typedef __success_type<chrono::time_point<_Clock, typename _CT::type>> 106 type; 107 }; 108 109 template<typename _Clock> 110 struct __timepoint_common_type_wrapper<__failure_type, _Clock> 111 { typedef __failure_type type; }; 112 113 template<typename _Clock, typename _Duration1, typename _Duration2> 114 struct common_type<chrono::time_point<_Clock, _Duration1>, 115 chrono::time_point<_Clock, _Duration2>> 116 : public __timepoint_common_type_wrapper<typename __member_type_wrapper< 117 common_type<_Duration1, _Duration2>>::type, _Clock>::type 118 { }; 119 120_GLIBCXX_END_NAMESPACE_VERSION 121 122 namespace chrono 123 { 124 _GLIBCXX_BEGIN_NAMESPACE_VERSION 125 126 // Primary template for duration_cast impl. 127 template<typename _ToDur, typename _CF, typename _CR, 128 bool _NumIsOne = false, bool _DenIsOne = false> 129 struct __duration_cast_impl 130 { 131 template<typename _Rep, typename _Period> 132 static constexpr _ToDur 133 __cast(const duration<_Rep, _Period>& __d) 134 { 135 typedef typename _ToDur::rep __to_rep; 136 return _ToDur(static_cast<__to_rep>(static_cast<_CR>(__d.count()) 137 * static_cast<_CR>(_CF::num) 138 / static_cast<_CR>(_CF::den))); 139 } 140 }; 141 142 template<typename _ToDur, typename _CF, typename _CR> 143 struct __duration_cast_impl<_ToDur, _CF, _CR, true, true> 144 { 145 template<typename _Rep, typename _Period> 146 static constexpr _ToDur 147 __cast(const duration<_Rep, _Period>& __d) 148 { 149 typedef typename _ToDur::rep __to_rep; 150 return _ToDur(static_cast<__to_rep>(__d.count())); 151 } 152 }; 153 154 template<typename _ToDur, typename _CF, typename _CR> 155 struct __duration_cast_impl<_ToDur, _CF, _CR, true, false> 156 { 157 template<typename _Rep, typename _Period> 158 static constexpr _ToDur 159 __cast(const duration<_Rep, _Period>& __d) 160 { 161 typedef typename _ToDur::rep __to_rep; 162 return _ToDur(static_cast<__to_rep>( 163 static_cast<_CR>(__d.count()) / static_cast<_CR>(_CF::den))); 164 } 165 }; 166 167 template<typename _ToDur, typename _CF, typename _CR> 168 struct __duration_cast_impl<_ToDur, _CF, _CR, false, true> 169 { 170 template<typename _Rep, typename _Period> 171 static constexpr _ToDur 172 __cast(const duration<_Rep, _Period>& __d) 173 { 174 typedef typename _ToDur::rep __to_rep; 175 return _ToDur(static_cast<__to_rep>( 176 static_cast<_CR>(__d.count()) * static_cast<_CR>(_CF::num))); 177 } 178 }; 179 180 template<typename _Tp> 181 struct __is_duration 182 : std::false_type 183 { }; 184 185 template<typename _Rep, typename _Period> 186 struct __is_duration<duration<_Rep, _Period>> 187 : std::true_type 188 { }; 189 190 /// duration_cast 191 template<typename _ToDur, typename _Rep, typename _Period> 192 constexpr typename enable_if<__is_duration<_ToDur>::value, 193 _ToDur>::type 194 duration_cast(const duration<_Rep, _Period>& __d) 195 { 196 typedef typename _ToDur::period __to_period; 197 typedef typename _ToDur::rep __to_rep; 198 typedef ratio_divide<_Period, __to_period> __cf; 199 typedef typename common_type<__to_rep, _Rep, intmax_t>::type 200 __cr; 201 typedef __duration_cast_impl<_ToDur, __cf, __cr, 202 __cf::num == 1, __cf::den == 1> __dc; 203 return __dc::__cast(__d); 204 } 205 206 /// treat_as_floating_point 207 template<typename _Rep> 208 struct treat_as_floating_point 209 : is_floating_point<_Rep> 210 { }; 211 212 /// duration_values 213 template<typename _Rep> 214 struct duration_values 215 { 216 static constexpr _Rep 217 zero() 218 { return _Rep(0); } 219 220 static constexpr _Rep 221 max() 222 { return numeric_limits<_Rep>::max(); } 223 224 static constexpr _Rep 225 min() 226 { return numeric_limits<_Rep>::lowest(); } 227 }; 228 229 template<typename _Tp> 230 struct __is_ratio 231 : std::false_type 232 { }; 233 234 template<intmax_t _Num, intmax_t _Den> 235 struct __is_ratio<ratio<_Num, _Den>> 236 : std::true_type 237 { }; 238 239 /// duration 240 template<typename _Rep, typename _Period> 241 struct duration 242 { 243 typedef _Rep rep; 244 typedef _Period period; 245 246 static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration"); 247 static_assert(__is_ratio<_Period>::value, 248 "period must be a specialization of ratio"); 249 static_assert(_Period::num > 0, "period must be positive"); 250 251 // 20.11.5.1 construction / copy / destroy 252 constexpr duration() = default; 253 254 // NB: Make constexpr implicit. This cannot be explicitly 255 // constexpr, as any UDT that is not a literal type with a 256 // constexpr copy constructor will be ill-formed. 257 duration(const duration&) = default; 258 259 template<typename _Rep2, typename = typename 260 enable_if<is_convertible<_Rep2, rep>::value 261 && (treat_as_floating_point<rep>::value 262 || !treat_as_floating_point<_Rep2>::value)>::type> 263 constexpr explicit duration(const _Rep2& __rep) 264 : __r(static_cast<rep>(__rep)) { } 265 266 template<typename _Rep2, typename _Period2, typename = typename 267 enable_if<treat_as_floating_point<rep>::value 268 || (ratio_divide<_Period2, period>::den == 1 269 && !treat_as_floating_point<_Rep2>::value)>::type> 270 constexpr duration(const duration<_Rep2, _Period2>& __d) 271 : __r(duration_cast<duration>(__d).count()) { } 272 273 ~duration() = default; 274 duration& operator=(const duration&) = default; 275 276 // 20.11.5.2 observer 277 constexpr rep 278 count() const 279 { return __r; } 280 281 // 20.11.5.3 arithmetic 282 constexpr duration 283 operator+() const 284 { return *this; } 285 286 constexpr duration 287 operator-() const 288 { return duration(-__r); } 289 290 duration& 291 operator++() 292 { 293 ++__r; 294 return *this; 295 } 296 297 duration 298 operator++(int) 299 { return duration(__r++); } 300 301 duration& 302 operator--() 303 { 304 --__r; 305 return *this; 306 } 307 308 duration 309 operator--(int) 310 { return duration(__r--); } 311 312 duration& 313 operator+=(const duration& __d) 314 { 315 __r += __d.count(); 316 return *this; 317 } 318 319 duration& 320 operator-=(const duration& __d) 321 { 322 __r -= __d.count(); 323 return *this; 324 } 325 326 duration& 327 operator*=(const rep& __rhs) 328 { 329 __r *= __rhs; 330 return *this; 331 } 332 333 duration& 334 operator/=(const rep& __rhs) 335 { 336 __r /= __rhs; 337 return *this; 338 } 339 340 // DR 934. 341 template<typename _Rep2 = rep> 342 typename enable_if<!treat_as_floating_point<_Rep2>::value, 343 duration&>::type 344 operator%=(const rep& __rhs) 345 { 346 __r %= __rhs; 347 return *this; 348 } 349 350 template<typename _Rep2 = rep> 351 typename enable_if<!treat_as_floating_point<_Rep2>::value, 352 duration&>::type 353 operator%=(const duration& __d) 354 { 355 __r %= __d.count(); 356 return *this; 357 } 358 359 // 20.11.5.4 special values 360 static constexpr duration 361 zero() 362 { return duration(duration_values<rep>::zero()); } 363 364 static constexpr duration 365 min() 366 { return duration(duration_values<rep>::min()); } 367 368 static constexpr duration 369 max() 370 { return duration(duration_values<rep>::max()); } 371 372 private: 373 rep __r; 374 }; 375 376 template<typename _Rep1, typename _Period1, 377 typename _Rep2, typename _Period2> 378 constexpr typename common_type<duration<_Rep1, _Period1>, 379 duration<_Rep2, _Period2>>::type 380 operator+(const duration<_Rep1, _Period1>& __lhs, 381 const duration<_Rep2, _Period2>& __rhs) 382 { 383 typedef duration<_Rep1, _Period1> __dur1; 384 typedef duration<_Rep2, _Period2> __dur2; 385 typedef typename common_type<__dur1,__dur2>::type __cd; 386 return __cd(__cd(__lhs).count() + __cd(__rhs).count()); 387 } 388 389 template<typename _Rep1, typename _Period1, 390 typename _Rep2, typename _Period2> 391 constexpr typename common_type<duration<_Rep1, _Period1>, 392 duration<_Rep2, _Period2>>::type 393 operator-(const duration<_Rep1, _Period1>& __lhs, 394 const duration<_Rep2, _Period2>& __rhs) 395 { 396 typedef duration<_Rep1, _Period1> __dur1; 397 typedef duration<_Rep2, _Period2> __dur2; 398 typedef typename common_type<__dur1,__dur2>::type __cd; 399 return __cd(__cd(__lhs).count() - __cd(__rhs).count()); 400 } 401 402 template<typename _Rep1, typename _Rep2, bool = 403 is_convertible<_Rep2, 404 typename common_type<_Rep1, _Rep2>::type>::value> 405 struct __common_rep_type { }; 406 407 template<typename _Rep1, typename _Rep2> 408 struct __common_rep_type<_Rep1, _Rep2, true> 409 { typedef typename common_type<_Rep1, _Rep2>::type type; }; 410 411 template<typename _Rep1, typename _Period, typename _Rep2> 412 constexpr 413 duration<typename __common_rep_type<_Rep1, _Rep2>::type, _Period> 414 operator*(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 415 { 416 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 417 __cd; 418 return __cd(__cd(__d).count() * __s); 419 } 420 421 template<typename _Rep1, typename _Rep2, typename _Period> 422 constexpr 423 duration<typename __common_rep_type<_Rep2, _Rep1>::type, _Period> 424 operator*(const _Rep1& __s, const duration<_Rep2, _Period>& __d) 425 { return __d * __s; } 426 427 template<typename _Rep1, typename _Period, typename _Rep2> 428 constexpr duration<typename __common_rep_type<_Rep1, typename 429 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> 430 operator/(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 431 { 432 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 433 __cd; 434 return __cd(__cd(__d).count() / __s); 435 } 436 437 template<typename _Rep1, typename _Period1, 438 typename _Rep2, typename _Period2> 439 constexpr typename common_type<_Rep1, _Rep2>::type 440 operator/(const duration<_Rep1, _Period1>& __lhs, 441 const duration<_Rep2, _Period2>& __rhs) 442 { 443 typedef duration<_Rep1, _Period1> __dur1; 444 typedef duration<_Rep2, _Period2> __dur2; 445 typedef typename common_type<__dur1,__dur2>::type __cd; 446 return __cd(__lhs).count() / __cd(__rhs).count(); 447 } 448 449 // DR 934. 450 template<typename _Rep1, typename _Period, typename _Rep2> 451 constexpr duration<typename __common_rep_type<_Rep1, typename 452 enable_if<!__is_duration<_Rep2>::value, _Rep2>::type>::type, _Period> 453 operator%(const duration<_Rep1, _Period>& __d, const _Rep2& __s) 454 { 455 typedef duration<typename common_type<_Rep1, _Rep2>::type, _Period> 456 __cd; 457 return __cd(__cd(__d).count() % __s); 458 } 459 460 template<typename _Rep1, typename _Period1, 461 typename _Rep2, typename _Period2> 462 constexpr typename common_type<duration<_Rep1, _Period1>, 463 duration<_Rep2, _Period2>>::type 464 operator%(const duration<_Rep1, _Period1>& __lhs, 465 const duration<_Rep2, _Period2>& __rhs) 466 { 467 typedef duration<_Rep1, _Period1> __dur1; 468 typedef duration<_Rep2, _Period2> __dur2; 469 typedef typename common_type<__dur1,__dur2>::type __cd; 470 return __cd(__cd(__lhs).count() % __cd(__rhs).count()); 471 } 472 473 // comparisons 474 template<typename _Rep1, typename _Period1, 475 typename _Rep2, typename _Period2> 476 constexpr bool 477 operator==(const duration<_Rep1, _Period1>& __lhs, 478 const duration<_Rep2, _Period2>& __rhs) 479 { 480 typedef duration<_Rep1, _Period1> __dur1; 481 typedef duration<_Rep2, _Period2> __dur2; 482 typedef typename common_type<__dur1,__dur2>::type __ct; 483 return __ct(__lhs).count() == __ct(__rhs).count(); 484 } 485 486 template<typename _Rep1, typename _Period1, 487 typename _Rep2, typename _Period2> 488 constexpr bool 489 operator<(const duration<_Rep1, _Period1>& __lhs, 490 const duration<_Rep2, _Period2>& __rhs) 491 { 492 typedef duration<_Rep1, _Period1> __dur1; 493 typedef duration<_Rep2, _Period2> __dur2; 494 typedef typename common_type<__dur1,__dur2>::type __ct; 495 return __ct(__lhs).count() < __ct(__rhs).count(); 496 } 497 498 template<typename _Rep1, typename _Period1, 499 typename _Rep2, typename _Period2> 500 constexpr bool 501 operator!=(const duration<_Rep1, _Period1>& __lhs, 502 const duration<_Rep2, _Period2>& __rhs) 503 { return !(__lhs == __rhs); } 504 505 template<typename _Rep1, typename _Period1, 506 typename _Rep2, typename _Period2> 507 constexpr bool 508 operator<=(const duration<_Rep1, _Period1>& __lhs, 509 const duration<_Rep2, _Period2>& __rhs) 510 { return !(__rhs < __lhs); } 511 512 template<typename _Rep1, typename _Period1, 513 typename _Rep2, typename _Period2> 514 constexpr bool 515 operator>(const duration<_Rep1, _Period1>& __lhs, 516 const duration<_Rep2, _Period2>& __rhs) 517 { return __rhs < __lhs; } 518 519 template<typename _Rep1, typename _Period1, 520 typename _Rep2, typename _Period2> 521 constexpr bool 522 operator>=(const duration<_Rep1, _Period1>& __lhs, 523 const duration<_Rep2, _Period2>& __rhs) 524 { return !(__lhs < __rhs); } 525 526 /// nanoseconds 527 typedef duration<int64_t, nano> nanoseconds; 528 529 /// microseconds 530 typedef duration<int64_t, micro> microseconds; 531 532 /// milliseconds 533 typedef duration<int64_t, milli> milliseconds; 534 535 /// seconds 536 typedef duration<int64_t> seconds; 537 538 /// minutes 539 typedef duration<int64_t, ratio< 60>> minutes; 540 541 /// hours 542 typedef duration<int64_t, ratio<3600>> hours; 543 544 /// time_point 545 template<typename _Clock, typename _Dur> 546 struct time_point 547 { 548 typedef _Clock clock; 549 typedef _Dur duration; 550 typedef typename duration::rep rep; 551 typedef typename duration::period period; 552 553 constexpr time_point() : __d(duration::zero()) 554 { } 555 556 constexpr explicit time_point(const duration& __dur) 557 : __d(__dur) 558 { } 559 560 // conversions 561 template<typename _Dur2, 562 typename = _Require<is_convertible<_Dur2, _Dur>>> 563 constexpr time_point(const time_point<clock, _Dur2>& __t) 564 : __d(__t.time_since_epoch()) 565 { } 566 567 // observer 568 constexpr duration 569 time_since_epoch() const 570 { return __d; } 571 572 // arithmetic 573 time_point& 574 operator+=(const duration& __dur) 575 { 576 __d += __dur; 577 return *this; 578 } 579 580 time_point& 581 operator-=(const duration& __dur) 582 { 583 __d -= __dur; 584 return *this; 585 } 586 587 // special values 588 static constexpr time_point 589 min() 590 { return time_point(duration::min()); } 591 592 static constexpr time_point 593 max() 594 { return time_point(duration::max()); } 595 596 private: 597 duration __d; 598 }; 599 600 /// time_point_cast 601 template<typename _ToDur, typename _Clock, typename _Dur> 602 constexpr typename enable_if<__is_duration<_ToDur>::value, 603 time_point<_Clock, _ToDur>>::type 604 time_point_cast(const time_point<_Clock, _Dur>& __t) 605 { 606 typedef time_point<_Clock, _ToDur> __time_point; 607 return __time_point(duration_cast<_ToDur>(__t.time_since_epoch())); 608 } 609 610 template<typename _Clock, typename _Dur1, 611 typename _Rep2, typename _Period2> 612 constexpr time_point<_Clock, 613 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 614 operator+(const time_point<_Clock, _Dur1>& __lhs, 615 const duration<_Rep2, _Period2>& __rhs) 616 { 617 typedef duration<_Rep2, _Period2> __dur2; 618 typedef typename common_type<_Dur1,__dur2>::type __ct; 619 typedef time_point<_Clock, __ct> __time_point; 620 return __time_point(__lhs.time_since_epoch() + __rhs); 621 } 622 623 template<typename _Rep1, typename _Period1, 624 typename _Clock, typename _Dur2> 625 constexpr time_point<_Clock, 626 typename common_type<duration<_Rep1, _Period1>, _Dur2>::type> 627 operator+(const duration<_Rep1, _Period1>& __lhs, 628 const time_point<_Clock, _Dur2>& __rhs) 629 { 630 typedef duration<_Rep1, _Period1> __dur1; 631 typedef typename common_type<__dur1,_Dur2>::type __ct; 632 typedef time_point<_Clock, __ct> __time_point; 633 return __time_point(__rhs.time_since_epoch() + __lhs); 634 } 635 636 template<typename _Clock, typename _Dur1, 637 typename _Rep2, typename _Period2> 638 constexpr time_point<_Clock, 639 typename common_type<_Dur1, duration<_Rep2, _Period2>>::type> 640 operator-(const time_point<_Clock, _Dur1>& __lhs, 641 const duration<_Rep2, _Period2>& __rhs) 642 { 643 typedef duration<_Rep2, _Period2> __dur2; 644 typedef typename common_type<_Dur1,__dur2>::type __ct; 645 typedef time_point<_Clock, __ct> __time_point; 646 return __time_point(__lhs.time_since_epoch() -__rhs); 647 } 648 649 template<typename _Clock, typename _Dur1, typename _Dur2> 650 constexpr typename common_type<_Dur1, _Dur2>::type 651 operator-(const time_point<_Clock, _Dur1>& __lhs, 652 const time_point<_Clock, _Dur2>& __rhs) 653 { return __lhs.time_since_epoch() - __rhs.time_since_epoch(); } 654 655 template<typename _Clock, typename _Dur1, typename _Dur2> 656 constexpr bool 657 operator==(const time_point<_Clock, _Dur1>& __lhs, 658 const time_point<_Clock, _Dur2>& __rhs) 659 { return __lhs.time_since_epoch() == __rhs.time_since_epoch(); } 660 661 template<typename _Clock, typename _Dur1, typename _Dur2> 662 constexpr bool 663 operator!=(const time_point<_Clock, _Dur1>& __lhs, 664 const time_point<_Clock, _Dur2>& __rhs) 665 { return !(__lhs == __rhs); } 666 667 template<typename _Clock, typename _Dur1, typename _Dur2> 668 constexpr bool 669 operator<(const time_point<_Clock, _Dur1>& __lhs, 670 const time_point<_Clock, _Dur2>& __rhs) 671 { return __lhs.time_since_epoch() < __rhs.time_since_epoch(); } 672 673 template<typename _Clock, typename _Dur1, typename _Dur2> 674 constexpr bool 675 operator<=(const time_point<_Clock, _Dur1>& __lhs, 676 const time_point<_Clock, _Dur2>& __rhs) 677 { return !(__rhs < __lhs); } 678 679 template<typename _Clock, typename _Dur1, typename _Dur2> 680 constexpr bool 681 operator>(const time_point<_Clock, _Dur1>& __lhs, 682 const time_point<_Clock, _Dur2>& __rhs) 683 { return __rhs < __lhs; } 684 685 template<typename _Clock, typename _Dur1, typename _Dur2> 686 constexpr bool 687 operator>=(const time_point<_Clock, _Dur1>& __lhs, 688 const time_point<_Clock, _Dur2>& __rhs) 689 { return !(__lhs < __rhs); } 690 691 692 // Clocks. 693 694 // Why nanosecond resolution as the default? 695 // Why have std::system_clock always count in the higest 696 // resolution (ie nanoseconds), even if on some OSes the low 3 697 // or 9 decimal digits will be always zero? This allows later 698 // implementations to change the system_clock::now() 699 // implementation any time to provide better resolution without 700 // changing function signature or units. 701 702 // To support the (forward) evolution of the library's defined 703 // clocks, wrap inside inline namespace so that the current 704 // defintions of system_clock, steady_clock, and 705 // high_resolution_clock types are uniquely mangled. This way, new 706 // code can use the latests clocks, while the library can contain 707 // compatibility definitions for previous versions. At some 708 // point, when these clocks settle down, the inlined namespaces 709 // can be removed. XXX GLIBCXX_ABI Deprecated 710 inline namespace _V2 { 711 712 /** 713 * @brief System clock. 714 * 715 * Time returned represents wall time from the system-wide clock. 716 */ 717 struct system_clock 718 { 719 typedef chrono::nanoseconds duration; 720 typedef duration::rep rep; 721 typedef duration::period period; 722 typedef chrono::time_point<system_clock, duration> time_point; 723 724 static_assert(system_clock::duration::min() 725 < system_clock::duration::zero(), 726 "a clock's minimum duration cannot be less than its epoch"); 727 728 static constexpr bool is_steady = false; 729 730 static time_point 731 now() noexcept; 732 733 // Map to C API 734 static std::time_t 735 to_time_t(const time_point& __t) noexcept 736 { 737 return std::time_t(duration_cast<chrono::seconds> 738 (__t.time_since_epoch()).count()); 739 } 740 741 static time_point 742 from_time_t(std::time_t __t) noexcept 743 { 744 typedef chrono::time_point<system_clock, seconds> __from; 745 return time_point_cast<system_clock::duration> 746 (__from(chrono::seconds(__t))); 747 } 748 }; 749 750 751 /** 752 * @brief Monotonic clock 753 * 754 * Time returned has the property of only increasing at a uniform rate. 755 */ 756 struct steady_clock 757 { 758 typedef chrono::nanoseconds duration; 759 typedef duration::rep rep; 760 typedef duration::period period; 761 typedef chrono::time_point<steady_clock, duration> time_point; 762 763 static constexpr bool is_steady = true; 764 765 static time_point 766 now() noexcept; 767 }; 768 769 770 /** 771 * @brief Highest-resolution clock 772 * 773 * This is the clock "with the shortest tick period." Alias to 774 * std::system_clock until higher-than-nanosecond definitions 775 * become feasible. 776 */ 777 using high_resolution_clock = system_clock; 778 779 } // end inline namespace _V2 780 781 _GLIBCXX_END_NAMESPACE_VERSION 782 } // namespace chrono 783 784#if __cplusplus > 201103L 785 786#define __cpp_lib_chrono_udls 201304 787 788 inline namespace literals 789 { 790 inline namespace chrono_literals 791 { 792 _GLIBCXX_BEGIN_NAMESPACE_VERSION 793 794 template<typename _Rep, unsigned long long _Val> 795 struct _Checked_integral_constant 796 : integral_constant<_Rep, static_cast<_Rep>(_Val)> 797 { 798 static_assert(_Checked_integral_constant::value >= 0 799 && _Checked_integral_constant::value == _Val, 800 "literal value cannot be represented by duration type"); 801 }; 802 803 template<typename _Dur, char... _Digits> 804 constexpr _Dur __check_overflow() 805 { 806 using _Val = __parse_int::_Parse_int<_Digits...>; 807 using _Rep = typename _Dur::rep; 808 // TODO: should be simply integral_constant<_Rep, _Val::value> 809 // but GCC doesn't reject narrowing conversions to _Rep. 810 using _CheckedVal = _Checked_integral_constant<_Rep, _Val::value>; 811 return _Dur{_CheckedVal::value}; 812 } 813 814 constexpr chrono::duration<long double, ratio<3600,1>> 815 operator""h(long double __hours) 816 { return chrono::duration<long double, ratio<3600,1>>{__hours}; } 817 818 template <char... _Digits> 819 constexpr chrono::hours 820 operator""h() 821 { return __check_overflow<chrono::hours, _Digits...>(); } 822 823 constexpr chrono::duration<long double, ratio<60,1>> 824 operator""min(long double __mins) 825 { return chrono::duration<long double, ratio<60,1>>{__mins}; } 826 827 template <char... _Digits> 828 constexpr chrono::minutes 829 operator""min() 830 { return __check_overflow<chrono::minutes, _Digits...>(); } 831 832 constexpr chrono::duration<long double> 833 operator""s(long double __secs) 834 { return chrono::duration<long double>{__secs}; } 835 836 template <char... _Digits> 837 constexpr chrono::seconds 838 operator""s() 839 { return __check_overflow<chrono::seconds, _Digits...>(); } 840 841 constexpr chrono::duration<long double, milli> 842 operator""ms(long double __msecs) 843 { return chrono::duration<long double, milli>{__msecs}; } 844 845 template <char... _Digits> 846 constexpr chrono::milliseconds 847 operator""ms() 848 { return __check_overflow<chrono::milliseconds, _Digits...>(); } 849 850 constexpr chrono::duration<long double, micro> 851 operator""us(long double __usecs) 852 { return chrono::duration<long double, micro>{__usecs}; } 853 854 template <char... _Digits> 855 constexpr chrono::microseconds 856 operator""us() 857 { return __check_overflow<chrono::microseconds, _Digits...>(); } 858 859 constexpr chrono::duration<long double, nano> 860 operator""ns(long double __nsecs) 861 { return chrono::duration<long double, nano>{__nsecs}; } 862 863 template <char... _Digits> 864 constexpr chrono::nanoseconds 865 operator""ns() 866 { return __check_overflow<chrono::nanoseconds, _Digits...>(); } 867 868 _GLIBCXX_END_NAMESPACE_VERSION 869 } // inline namespace chrono_literals 870 } // inline namespace literals 871 872 namespace chrono 873 { 874 _GLIBCXX_BEGIN_NAMESPACE_VERSION 875 876 using namespace literals::chrono_literals; 877 878 _GLIBCXX_END_NAMESPACE_VERSION 879 } // namespace chrono 880 881#endif // __cplusplus > 201103L 882 883 // @} group chrono 884} // namespace std 885 886#endif //_GLIBCXX_USE_C99_STDINT_TR1 887 888#endif // C++11 889 890#endif //_GLIBCXX_CHRONO 891