xtime.hpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // Copyright (C) 2001-2003
  2. // William E. Kempf
  3. // Copyright (C) 2007-8 Anthony Williams
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. #ifndef BOOST_XTIME_WEK070601_HPP
  8. #define BOOST_XTIME_WEK070601_HPP
  9. #include <boost/thread/detail/config.hpp>
  10. #if defined BOOST_THREAD_USES_DATETIME
  11. #include <boost/cstdint.hpp>
  12. #include <boost/thread/thread_time.hpp>
  13. #include <boost/date_time/posix_time/conversion.hpp>
  14. #include <boost/config/abi_prefix.hpp>
  15. namespace boost {
  16. enum xtime_clock_types
  17. {
  18. TIME_UTC_=1
  19. // TIME_TAI,
  20. // TIME_MONOTONIC,
  21. // TIME_PROCESS,
  22. // TIME_THREAD,
  23. // TIME_LOCAL,
  24. // TIME_SYNC,
  25. // TIME_RESOLUTION
  26. };
  27. struct xtime
  28. {
  29. #if defined(BOOST_NO_INT64_T)
  30. typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX
  31. #else
  32. typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX
  33. #endif
  34. typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND
  35. xtime_sec_t sec;
  36. xtime_nsec_t nsec;
  37. operator system_time() const
  38. {
  39. return boost::posix_time::from_time_t(0)+
  40. boost::posix_time::seconds(static_cast<long>(sec))+
  41. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  42. boost::posix_time::nanoseconds(nsec);
  43. #else
  44. boost::posix_time::microseconds((nsec+500)/1000);
  45. #endif
  46. }
  47. };
  48. inline ::boost::xtime get_xtime(boost::system_time const& abs_time)
  49. {
  50. ::boost::xtime res;
  51. boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
  52. res.sec=static_cast< ::boost::xtime::xtime_sec_t>(time_since_epoch.total_seconds());
  53. res.nsec=static_cast< ::boost::xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()));
  54. return res;
  55. }
  56. inline int xtime_get(struct ::boost::xtime* xtp, int clock_type)
  57. {
  58. if (clock_type == TIME_UTC_)
  59. {
  60. *xtp=get_xtime(get_system_time());
  61. return clock_type;
  62. }
  63. return 0;
  64. }
  65. inline int xtime_cmp(const ::boost::xtime& xt1, const ::boost::xtime& xt2)
  66. {
  67. if (xt1.sec == xt2.sec)
  68. return (int)(xt1.nsec - xt2.nsec);
  69. else
  70. return (xt1.sec > xt2.sec) ? 1 : -1;
  71. }
  72. } // namespace boost
  73. #include <boost/config/abi_suffix.hpp>
  74. #endif
  75. #endif //BOOST_XTIME_WEK070601_HPP