File indexing completed on 2026-09-16 08:49:50
0001
0002
0003
0004
0005
0006
0007
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 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 # pragma once
0021 #endif
0022
0023 #include <boost/interprocess/detail/mpl.hpp>
0024 #include <boost/interprocess/detail/type_traits.hpp>
0025 #include <boost/interprocess/timed_utils.hpp>
0026
0027 namespace boost {
0028
0029 namespace interprocess {
0030
0031 namespace ipcdetail {
0032
0033 template<class TimePoint>
0034 inline timespec timepoint_to_timespec ( const TimePoint &tm
0035 , typename enable_if_ptime<TimePoint>::type * = 0)
0036 {
0037 typedef typename TimePoint::date_type date_type;
0038 typedef typename TimePoint::time_duration_type time_duration_type;
0039
0040 const TimePoint epoch(date_type(1970,1,1));
0041
0042
0043 time_duration_type duration = (tm <= epoch) ? time_duration_type(epoch - epoch)
0044 : time_duration_type(tm - epoch);
0045 timespec ts;
0046 ts.tv_sec = static_cast<time_t>(duration.total_seconds());
0047 ts.tv_nsec = static_cast<long>(duration.total_nanoseconds() % 1000000000);
0048 return ts;
0049 }
0050
0051 inline timespec timepoint_to_timespec (const ustime &tm)
0052 {
0053 timespec ts;
0054 const boost::uint64_t micros = tm.get_microsecs();
0055 ts.tv_sec = static_cast<time_t>(micros /1000000u);
0056 ts.tv_nsec = static_cast<long>((micros%1000000u)*1000u);
0057 return ts;
0058 }
0059
0060 template<class TimePoint>
0061 inline timespec timepoint_to_timespec ( const TimePoint &tm
0062 , typename enable_if_time_point<TimePoint>::type * = 0)
0063 {
0064 typedef typename TimePoint::duration duration_t;
0065 duration_t d(tm.time_since_epoch());
0066
0067 timespec ts;
0068 const typename duration_t::rep cnt = d.count();
0069
0070 BOOST_IF_CONSTEXPR(duration_t::period::num == 1 && duration_t::period::den == 1000000000)
0071 {
0072
0073 ts.tv_sec = static_cast<time_t>(cnt /duration_t::period::den);
0074 ts.tv_nsec = static_cast<long>(cnt%duration_t::period::den);
0075 }
0076 else
0077 {
0078 const double factor = double(duration_t::period::num)/double(duration_t::period::den);
0079 const double res = double(cnt)*factor;
0080 ts.tv_sec = static_cast<time_t>(res);
0081 ts.tv_nsec = static_cast<long>(1000000000.0*(res - double(ts.tv_sec)));
0082 }
0083 return ts;
0084 }
0085
0086 }
0087
0088 }
0089
0090 }
0091
0092 #endif