Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-09 08:13:43

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/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    const boost::uint64_t micros = tm.get_microsecs();
0054    ts.tv_sec  = static_cast<time_t>(micros /1000000u);
0055    ts.tv_nsec = static_cast<long>((micros%1000000u)*1000u);
0056    return ts;
0057 }
0058 
0059 template<class TimePoint>
0060 inline timespec timepoint_to_timespec ( const TimePoint &tm
0061                                       , typename enable_if_time_point<TimePoint>::type * = 0)
0062 {
0063    typedef typename TimePoint::duration duration_t;
0064    duration_t d(tm.time_since_epoch());
0065 
0066    timespec ts;
0067    const typename duration_t::rep cnt = d.count();
0068 
0069    BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000)
0070    {
0071 
0072       ts.tv_sec  = static_cast<time_t>(cnt /duration_t::period::den);
0073       ts.tv_nsec = static_cast<long>(cnt%duration_t::period::den);
0074    }
0075    else
0076    {
0077       const double factor = double(duration_t::period::num)/double(duration_t::period::den);
0078       const double res = double(cnt)*factor;
0079       ts.tv_sec  = static_cast<time_t>(res);
0080       ts.tv_nsec = static_cast<long>(1000000000.0*(res - double(ts.tv_sec)));
0081    }
0082    return ts;
0083 }
0084 
0085 }  //namespace ipcdetail {
0086 
0087 }  //namespace interprocess {
0088 
0089 }  //namespace boost {
0090 
0091 #endif   //ifndef BOOST_INTERPROCESS_DETAIL_TIMEPOINT_TO_TIMESPEC_HPP