Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #ifndef ROS_TIME_IMPL_H_INCLUDED
00036 #define ROS_TIME_IMPL_H_INCLUDED
00037
00038
00039
00040
00041
00042 #include <ros/platform.h>
00043 #include <iostream>
00044 #include <cmath>
00045 #include <ros/exception.h>
00046 #include <ros/time.h>
00047 #include <boost/date_time/posix_time/posix_time.hpp>
00048
00049
00050
00051
00052
00053 #ifdef WIN32
00054 #include <sys/timeb.h>
00055 #else
00056 #include <sys/time.h>
00057 #endif
00058
00059 namespace ros
00060 {
00061
00062 template<class T, class D>
00063 T& TimeBase<T, D>::fromNSec(uint64_t t)
00064 {
00065 uint64_t sec64 = 0;
00066 uint64_t nsec64 = t;
00067
00068 normalizeSecNSec(sec64, nsec64);
00069
00070 sec = (uint32_t)sec64;
00071 nsec = (uint32_t)nsec64;
00072
00073 return *static_cast<T*>(this);
00074 }
00075
00076 template<class T, class D>
00077 D TimeBase<T, D>::operator-(const T &rhs) const
00078 {
00079 return D((int32_t)sec - (int32_t)rhs.sec,
00080 (int32_t)nsec - (int32_t)rhs.nsec);
00081 }
00082
00083 template<class T, class D>
00084 T TimeBase<T, D>::operator-(const D &rhs) const
00085 {
00086 return *static_cast<const T*>(this) + ( -rhs);
00087 }
00088
00089 template<class T, class D>
00090 T TimeBase<T, D>::operator+(const D &rhs) const
00091 {
00092 int64_t sec_sum = (int64_t)sec + (int64_t)rhs.sec;
00093 int64_t nsec_sum = (int64_t)nsec + (int64_t)rhs.nsec;
00094
00095
00096 normalizeSecNSecUnsigned(sec_sum, nsec_sum);
00097
00098
00099 return T((uint32_t)sec_sum, (uint32_t)nsec_sum);
00100 }
00101
00102 template<class T, class D>
00103 T& TimeBase<T, D>::operator+=(const D &rhs)
00104 {
00105 *this = *this + rhs;
00106 return *static_cast<T*>(this);
00107 }
00108
00109 template<class T, class D>
00110 T& TimeBase<T, D>::operator-=(const D &rhs)
00111 {
00112 *this += (-rhs);
00113 return *static_cast<T*>(this);
00114 }
00115
00116 template<class T, class D>
00117 bool TimeBase<T, D>::operator==(const T &rhs) const
00118 {
00119 return sec == rhs.sec && nsec == rhs.nsec;
00120 }
00121
00122 template<class T, class D>
00123 bool TimeBase<T, D>::operator<(const T &rhs) const
00124 {
00125 if (sec < rhs.sec)
00126 return true;
00127 else if (sec == rhs.sec && nsec < rhs.nsec)
00128 return true;
00129 return false;
00130 }
00131
00132 template<class T, class D>
00133 bool TimeBase<T, D>::operator>(const T &rhs) const
00134 {
00135 if (sec > rhs.sec)
00136 return true;
00137 else if (sec == rhs.sec && nsec > rhs.nsec)
00138 return true;
00139 return false;
00140 }
00141
00142 template<class T, class D>
00143 bool TimeBase<T, D>::operator<=(const T &rhs) const
00144 {
00145 if (sec < rhs.sec)
00146 return true;
00147 else if (sec == rhs.sec && nsec <= rhs.nsec)
00148 return true;
00149 return false;
00150 }
00151
00152 template<class T, class D>
00153 bool TimeBase<T, D>::operator>=(const T &rhs) const
00154 {
00155 if (sec > rhs.sec)
00156 return true;
00157 else if (sec == rhs.sec && nsec >= rhs.nsec)
00158 return true;
00159 return false;
00160 }
00161
00162 template<class T, class D>
00163 boost::posix_time::ptime
00164 TimeBase<T, D>::toBoost() const
00165 {
00166 namespace pt = boost::posix_time;
00167 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
00168 return pt::from_time_t(sec) + pt::nanoseconds(nsec);
00169 #else
00170 return pt::from_time_t(sec) + pt::microseconds(nsec/1000.0);
00171 #endif
00172 }
00173
00174
00175 }
00176
00177 #endif // ROS_IMPL_TIME_H_INCLUDED
00178