xtime.hpp 2.2 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. #include <boost/cstdint.hpp>
  11. #include <boost/thread/thread_time.hpp>
  12. #include <boost/date_time/posix_time/conversion.hpp>
  13. #include <boost/config/abi_prefix.hpp>
  14. namespace boost {
  15. enum xtime_clock_types
  16. {
  17. TIME_UTC=1
  18. // TIME_TAI,
  19. // TIME_MONOTONIC,
  20. // TIME_PROCESS,
  21. // TIME_THREAD,
  22. // TIME_LOCAL,
  23. // TIME_SYNC,
  24. // TIME_RESOLUTION
  25. };
  26. struct xtime
  27. {
  28. #if defined(BOOST_NO_INT64_T)
  29. typedef int_fast32_t xtime_sec_t; //INT_FAST32_MIN <= sec <= INT_FAST32_MAX
  30. #else
  31. typedef int_fast64_t xtime_sec_t; //INT_FAST64_MIN <= sec <= INT_FAST64_MAX
  32. #endif
  33. typedef int_fast32_t xtime_nsec_t; //0 <= xtime.nsec < NANOSECONDS_PER_SECOND
  34. xtime_sec_t sec;
  35. xtime_nsec_t nsec;
  36. operator system_time() const
  37. {
  38. return boost::posix_time::from_time_t(0)+
  39. boost::posix_time::seconds(static_cast<long>(sec))+
  40. #ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
  41. boost::posix_time::nanoseconds(nsec);
  42. #else
  43. boost::posix_time::microseconds((nsec+500)/1000);
  44. #endif
  45. }
  46. };
  47. inline xtime get_xtime(boost::system_time const& abs_time)
  48. {
  49. xtime res;
  50. boost::posix_time::time_duration const time_since_epoch=abs_time-boost::posix_time::from_time_t(0);
  51. res.sec=static_cast<xtime::xtime_sec_t>(time_since_epoch.total_seconds());
  52. res.nsec=static_cast<xtime::xtime_nsec_t>(time_since_epoch.fractional_seconds()*(1000000000/time_since_epoch.ticks_per_second()));
  53. return res;
  54. }
  55. inline int xtime_get(struct xtime* xtp, int clock_type)
  56. {
  57. if (clock_type == TIME_UTC)
  58. {
  59. *xtp=get_xtime(get_system_time());
  60. return clock_type;
  61. }
  62. return 0;
  63. }
  64. inline int xtime_cmp(const xtime& xt1, const xtime& xt2)
  65. {
  66. if (xt1.sec == xt2.sec)
  67. return (int)(xt1.nsec - xt2.nsec);
  68. else
  69. return (xt1.sec > xt2.sec) ? 1 : -1;
  70. }
  71. } // namespace boost
  72. #include <boost/config/abi_suffix.hpp>
  73. #endif //BOOST_XTIME_WEK070601_HPP