TimeStamp.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifdef __LINUX__
20 #else
21 #include <windows.h>
23 
24 namespace RTB
25 {
26 #endif
27 
28 //-----------------------------------------------------------------------------
29 #ifdef __LINUX__
31 {
32  m_TimeStamp.tv_sec = 0;
33  m_TimeStamp.tv_nsec = 0;
34 }
35 
36 void TimeStamp::SetNow()
37 {
38  ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
39 }
40 
41 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
42 {
43  return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
44 }
45 
46 ::timespec TimeStamp::DoubleToTimespec(double TimeS)
47 {
48  ::timespec DeltaTime;
49  if (!(TimeS < 4e9 && TimeS > 0.0))
50  {
51  DeltaTime.tv_sec = 0;
52  DeltaTime.tv_nsec = 0;
53  return DeltaTime;
54  }
55 
56  DeltaTime.tv_sec = ::time_t(TimeS);
57  DeltaTime.tv_nsec = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
58 
59  return DeltaTime;
60 }
61 
62 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
63 {
64  ::timespec Res;
65 
66  Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
67  Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
68 
69  if (Res.tv_nsec < 0)
70  {
71  Res.tv_sec--;
72  Res.tv_nsec += 1000000000;
73  }
74 
75  return TimespecToDouble(Res);
76 }
77 
78 void TimeStamp::operator+=(double TimeS)
79 {
80  ::timespec Dbl = DoubleToTimespec(TimeS);
81  m_TimeStamp.tv_sec += Dbl.tv_sec;
82  m_TimeStamp.tv_nsec += Dbl.tv_nsec;
83  if (m_TimeStamp.tv_nsec > 1000000000)
84  {
85  m_TimeStamp.tv_sec++;
86  m_TimeStamp.tv_nsec -= 1000000000;
87  }
88 }
89 
90 void TimeStamp::operator-=(double TimeS)
91 {
92  ::timespec Dbl = DoubleToTimespec(TimeS);
93  m_TimeStamp.tv_sec -= Dbl.tv_sec;
94  m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
95  if (m_TimeStamp.tv_nsec < 0.0)
96  {
97  m_TimeStamp.tv_sec--;
98  m_TimeStamp.tv_nsec += 1000000000;
99  }
100 }
101 
102 bool TimeStamp::operator>(const TimeStamp& Time)
103 {
104  if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec)
105  return true;
106  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) && (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec))
107  return true;
108  return false;
109 }
110 
111 bool TimeStamp::operator<(const TimeStamp& Time)
112 {
113  if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec)
114  return true;
115  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) && (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec))
116  return true;
117  return false;
118 }
119 
120 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
121 {
122  lSeconds = m_TimeStamp.tv_sec;
123  lNanoSeconds = m_TimeStamp.tv_nsec;
124 };
125 
126 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
127 {
128  m_TimeStamp.tv_sec = lSeconds;
129  m_TimeStamp.tv_nsec = lNanoSeconds;
130 };
131 
132 #else
133 //-------------------------------------------------------------------
134 
135 LARGE_INTEGER ClockFrequency;
136 BOOL CounterHW = QueryPerformanceFrequency(&ClockFrequency);
137 double TimeStamp::m_SecPerCount = 1.0 / TimeStamp::LargeIntToDouble(ClockFrequency);
138 double TimeStamp::m_DwordSize = double(0xFFFFFFFF);
139 
140 //-----------------------------------------------------------------------------
141 
143 {
144  m_TimeStamp.HighPart = 0;
145  m_TimeStamp.LowPart = 0;
146 }
147 
149 {
150  QueryPerformanceCounter(&m_TimeStamp);
151 }
152 
153 double TimeStamp::LargeIntToDouble(const LARGE_INTEGER& LargeInt)
154 {
155  return LargeInt.HighPart * m_DwordSize + LargeInt.LowPart;
156 }
157 
158 LARGE_INTEGER TimeStamp::DoubleToLargeInt(double TimeS)
159 {
160  LARGE_INTEGER DeltaTime;
161  DeltaTime.LowPart = 0;
162  DeltaTime.HighPart = 0;
163  if (!(TimeS < 4e9 && TimeS > 0.0))
164  return DeltaTime;
165  DeltaTime.LowPart = (unsigned long)(TimeS / m_SecPerCount);
166  return DeltaTime;
167 }
168 
169 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
170 {
171  LARGE_INTEGER Res;
172  Res.HighPart = m_TimeStamp.HighPart - EarlierTime.m_TimeStamp.HighPart;
173  Res.LowPart = m_TimeStamp.LowPart - EarlierTime.m_TimeStamp.LowPart;
174  if (EarlierTime.m_TimeStamp.LowPart > m_TimeStamp.LowPart)
175  Res.HighPart--;
176  return LargeIntToDouble(Res) * m_SecPerCount;
177 }
178 
179 void TimeStamp::operator+=(double TimeS)
180 {
181  LARGE_INTEGER& Dbl = DoubleToLargeInt(TimeS);
182  LARGE_INTEGER Res;
183  Res.LowPart = m_TimeStamp.LowPart + Dbl.LowPart;
184  if ((Res.LowPart < m_TimeStamp.LowPart) || (Res.LowPart < Dbl.LowPart))
185  m_TimeStamp.HighPart++;
186  m_TimeStamp.LowPart = Res.LowPart;
187 }
188 
189 void TimeStamp::operator-=(double TimeS)
190 {
191  LARGE_INTEGER& Dbl = DoubleToLargeInt(TimeS);
192  LARGE_INTEGER Res;
193  Res.LowPart = m_TimeStamp.LowPart - Dbl.LowPart;
194  Res.HighPart = m_TimeStamp.HighPart - Dbl.HighPart;
195  if ((Res.LowPart > m_TimeStamp.LowPart))
196  Res.HighPart--;
197  m_TimeStamp.LowPart = Res.LowPart;
198  m_TimeStamp.HighPart = Res.HighPart;
199 }
200 
202 {
203  if (m_TimeStamp.HighPart > Time.m_TimeStamp.HighPart)
204  return true;
205  if ((m_TimeStamp.HighPart == Time.m_TimeStamp.HighPart) && (m_TimeStamp.LowPart > Time.m_TimeStamp.LowPart))
206  return true;
207  return false;
208 }
209 
211 {
212  if (m_TimeStamp.HighPart < Time.m_TimeStamp.HighPart)
213  return true;
214  if ((m_TimeStamp.HighPart == Time.m_TimeStamp.HighPart) && (m_TimeStamp.LowPart < Time.m_TimeStamp.LowPart))
215  return true;
216  return false;
217 }
218 }
219 #endif
static double m_SecPerCount
Seconds per counts.
Definition: TimeStamp.h:108
void setTimeStamp(const long &lSeconds, const long &lNanoSeconds)
void operator+=(double TimeS)
Increase the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:179
static double LargeIntToDouble(const LARGE_INTEGER &LargeInt)
Conversion timespec -> double.
Definition: TimeStamp.cpp:153
double operator-(const TimeStamp &EarlierTime) const
Retrieves time difference in seconds.
Definition: TimeStamp.cpp:169
LARGE_INTEGER m_TimeStamp
Internal time stamp data.
Definition: TimeStamp.h:88
LARGE_INTEGER ClockFrequency
Definition: TimeStamp.cpp:135
bool operator>(const TimeStamp &Time)
Checks if this time is after time "Time".
Definition: TimeStamp.cpp:201
BOOL CounterHW
Definition: TimeStamp.cpp:136
TimeStamp()
Constructor.
Definition: TimeStamp.cpp:142
bool operator<(const TimeStamp &Time)
Checks if this time is before time "Time".
Definition: TimeStamp.cpp:210
static double m_DwordSize
Size of a Dword field.
Definition: TimeStamp.h:105
void SetNow()
Makes time measurement.
Definition: TimeStamp.cpp:148
void operator-=(double TimeS)
Reduces the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:189
void getTimeStamp(long &lSeconds, long &lNanoSeconds)
Definition: TimeStamp.h:30
static LARGE_INTEGER DoubleToLargeInt(double TimeS)
Conversion double -> LARGE_INTEGER.
Definition: TimeStamp.cpp:158


schunk_powercube_chain
Author(s): Florian Weisshardt
autogenerated on Mon Nov 25 2019 03:48:21