timepoint_to_timespec.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/mpl.hpp>
  20. #include <boost/interprocess/detail/type_traits.hpp>
  21. #include <boost/interprocess/timed_utils.hpp>
  22. namespace boost {
  23. namespace interprocess {
  24. namespace ipcdetail {
  25. template<class TimePoint>
  26. inline timespec timepoint_to_timespec ( const TimePoint &tm
  27. , typename enable_if_ptime<TimePoint>::type * = 0)
  28. {
  29. typedef typename TimePoint::date_type date_type;
  30. typedef typename TimePoint::time_duration_type time_duration_type;
  31. const TimePoint epoch(date_type(1970,1,1));
  32. //Avoid negative absolute times
  33. time_duration_type duration = (tm <= epoch) ? time_duration_type(epoch - epoch)
  34. : time_duration_type(tm - epoch);
  35. timespec ts;
  36. ts.tv_sec = static_cast<time_t>(duration.total_seconds());
  37. ts.tv_nsec = static_cast<long>(duration.total_nanoseconds() % 1000000000);
  38. return ts;
  39. }
  40. inline timespec timepoint_to_timespec (const ustime &tm)
  41. {
  42. timespec ts;
  43. const boost::uint64_t micros = tm.get_microsecs();
  44. ts.tv_sec = static_cast<time_t>(micros /1000000u);
  45. ts.tv_nsec = static_cast<long>((micros%1000000u)*1000u);
  46. return ts;
  47. }
  48. template<class TimePoint>
  49. inline timespec timepoint_to_timespec ( const TimePoint &tm
  50. , typename enable_if_time_point<TimePoint>::type * = 0)
  51. {
  52. typedef typename TimePoint::duration duration_t;
  53. duration_t d(tm.time_since_epoch());
  54. timespec ts;
  55. const typename duration_t::rep cnt = d.count();
  56. BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000)
  57. {
  58. ts.tv_sec = static_cast<time_t>(cnt /duration_t::period::den);
  59. ts.tv_nsec = static_cast<long>(cnt%duration_t::period::den);
  60. }
  61. else
  62. {
  63. const double factor = double(duration_t::period::num)/double(duration_t::period::den);
  64. const double res = double(cnt)*factor;
  65. ts.tv_sec = static_cast<time_t>(res);
  66. ts.tv_nsec = static_cast<long>(1000000000.0*(res - double(ts.tv_sec)));
  67. }
  68. return ts;
  69. }
  70. } //namespace ipcdetail {
  71. } //namespace interprocess {
  72. } //namespace boost {
  73. #endif //ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP