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_trajectory_controller/TimeStamp.h>
00019 
00020 //-----------------------------------------------------------------------------
00021 TimeStamp::TimeStamp()
00022 {
00023         m_TimeStamp.tv_sec = 0;
00024         m_TimeStamp.tv_nsec = 0;
00025 }
00026 
00027 void TimeStamp::SetNow()
00028 {
00029         ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
00030 }
00031 
00032 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
00033 {
00034         return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
00035 }
00036 
00037 ::timespec TimeStamp::DoubleToTimespec(double TimeS)
00038 {
00039         ::timespec DeltaTime;
00040         if (! ( TimeS < 4e9 && TimeS > 0.0 ))
00041         {
00042                 DeltaTime.tv_sec = 0;
00043                 DeltaTime.tv_nsec = 0;
00044                 return DeltaTime;
00045         }
00046 
00047         DeltaTime.tv_sec = ::time_t(TimeS);
00048         DeltaTime.tv_nsec
00049                 = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
00050 
00051         return DeltaTime;
00052 }
00053 
00054 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
00055 {
00056         ::timespec Res;
00057 
00058         Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
00059         Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
00060 
00061         if (Res.tv_nsec < 0) {
00062                 Res.tv_sec--;
00063                 Res.tv_nsec += 1000000000;
00064         }
00065 
00066         return TimespecToDouble(Res);
00067 }
00068 
00069 void TimeStamp::operator+=(double TimeS)
00070 {
00071         ::timespec Dbl = DoubleToTimespec(TimeS);
00072         m_TimeStamp.tv_sec += Dbl.tv_sec;
00073         m_TimeStamp.tv_nsec += Dbl.tv_nsec;
00074         if (m_TimeStamp.tv_nsec > 1000000000)
00075         {
00076                 m_TimeStamp.tv_sec++;
00077                 m_TimeStamp.tv_nsec -= 1000000000;
00078         }
00079 }
00080 
00081 void TimeStamp::operator-=(double TimeS)
00082 {
00083         ::timespec Dbl = DoubleToTimespec(TimeS);
00084         m_TimeStamp.tv_sec -= Dbl.tv_sec;
00085         m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
00086         if (m_TimeStamp.tv_nsec < 0.0)
00087         {
00088                 m_TimeStamp.tv_sec--;
00089                 m_TimeStamp.tv_nsec += 1000000000;
00090         }
00091 }
00092 
00093 bool TimeStamp::operator>(const TimeStamp& Time)
00094 {
00095         if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true;
00096         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00097                 (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true;
00098         return false;
00099 }
00100 
00101 bool TimeStamp::operator<(const TimeStamp& Time)
00102 {
00103         if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true;
00104         if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
00105                 (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true;
00106         return false;
00107 }
00108 
00109 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
00110 {
00111         lSeconds = m_TimeStamp.tv_sec;
00112         lNanoSeconds = m_TimeStamp.tv_nsec;
00113 };
00114 
00115 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
00116 {
00117         m_TimeStamp.tv_sec = lSeconds;
00118         m_TimeStamp.tv_nsec = lNanoSeconds;
00119 };


cob_trajectory_controller
Author(s): Alexander Bubeck
autogenerated on Thu Jun 6 2019 21:19:22