microsec_time_clock.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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: 2009-06-04 07:36:43 -0400 (Thu, 04 Jun 2009) $
  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. #include <boost/date_time/filetime_functions.hpp>
  20. #ifdef BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
  21. namespace boost {
  22. namespace date_time {
  23. //! A clock providing microsecond level resolution
  24. /*! A high precision clock that measures the local time
  25. * at a resolution up to microseconds and adjusts to the
  26. * resolution of the time system. For example, for the
  27. * a library configuration with nano second resolution,
  28. * the last 3 places of the fractional seconds will always
  29. * be 000 since there are 1000 nano-seconds in a micro second.
  30. */
  31. template<class time_type>
  32. class microsec_clock
  33. {
  34. private:
  35. //! Type for the function used to convert time_t to tm
  36. typedef std::tm* (*time_converter)(const std::time_t*, std::tm*);
  37. public:
  38. typedef typename time_type::date_type date_type;
  39. typedef typename time_type::time_duration_type time_duration_type;
  40. typedef typename time_duration_type::rep_type resolution_traits_type;
  41. //! return a local time object for the given zone, based on computer clock
  42. //JKG -- looks like we could rewrite this against universal_time
  43. template<class time_zone_type>
  44. static time_type local_time(shared_ptr<time_zone_type> tz_ptr)
  45. {
  46. typedef typename time_type::utc_time_type utc_time_type;
  47. typedef second_clock<utc_time_type> second_clock;
  48. // we'll need to know the utc_offset this machine has
  49. // in order to get a utc_time_type set to utc
  50. utc_time_type utc_time = second_clock::universal_time();
  51. time_duration_type utc_offset = second_clock::local_time() - utc_time;
  52. // use micro clock to get a local time with sub seconds
  53. // and adjust it to get a true utc time reading with sub seconds
  54. utc_time = microsec_clock<utc_time_type>::local_time() - utc_offset;
  55. return time_type(utc_time, tz_ptr);
  56. }
  57. //! Returns the local time based on computer clock settings
  58. static time_type local_time()
  59. {
  60. return create_time(&c_time::localtime);
  61. }
  62. //! Returns the UTC time based on computer settings
  63. static time_type universal_time()
  64. {
  65. return create_time(&c_time::gmtime);
  66. }
  67. private:
  68. static time_type create_time(time_converter converter)
  69. {
  70. #ifdef BOOST_HAS_GETTIMEOFDAY
  71. timeval tv;
  72. gettimeofday(&tv, 0); //gettimeofday does not support TZ adjust on Linux.
  73. std::time_t t = tv.tv_sec;
  74. boost::uint32_t sub_sec = tv.tv_usec;
  75. #elif defined(BOOST_HAS_FTIME)
  76. winapi::file_time ft;
  77. winapi::get_system_time_as_file_time(ft);
  78. uint64_t micros = winapi::file_time_to_microseconds(ft); // it will not wrap, since ft is the current time
  79. // and cannot be before 1970-Jan-01
  80. std::time_t t = static_cast<std::time_t>(micros / 1000000UL); // seconds since epoch
  81. // microseconds -- static casts supress warnings
  82. boost::uint32_t sub_sec = static_cast<boost::uint32_t>(micros % 1000000UL);
  83. #else
  84. #error Internal Boost.DateTime error: BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK is defined, however neither gettimeofday nor FILETIME support is detected.
  85. #endif
  86. std::tm curr;
  87. std::tm* curr_ptr = converter(&t, &curr);
  88. date_type d(curr_ptr->tm_year + 1900,
  89. curr_ptr->tm_mon + 1,
  90. curr_ptr->tm_mday);
  91. //The following line will adjust the fractional second tick in terms
  92. //of the current time system. For example, if the time system
  93. //doesn't support fractional seconds then res_adjust returns 0
  94. //and all the fractional seconds return 0.
  95. int adjust = static_cast< int >(resolution_traits_type::res_adjust() / 1000000);
  96. time_duration_type td(curr_ptr->tm_hour,
  97. curr_ptr->tm_min,
  98. curr_ptr->tm_sec,
  99. sub_sec * adjust);
  100. return time_type(d,td);
  101. }
  102. };
  103. } } //namespace date_time
  104. #endif //BOOST_DATE_TIME_HAS_HIGH_PRECISION_CLOCK
  105. #endif