ptime_to_timespec.hpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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_PTIME_TO_TIMESPEC_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_PTIME_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/posix_time_types_wrk.hpp>
  20. namespace boost {
  21. namespace interprocess {
  22. namespace ipcdetail {
  23. inline timespec ptime_to_timespec (const boost::posix_time::ptime &tm)
  24. {
  25. const boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
  26. //Avoid negative absolute times
  27. boost::posix_time::time_duration duration = (tm <= epoch) ? boost::posix_time::time_duration(epoch - epoch)
  28. : boost::posix_time::time_duration(tm - epoch);
  29. timespec ts;
  30. ts.tv_sec = duration.total_seconds();
  31. ts.tv_nsec = duration.total_nanoseconds() % 1000000000;
  32. return ts;
  33. }
  34. } //namespace ipcdetail {
  35. } //namespace interprocess {
  36. } //namespace boost {
  37. #endif //ifndef BOOST_INTERPROCESS_DETAIL_PTIME_TO_TIMESPEC_HPP