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 
19 
20 //-----------------------------------------------------------------------------
21 
23 {
24  m_TimeStamp.tv_sec = 0;
25  m_TimeStamp.tv_nsec = 0;
26 }
27 
29 {
30  ::clock_gettime(CLOCK_REALTIME, &m_TimeStamp);
31 }
32 
33 double TimeStamp::TimespecToDouble(const ::timespec& LargeInt)
34 {
35  return double(LargeInt.tv_sec) + double(LargeInt.tv_nsec) / 1e9;
36 }
37 
38 ::timespec TimeStamp::DoubleToTimespec(double TimeS)
39 {
40  ::timespec DeltaTime;
41  if (! ( TimeS < 4e9 && TimeS > 0.0 ))
42  {
43  DeltaTime.tv_sec = 0;
44  DeltaTime.tv_nsec = 0;
45  return DeltaTime;
46  }
47 
48  DeltaTime.tv_sec = ::time_t(TimeS);
49  DeltaTime.tv_nsec
50  = static_cast<long int>((TimeS - double(DeltaTime.tv_sec)) * 1e9);
51 
52  return DeltaTime;
53 }
54 
55 double TimeStamp::operator-(const TimeStamp& EarlierTime) const
56 {
57  ::timespec Res;
58 
59  Res.tv_sec = m_TimeStamp.tv_sec - EarlierTime.m_TimeStamp.tv_sec;
60  Res.tv_nsec = m_TimeStamp.tv_nsec - EarlierTime.m_TimeStamp.tv_nsec;
61 
62  if (Res.tv_nsec < 0) {
63  Res.tv_sec--;
64  Res.tv_nsec += 1000000000;
65  }
66 
67  return TimespecToDouble(Res);
68 }
69 
70 void TimeStamp::operator+=(double TimeS)
71 {
72  ::timespec Dbl = DoubleToTimespec(TimeS);
73  m_TimeStamp.tv_sec += Dbl.tv_sec;
74  m_TimeStamp.tv_nsec += Dbl.tv_nsec;
75  if (m_TimeStamp.tv_nsec > 1000000000)
76  {
77  m_TimeStamp.tv_sec++;
78  m_TimeStamp.tv_nsec -= 1000000000;
79  }
80 }
81 
82 void TimeStamp::operator-=(double TimeS)
83 {
84  ::timespec Dbl = DoubleToTimespec(TimeS);
85  m_TimeStamp.tv_sec -= Dbl.tv_sec;
86  m_TimeStamp.tv_nsec -= Dbl.tv_nsec;
87  if (m_TimeStamp.tv_nsec < 0.0)
88  {
89  m_TimeStamp.tv_sec--;
90  m_TimeStamp.tv_nsec += 1000000000;
91  }
92 }
93 
95 {
96  if (m_TimeStamp.tv_sec > Time.m_TimeStamp.tv_sec) return true;
97  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
98  (m_TimeStamp.tv_nsec > Time.m_TimeStamp.tv_nsec)) return true;
99  return false;
100 }
101 
103 {
104  if (m_TimeStamp.tv_sec < Time.m_TimeStamp.tv_sec) return true;
105  if ((m_TimeStamp.tv_sec == Time.m_TimeStamp.tv_sec) &&
106  (m_TimeStamp.tv_nsec < Time.m_TimeStamp.tv_nsec)) return true;
107  return false;
108 }
109 
110 void TimeStamp::getTimeStamp(long& lSeconds, long& lNanoSeconds)
111 {
112  lSeconds = m_TimeStamp.tv_sec;
113  lNanoSeconds = m_TimeStamp.tv_nsec;
114 };
115 
116 void TimeStamp::setTimeStamp(const long& lSeconds, const long& lNanoSeconds)
117 {
118  m_TimeStamp.tv_sec = lSeconds;
119  m_TimeStamp.tv_nsec = lNanoSeconds;
120 };
121 
123 {
124 # define TIME_SIZE 400
125 
126  const struct tm *tm;
127  size_t len;
128  time_t now;
129  char pres[TIME_SIZE];
130  std::string s;
131 
132  now = time ( NULL );
133  tm = localtime ( &now );
134  len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm );
135 
136  s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000);
137 
138  return s;
139 # undef TIME_SIZE
140 }
141 
142 std::string TimeStamp::ToString()
143 {
144 # define TIME_SIZE 4000
145 
146  const struct tm *tm;
147  size_t len;
148  //time_t now;
149  char pres[TIME_SIZE];
150  std::string s;
151 
152  tm = localtime ( &m_TimeStamp.tv_sec );
153  len = strftime ( pres, TIME_SIZE, "%Y-%m-%d %H:%M:%S.", tm );
154 
155  s = (std::string)pres + NumToString(m_TimeStamp.tv_nsec / 1000);
156 
157  return s;
158 # undef TIME_SIZE
159 }
void setTimeStamp(const long &lSeconds, const long &lNanoSeconds)
Definition: TimeStamp.cpp:116
timespec m_TimeStamp
Internal time stamp data.
Definition: TimeStamp.h:83
void operator-=(double TimeS)
Reduces the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:82
void getTimeStamp(long &lSeconds, long &lNanoSeconds)
Definition: TimeStamp.cpp:110
void SetNow()
Makes time measurement.
Definition: TimeStamp.cpp:28
TimeStamp()
Constructor.
Definition: TimeStamp.cpp:22
void operator+=(double TimeS)
Increase the timestamp by TimeS seconds.
Definition: TimeStamp.cpp:70
bool operator>(const TimeStamp &Time)
Checks if this time is after time "Time".
Definition: TimeStamp.cpp:94
bool operator<(const TimeStamp &Time)
Checks if this time is before time "Time".
Definition: TimeStamp.cpp:102
std::string CurrentToString()
Definition: TimeStamp.cpp:122
std::string NumToString(const int n)
Definition: StrUtil.cpp:37
double operator-(const TimeStamp &EarlierTime) const
Retrieves time difference in seconds.
Definition: TimeStamp.cpp:55
static double TimespecToDouble(const ::timespec &LargeInt)
Conversion timespec -> double.
Definition: TimeStamp.cpp:33
std::string ToString()
Definition: TimeStamp.cpp:142
::timespec DoubleToTimespec(double TimeS)
Conversion double -> timespec.
Definition: TimeStamp.cpp:38
#define TIME_SIZE


cob_utilities
Author(s): Christian Connette
autogenerated on Wed Apr 7 2021 02:11:52