////////////////////////////////////////////////////////////////////////////// // // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // // See http://www.boost.org/libs/interprocess for documentation. // ////////////////////////////////////////////////////////////////////////////// #ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP #define BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP #ifndef BOOST_CONFIG_HPP # include #endif # #if defined(BOOST_HAS_PRAGMA_ONCE) # pragma once #endif #include #include #include namespace boost { namespace interprocess { namespace ipcdetail { template inline timespec timepoint_to_timespec ( const TimePoint &tm , typename enable_if_ptime::type * = 0) { typedef typename TimePoint::date_type date_type; typedef typename TimePoint::time_duration_type time_duration_type; const TimePoint epoch(date_type(1970,1,1)); //Avoid negative absolute times time_duration_type duration = (tm <= epoch) ? time_duration_type(epoch - epoch) : time_duration_type(tm - epoch); timespec ts; ts.tv_sec = static_cast(duration.total_seconds()); ts.tv_nsec = static_cast(duration.total_nanoseconds() % 1000000000); return ts; } inline timespec timepoint_to_timespec (const ustime &tm) { timespec ts; ts.tv_sec = static_cast(tm.get_microsecs()/1000000u); ts.tv_nsec = static_cast((tm.get_microsecs()%1000000u)*1000u); return ts; } template inline timespec timepoint_to_timespec ( const TimePoint &tm , typename enable_if_time_point::type * = 0) { typedef typename TimePoint::duration duration_t; duration_t d(tm.time_since_epoch()); timespec ts; BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000) { ts.tv_sec = static_cast(d.count()/duration_t::period::den); ts.tv_nsec = static_cast(d.count()%duration_t::period::den); } else { const double factor = double(duration_t::period::num)/double(duration_t::period::den); const double res = d.count()*factor; ts.tv_sec = static_cast(res); ts.tv_nsec = static_cast(res - double(ts.tv_sec)); } return ts; } } //namespace ipcdetail { } //namespace interprocess { } //namespace boost { #endif //ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP