TimeStamp.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *   http://www.apache.org/licenses/LICENSE-2.0
00009 
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016  
00017 
00018 #include <cob_utilities/TimeStamp.h>
00019 
00020 //-----------------------------------------------------------------------------
00021 
00022 TimeStamp::TimeStamp()
00023 {
00024         m_TimeStamp.tv_sec = 0;
00025         m_TimeStamp.tv_nsec = 0;
00026 }
00027 
00028 void TimeStamp::SetNow()
00029 {
00030         ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
00031 }
00032 
00033 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
00034 {
00035         return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
00036 }
00037 
00038 ::timespec TimeStamp::DoubleToTimespec(double TimeS)
00039 {
00040         ::timespec DeltaTime;
00041         if (! ( TimeS < 4e9 && TimeS > 0.0 ))
00042         {
00043                 DeltaTime.tv_sec = 0;
00044                 DeltaTime.tv_nsec = 0;
00045                 return DeltaTime;
00046         }
00047 
00048         DeltaTime.tv_sec = ::time_t(TimeS);
00049         DeltaTime.tv_nsec
00050                 = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
00051 
00052         return DeltaTime;
00053 }
00054 
00055 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
00056 {
00057         ::timespec Res;
00058 
00059         Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
00060         Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
00061 
00062         if (Res.tv_nsec < 0) {
00063                 Res.tv_sec--;
00064                 Res.tv_nsec += 1000000000;
00065         }
00066 
00067         return TimespecToDouble(Res);
00068 }
00069 
00070 void TimeStamp::operator+=(double TimeS)
00071 {
00072         ::timespec Dbl = DoubleToTimespec(TimeS);
00073         m_TimeStamp.tv_sec += Dbl.tv_sec;
00074         m_TimeStamp.tv_nsec += Dbl.tv_nsec;
00075         if (m_TimeStamp.tv_nsec > 1000000000)
00076         {
00077                 m_TimeStamp.tv_sec++;
00078                 m_TimeStamp.tv_nsec -= 1000000000;
00079         }
00080 }
00081 
00082 void TimeStamp::operator-=(double TimeS)
00083 {
00084         ::timespec Dbl = DoubleToTimespec(TimeS);
00085         m_TimeStamp.tv_sec -= Dbl.tv_sec;
00086         m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
00087         if (m_TimeStamp.tv_nsec < 0.0)
00088         {
00089                 m_TimeStamp.tv_sec--;
00090                 m_TimeStamp.tv_nsec += 1000000000;
00091         }
00092 }
00093 
00094 bool TimeStamp::operator>(const TimeStamp& Time)
00095 {
00096         if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true;
00097         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00098                 (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true;
00099         return false;
00100 }
00101 
00102 bool TimeStamp::operator<(const TimeStamp& Time)
00103 {
00104         if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true;
00105         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00106                 (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true;
00107         return false;
00108 }
00109 
00110 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
00111 {
00112         lSeconds = m_TimeStamp.tv_sec;
00113         lNanoSeconds = m_TimeStamp.tv_nsec;
00114 };
00115 
00116 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
00117 {
00118         m_TimeStamp.tv_sec = lSeconds;
00119         m_TimeStamp.tv_nsec = lNanoSeconds;
00120 };
00121 
00122 std::string TimeStamp::CurrentToString()
00123 {
00124 # define TIME_SIZE 400
00125 
00126         const struct tm *tm;
00127         size_t len;
00128         time_t now;
00129         char pres[TIME_SIZE];
00130         std::string s;
00131 
00132         now = time ( NULL );
00133         tm = localtime ( &now );
00134         len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm );
00135 
00136         s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000);
00137 
00138         return s;
00139 # undef TIME_SIZE
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[TIME_SIZE];
00150         std::string s;
00151 
00152         tm = localtime ( &m_TimeStamp.tv_sec );
00153         len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm );
00154 
00155         s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000);
00156 
00157         return s;
00158 # undef TIME_SIZE
00159 }


cob_utilities
Author(s): Christian Connette
autogenerated on Sat Jun 8 2019 21:02:25