timepoint_to_timespec.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/detail/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. ts.tv_sec = static_cast<time_t>(tm.get_microsecs()/1000000u);
  44. ts.tv_nsec = static_cast<long>((tm.get_microsecs()%1000000u)*1000u);
  45. return ts;
  46. }
  47. template<class TimePoint>
  48. inline timespec timepoint_to_timespec ( const TimePoint &tm
  49. , typename enable_if_time_point<TimePoint>::type * = 0)
  50. {
  51. typedef typename TimePoint::duration duration_t;
  52. duration_t d(tm.time_since_epoch());
  53. timespec ts;
  54. BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000)
  55. {
  56. ts.tv_sec = static_cast<time_t>(d.count()/duration_t::period::den);
  57. ts.tv_nsec = static_cast<long>(d.count()%duration_t::period::den);
  58. }
  59. else
  60. {
  61. const double factor = double(duration_t::period::num)/double(duration_t::period::den);
  62. const double res = d.count()*factor;
  63. ts.tv_sec = static_cast<time_t>(res);
  64. ts.tv_nsec = static_cast<long>(res - double(ts.tv_sec));
  65. }
  66. return ts;
  67. }
  68. } //namespace ipcdetail {
  69. } //namespace interprocess {
  70. } //namespace boost {
  71. #endif //ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP