Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:31

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/mpl.hpp>
0023 #include <boost/interprocess/detail/type_traits.hpp>
0024 #include <boost/interprocess/detail/timed_utils.hpp>
0025 
0026 namespace boost {
0027 
0028 namespace interprocess {
0029 
0030 namespace ipcdetail {
0031 
0032 template<class TimePoint>
0033 inline timespec timepoint_to_timespec ( const TimePoint &tm
0034                                       , typename enable_if_ptime<TimePoint>::type * = 0)
0035 {
0036    typedef typename TimePoint::date_type            date_type;
0037    typedef typename TimePoint::time_duration_type   time_duration_type;
0038 
0039    const TimePoint epoch(date_type(1970,1,1));
0040 
0041    //Avoid negative absolute times
0042    time_duration_type duration  = (tm <= epoch) ? time_duration_type(epoch - epoch)
0043                                                 : time_duration_type(tm - epoch);
0044    timespec ts;
0045    ts.tv_sec  = static_cast<time_t>(duration.total_seconds());
0046    ts.tv_nsec = static_cast<long>(duration.total_nanoseconds() % 1000000000);
0047    return ts;
0048 }
0049 
0050 inline timespec timepoint_to_timespec (const ustime &tm)
0051 {
0052    timespec ts;
0053    ts.tv_sec  = static_cast<time_t>(tm.get_microsecs()/1000000u);
0054    ts.tv_nsec = static_cast<long>((tm.get_microsecs()%1000000u)*1000u);
0055    return ts;
0056 }
0057 
0058 template<class TimePoint>
0059 inline timespec timepoint_to_timespec ( const TimePoint &tm
0060                                       , typename enable_if_time_point<TimePoint>::type * = 0)
0061 {
0062    typedef typename TimePoint::duration duration_t;
0063    duration_t d(tm.time_since_epoch());
0064 
0065    timespec ts;
0066    BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000)
0067    {
0068       ts.tv_sec  = static_cast<time_t>(d.count()/duration_t::period::den);
0069       ts.tv_nsec = static_cast<long>(d.count()%duration_t::period::den);
0070    }
0071    else
0072    {
0073       const double factor = double(duration_t::period::num)/double(duration_t::period::den);
0074       const double res = d.count()*factor;
0075       ts.tv_sec  = static_cast<time_t>(res);
0076       ts.tv_nsec = static_cast<long>(res - double(ts.tv_sec));
0077    }
0078    return ts;
0079 }
0080 
0081 }  //namespace ipcdetail {
0082 
0083 }  //namespace interprocess {
0084 
0085 }  //namespace boost {
0086 
0087 #endif   //ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP