00001 //----------------------------------------------- 00002 // Neobotix 00003 // www.neobotix.de 00004 // Copyright (c) 2003. All rights reserved. 00005 00006 // author: 00007 //----------------------------------------------- 00008 00009 //#include "stdafx.h" 00010 00011 #include <cob_forcetorque/TimeStamp.h> 00012 00013 using namespace Neobotix; 00014 00015 //----------------------------------------------------------------------------- 00016 00017 TimeStamp::TimeStamp() 00018 { 00019 m_TimeStamp.tv_sec = 0; 00020 m_TimeStamp.tv_nsec = 0; 00021 } 00022 00023 void TimeStamp::SetNow() 00024 { 00025 ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp); 00026 } 00027 00028 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt) 00029 { 00030 return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9; 00031 } 00032 00033 ::timespec TimeStamp::DoubleToTimespec(double TimeS) 00034 { 00035 ::timespec DeltaTime; 00036 if (! ( TimeS < 4e9 && TimeS > 0.0 )) 00037 { 00038 DeltaTime.tv_sec = 0; 00039 DeltaTime.tv_nsec = 0; 00040 return DeltaTime; 00041 } 00042 00043 DeltaTime.tv_sec = ::time_t(TimeS); 00044 DeltaTime.tv_nsec 00045 = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9); 00046 00047 return DeltaTime; 00048 } 00049 00050 double TimeStamp::operator-(const TimeStamp& EarlierTime) const 00051 { 00052 ::timespec Res; 00053 00054 Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec; 00055 Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec; 00056 00057 if (Res.tv_nsec < 0) { 00058 Res.tv_sec--; 00059 Res.tv_nsec += 1000000000; 00060 } 00061 00062 return TimespecToDouble(Res); 00063 } 00064 00065 void TimeStamp::operator+=(double TimeS) 00066 { 00067 ::timespec Dbl = DoubleToTimespec(TimeS); 00068 m_TimeStamp.tv_sec += Dbl.tv_sec; 00069 m_TimeStamp.tv_nsec += Dbl.tv_nsec; 00070 if (m_TimeStamp.tv_nsec > 1000000000) 00071 { 00072 m_TimeStamp.tv_sec++; 00073 m_TimeStamp.tv_nsec -= 1000000000; 00074 } 00075 } 00076 00077 void TimeStamp::operator-=(double TimeS) 00078 { 00079 ::timespec Dbl = DoubleToTimespec(TimeS); 00080 m_TimeStamp.tv_sec -= Dbl.tv_sec; 00081 m_TimeStamp.tv_nsec -= Dbl.tv_nsec; 00082 if (m_TimeStamp.tv_nsec < 0.0) 00083 { 00084 m_TimeStamp.tv_sec--; 00085 m_TimeStamp.tv_nsec += 1000000000; 00086 } 00087 } 00088 00089 bool TimeStamp::operator>(const TimeStamp& Time) 00090 { 00091 if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true; 00092 if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) && 00093 (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true; 00094 return false; 00095 } 00096 00097 bool TimeStamp::operator<(const TimeStamp& Time) 00098 { 00099 if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true; 00100 if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) && 00101 (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true; 00102 return false; 00103 } 00104 00105 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds) 00106 { 00107 lSeconds = m_TimeStamp.tv_sec; 00108 lNanoSeconds = m_TimeStamp.tv_nsec; 00109 }; 00110 00111 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds) 00112 { 00113 m_TimeStamp.tv_sec = lSeconds; 00114 m_TimeStamp.tv_nsec = lNanoSeconds; 00115 }; 00116 /* 00117 std::string TimeStamp::CurrentToString() 00118 { 00119 # define TIME_SIZE 400 00120 00121 const struct tm *tm; 00122 size_t len; 00123 time_t now; 00124 //char * pres = new char[TIME_SIZE]; 00125 char pres[TIME_SIZE]; 00126 std::string s; 00127 00128 now = time ( NULL ); 00129 tm = localtime ( &now ); 00130 // len = strftime ( pres, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); 00131 len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm ); 00132 00133 s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000); 00134 00135 //delete pres[]; 00136 00137 return s; 00138 # undef TIME_SIZE 00139 }*/ 00140 00141 /* 00142 std::string TimeStamp::ToString() 00143 { 00144 # define TIME_SIZE 4000 00145 00146 const struct tm *tm; 00147 size_t len; 00148 time_t now; 00149 //char * pres = new char[TIME_SIZE]; 00150 char pres[TIME_SIZE]; 00151 std::string s; 00152 00153 tm = localtime ( &m_TimeStamp.tv_sec ); 00154 // len = strftime ( pres, TIME_SIZE, "%d %B %Y %I:%M:%S %p", tm ); 00155 len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm ); 00156 00157 s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000); 00158 00159 //delete pres[]; 00160 00161 return s; 00162 # undef TIME_SIZE 00163 }a*/