$search
00001 /********************************************************************* 00002 * Software License Agreement (BSD License) 00003 * 00004 * Copyright (c) 2008, Willow Garage, Inc. 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 00011 * * Redistributions of source code must retain the above copyright 00012 * notice, this list of conditions and the following disclaimer. 00013 * * Redistributions in binary form must reproduce the above 00014 * copyright notice, this list of conditions and the following 00015 * disclaimer in the documentation and/or other materials provided 00016 * with the distribution. 00017 * * Neither the name of Willow Garage, Inc. nor the names of its 00018 * contributors may be used to endorse or promote products derived 00019 * from this software without specific prior written permission. 00020 * 00021 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 * POSSIBILITY OF SUCH DAMAGE. 00033 *********************************************************************/ 00034 00035 #ifndef ROS_TIME_IMPL_H_INCLUDED 00036 #define ROS_TIME_IMPL_H_INCLUDED 00037 00038 /********************************************************************* 00039 ** Headers 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 ** Cross Platform Headers 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 sec = (int32_t)(t / 1000000000); 00066 nsec = (int32_t)(t % 1000000000); 00067 00068 normalizeSecNSec(sec, nsec); 00069 00070 return *static_cast<T*>(this); 00071 } 00072 00073 template<class T, class D> 00074 D TimeBase<T, D>::operator-(const T &rhs) const 00075 { 00076 return D((int32_t)sec - (int32_t)rhs.sec, 00077 (int32_t)nsec - (int32_t)rhs.nsec); // carry handled in ctor 00078 } 00079 00080 template<class T, class D> 00081 T TimeBase<T, D>::operator-(const D &rhs) const 00082 { 00083 return *static_cast<const T*>(this) + ( -rhs); 00084 } 00085 00086 template<class T, class D> 00087 T TimeBase<T, D>::operator+(const D &rhs) const 00088 { 00089 int64_t sec_sum = (int64_t)sec + (int64_t)rhs.sec; 00090 int64_t nsec_sum = (int64_t)nsec + (int64_t)rhs.nsec; 00091 00092 // Throws an exception if we go out of 32-bit range 00093 normalizeSecNSecUnsigned(sec_sum, nsec_sum); 00094 00095 // now, it's safe to downcast back to uint32 bits 00096 return T((uint32_t)sec_sum, (uint32_t)nsec_sum); 00097 } 00098 00099 template<class T, class D> 00100 T& TimeBase<T, D>::operator+=(const D &rhs) 00101 { 00102 *this = *this + rhs; 00103 return *static_cast<T*>(this); 00104 } 00105 00106 template<class T, class D> 00107 T& TimeBase<T, D>::operator-=(const D &rhs) 00108 { 00109 *this += (-rhs); 00110 return *static_cast<T*>(this); 00111 } 00112 00113 template<class T, class D> 00114 bool TimeBase<T, D>::operator==(const T &rhs) const 00115 { 00116 return sec == rhs.sec && nsec == rhs.nsec; 00117 } 00118 00119 template<class T, class D> 00120 bool TimeBase<T, D>::operator<(const T &rhs) const 00121 { 00122 if (sec < rhs.sec) 00123 return true; 00124 else if (sec == rhs.sec && nsec < rhs.nsec) 00125 return true; 00126 return false; 00127 } 00128 00129 template<class T, class D> 00130 bool TimeBase<T, D>::operator>(const T &rhs) const 00131 { 00132 if (sec > rhs.sec) 00133 return true; 00134 else if (sec == rhs.sec && nsec > rhs.nsec) 00135 return true; 00136 return false; 00137 } 00138 00139 template<class T, class D> 00140 bool TimeBase<T, D>::operator<=(const T &rhs) const 00141 { 00142 if (sec < rhs.sec) 00143 return true; 00144 else if (sec == rhs.sec && nsec <= rhs.nsec) 00145 return true; 00146 return false; 00147 } 00148 00149 template<class T, class D> 00150 bool TimeBase<T, D>::operator>=(const T &rhs) const 00151 { 00152 if (sec > rhs.sec) 00153 return true; 00154 else if (sec == rhs.sec && nsec >= rhs.nsec) 00155 return true; 00156 return false; 00157 } 00158 00159 template<class T, class D> 00160 boost::posix_time::ptime 00161 TimeBase<T, D>::toBoost() const 00162 { 00163 namespace pt = boost::posix_time; 00164 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS) 00165 return pt::from_time_t(sec) + pt::nanoseconds(nsec); 00166 #else 00167 return pt::from_time_t(sec) + pt::microseconds(nsec/1000.0); 00168 #endif 00169 } 00170 00171 00172 } 00173 00174 #endif // ROS_IMPL_TIME_H_INCLUDED 00175