chrono.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // posix/chrono.cpp --------------------------------------------------------------//
  2. // Copyright Beman Dawes 2008
  3. // Copyright Vicente J. Botet Escriba 2009
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // See http://www.boost.org/LICENSE_1_0.txt
  6. //----------------------------------------------------------------------------//
  7. // POSIX //
  8. //----------------------------------------------------------------------------//
  9. #include <time.h> // for clock_gettime
  10. #include <boost/assert.hpp>
  11. #include <boost/predef/os.h>
  12. namespace boost
  13. {
  14. namespace chrono
  15. {
  16. system_clock::time_point system_clock::now() BOOST_NOEXCEPT
  17. {
  18. timespec ts;
  19. if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
  20. {
  21. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  22. }
  23. return time_point(duration(
  24. static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  25. }
  26. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  27. system_clock::time_point system_clock::now(system::error_code & ec)
  28. {
  29. timespec ts;
  30. if ( ::clock_gettime( CLOCK_REALTIME, &ts ) )
  31. {
  32. if (::boost::chrono::is_throws(ec))
  33. {
  34. boost::throw_exception(
  35. system::system_error(
  36. errno,
  37. ::boost::system::system_category(),
  38. "chrono::system_clock" ));
  39. }
  40. else
  41. {
  42. ec.assign( errno, ::boost::system::system_category() );
  43. return time_point();
  44. }
  45. }
  46. if (!::boost::chrono::is_throws(ec))
  47. {
  48. ec.clear();
  49. }
  50. return time_point(duration(
  51. static_cast<system_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  52. }
  53. #endif
  54. std::time_t system_clock::to_time_t(const system_clock::time_point& t) BOOST_NOEXCEPT
  55. {
  56. return static_cast<std::time_t>( t.time_since_epoch().count() / 1000000000 );
  57. }
  58. system_clock::time_point system_clock::from_time_t(std::time_t t) BOOST_NOEXCEPT
  59. {
  60. return time_point(duration(static_cast<system_clock::rep>(t) * 1000000000));
  61. }
  62. #ifdef BOOST_CHRONO_HAS_CLOCK_STEADY
  63. steady_clock::time_point steady_clock::now() BOOST_NOEXCEPT
  64. {
  65. timespec ts;
  66. #if BOOST_OS_CYGWIN
  67. // lack of thread safety in high resolution timer initialization
  68. // can lead to a timespec of zero without an error; was reported
  69. // to the cygwin mailing list and can be removed once fixed
  70. do
  71. {
  72. #endif
  73. if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
  74. {
  75. BOOST_ASSERT(0 && "Boost::Chrono - Internal Error");
  76. }
  77. #if BOOST_OS_CYGWIN
  78. } while (ts.tv_sec == 0 && ts.tv_nsec == 0);
  79. #endif
  80. return time_point(duration(
  81. static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  82. }
  83. #if !defined BOOST_CHRONO_DONT_PROVIDE_HYBRID_ERROR_HANDLING
  84. steady_clock::time_point steady_clock::now(system::error_code & ec)
  85. {
  86. timespec ts;
  87. #if BOOST_OS_CYGWIN
  88. // lack of thread safety in high resolution timer initialization
  89. // can lead to a timespec of zero without an error; was reported
  90. // to the cygwin mailing list and can be removed once fixed
  91. do
  92. {
  93. #endif
  94. if ( ::clock_gettime( CLOCK_MONOTONIC, &ts ) )
  95. {
  96. if (::boost::chrono::is_throws(ec))
  97. {
  98. boost::throw_exception(
  99. system::system_error(
  100. errno,
  101. ::boost::system::system_category(),
  102. "chrono::steady_clock" ));
  103. }
  104. else
  105. {
  106. ec.assign( errno, ::boost::system::system_category() );
  107. return time_point();
  108. }
  109. }
  110. #if BOOST_OS_CYGWIN
  111. } while (ts.tv_sec == 0 && ts.tv_nsec == 0);
  112. #endif
  113. if (!::boost::chrono::is_throws(ec))
  114. {
  115. ec.clear();
  116. }
  117. return time_point(duration(
  118. static_cast<steady_clock::rep>( ts.tv_sec ) * 1000000000 + ts.tv_nsec));
  119. }
  120. #endif
  121. #endif
  122. } // namespace chrono
  123. } // namespace boost