microsec_time_clock.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #ifndef DATE_TIME_HIGHRES_TIME_CLOCK_HPP___
  2. #define DATE_TIME_HIGHRES_TIME_CLOCK_HPP___
  3. /* Copyright (c) 2002,2003,2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. /*! @file microsec_time_clock.hpp
  11. This file contains a high resolution time clock implementation.
  12. */
  13. #include <boost/cstdint.hpp>
  14. #include <boost/shared_ptr.hpp>
  15. #include <boost/detail/workaround.hpp>
  16. #include <boost/date_time/compiler_config.hpp>
  17. #include <boost/date_time/c_time.hpp>
  18. #include <boost/date_time/time_clock.hpp>
  19. #if defined(BOOST_HAS_FTIME)
  20. #include <boost/winapi/time.hpp>
  21. #endif
  22. #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
  23. namespace boost {
  24. namespace date_time {
  25. //! A clock providing microsecond level resolution
  26. /*! A high precision clock that measures the local time
  27. * at a resolution up to microseconds and adjusts to the
  28. * resolution of the time system. For example, for the
  29. * a library configuration with nano second resolution,
  30. * the last 3 places of the fractional seconds will always
  31. * be 000 since there are 1000 nano-seconds in a micro second.
  32. */
  33. template<class time_type>
  34. class microsec_clock
  35. {
  36. private:
  37. //! Type for the function used to convert time_t to tm
  38. typedef std::tm* (*time_converter)(const std::time_t*, std::tm*);
  39. public:
  40. typedef typename time_type::date_type date_type;
  41. typedef typename time_type::time_duration_type time_duration_type;
  42. typedef typename time_duration_type::rep_type resolution_traits_type;
  43. //! return a local time object for the given zone, based on computer clock
  44. //JKG -- looks like we could rewrite this against universal_time
  45. template<class time_zone_type>
  46. static time_type local_time(shared_ptr<time_zone_type> tz_ptr)
  47. {
  48. typedef typename time_type::utc_time_type utc_time_type;
  49. typedef second_clock<utc_time_type> second_clock;
  50. // we'll need to know the utc_offset this machine has
  51. // in order to get a utc_time_type set to utc
  52. utc_time_type utc_time = second_clock::universal_time();
  53. time_duration_type utc_offset = second_clock::local_time() - utc_time;
  54. // use micro clock to get a local time with sub seconds
  55. // and adjust it to get a true utc time reading with sub seconds
  56. utc_time = microsec_clock<utc_time_type>::local_time() - utc_offset;
  57. return time_type(utc_time, tz_ptr);
  58. }
  59. //! Returns the local time based on computer clock settings
  60. static time_type local_time()
  61. {
  62. return create_time(&c_time::localtime);
  63. }
  64. //! Returns the UTC time based on computer settings
  65. static time_type universal_time()
  66. {
  67. return create_time(&c_time::gmtime);
  68. }
  69. private:
  70. static time_type create_time(time_converter converter)
  71. {
  72. #ifdef BOOST_HAS_GETTIMEOFDAY
  73. timeval tv;
  74. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  75. std::time_t t = tv.tv_sec;
  76. boost::uint32_t sub_sec = tv.tv_usec;
  77. #elif defined(BOOST_HAS_FTIME)
  78. boost::winapi::FILETIME_ ft;
  79. boost::winapi::GetSystemTimeAsFileTime(&ft);
  80. #if BOOST_WORKAROUND(__MWERKS__, BOOST_TESTED_AT(0x3205))
  81. // Some runtime library implementations expect local times as the norm for ctime functions.
  82. {
  83. boost::winapi::FILETIME_ local_ft;
  84. boost::winapi::FileTimeToLocalFileTime(&ft, &local_ft);
  85. ft = local_ft;
  86. }
  87. #endif
  88. boost::uint64_t micros = file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  89. // and cannot be before 1970-Jan-01
  90. std::time_t t = static_cast<std::time_t>(micros / 1000000UL); // seconds since epoch
  91. // microseconds -- static casts suppress warnings
  92. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(micros % 1000000UL);
  93. #else
  94. #error Internal Boost.DateTime error: BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK is defined, however neither gettimeofday nor FILETIME support is detected.
  95. #endif
  96. std::tm curr;
  97. std::tm* curr_ptr = converter(&t, &curr);
  98. date_type d(static_cast< typename date_type::year_type::value_type >(curr_ptr->tm_year + 1900),
  99. static_cast< typename date_type::month_type::value_type >(curr_ptr->tm_mon + 1),
  100. static_cast< typename date_type::day_type::value_type >(curr_ptr->tm_mday));
  101. //The following line will adjust the fractional second tick in terms
  102. //of the current time system. For example, if the time system
  103. //doesn't support fractional seconds then res_adjust returns 0
  104. //and all the fractional seconds return 0.
  105. int adjust = static_cast< int >(resolution_traits_type::res_adjust() / 1000000);
  106. time_duration_type td(static_cast< typename time_duration_type::hour_type >(curr_ptr->tm_hour),
  107. static_cast< typename time_duration_type::min_type >(curr_ptr->tm_min),
  108. static_cast< typename time_duration_type::sec_type >(curr_ptr->tm_sec),
  109. sub_sec * adjust);
  110. return time_type(d,td);
  111. }
  112. #if defined(BOOST_HAS_FTIME)
  113. /*!
  114. * The function converts file_time into number of microseconds elapsed since 1970-Jan-01
  115. *
  116. * \note Only dates after 1970-Jan-01 are supported. Dates before will be wrapped.
  117. */
  118. static boost::uint64_t file_time_to_microseconds(boost::winapi::FILETIME_ const& ft)
  119. {
  120. // shift is difference between 1970-Jan-01 & 1601-Jan-01
  121. // in 100-nanosecond units
  122. const boost::uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008
  123. // 100-nanos since 1601-Jan-01
  124. boost::uint64_t ft_as_integer = (static_cast< boost::uint64_t >(ft.dwHighDateTime) << 32) | static_cast< boost::uint64_t >(ft.dwLowDateTime);
  125. ft_as_integer -= shift; // filetime is now 100-nanos since 1970-Jan-01
  126. return (ft_as_integer / 10U); // truncate to microseconds
  127. }
  128. #endif
  129. };
  130. } } //namespace date_time
  131. #endif //BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
  132. #endif