time.h
Go to the documentation of this file.
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     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); // carry handled in ctor
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     // Throws an exception if we go out of 32-bit range
00096     normalizeSecNSecUnsigned(sec_sum, nsec_sum);
00097 
00098     // now, it's safe to downcast back to uint32 bits
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 


rostime
Author(s): Josh Faust
autogenerated on Fri Aug 28 2015 12:39:10