35 #ifndef ROS_TIME_IMPL_H_INCLUDED
36 #define ROS_TIME_IMPL_H_INCLUDED
48 #include <boost/date_time/posix_time/posix_time.hpp>
55 #include <sys/timeb.h>
63 template<
class T,
class D>
71 sec =
static_cast<uint32_t
>(sec64);
72 nsec =
static_cast<uint32_t
>(nsec64);
74 return *
static_cast<T*
>(
this);
77 template<
class T,
class D>
80 throw std::runtime_error(
"Time cannot be negative.");
81 if (!std::isfinite(t))
82 throw std::runtime_error(
"Time has to be finite.");
83 constexpr
double maxInt64AsDouble =
static_cast<double>(std::numeric_limits<int64_t>::max());
84 if (t >= maxInt64AsDouble)
85 throw std::runtime_error(
"Time is out of 64-bit integer range");
86 int64_t sec64 =
static_cast<int64_t
>(floor(t));
87 if (sec64 > std::numeric_limits<uint32_t>::max())
88 throw std::runtime_error(
"Time is out of dual 32-bit range");
89 sec =
static_cast<uint32_t
>(sec64);
90 nsec =
static_cast<uint32_t
>(boost::math::round((t-sec) * 1e9));
92 sec += (nsec / 1000000000ul);
94 return *
static_cast<T*
>(
this);
97 template<
class T,
class D>
101 return d.fromNSec(toNSec() - rhs.toNSec());
104 template<
class T,
class D>
107 return *
static_cast<const T*
>(
this) + ( -rhs);
110 template<
class T,
class D>
113 int64_t sec_sum =
static_cast<uint64_t
>(sec) +
static_cast<uint64_t
>(rhs.sec);
114 int64_t nsec_sum =
static_cast<uint64_t
>(nsec) +
static_cast<uint64_t
>(rhs.nsec);
120 return T(
static_cast<uint32_t
>(sec_sum),
static_cast<uint32_t
>(nsec_sum));
123 template<
class T,
class D>
127 return *
static_cast<T*
>(
this);
130 template<
class T,
class D>
134 return *
static_cast<T*
>(
this);
137 template<
class T,
class D>
140 return sec == rhs.sec && nsec == rhs.nsec;
143 template<
class T,
class D>
148 else if (sec == rhs.sec && nsec < rhs.nsec)
153 template<
class T,
class D>
158 else if (sec == rhs.sec && nsec > rhs.nsec)
163 template<
class T,
class D>
168 else if (sec == rhs.sec && nsec <= rhs.nsec)
173 template<
class T,
class D>
178 else if (sec == rhs.sec && nsec >= rhs.nsec)
183 template<
class T,
class D>
184 boost::posix_time::ptime
188 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
189 return pt::from_time_t(sec) + pt::nanoseconds(nsec);
191 return pt::from_time_t(sec) + pt::microseconds(nsec/1000);
196 #endif // ROS_IMPL_TIME_H_INCLUDED