Timestamp.cpp
Go to the documentation of this file.
1 /*
2  * File: Timestamp.cpp
3  * Author: Dorian Galvez-Lopez
4  * Date: March 2009
5  * Description: timestamping functions
6  *
7  * Note: in windows, this class has a 1ms resolution
8  *
9  * License: see the LICENSE.txt file
10  *
11  */
12 
13 #include <cstdio>
14 #include <cstdlib>
15 #include <ctime>
16 #include <cmath>
17 #include <sstream>
18 #include <iomanip>
19 
20 #ifdef _WIN32
21 #ifndef WIN32
22 #define WIN32
23 #endif
24 #endif
25 
26 #ifdef WIN32
27 #include <sys/timeb.h>
28 #define sprintf sprintf_s
29 #else
30 #include <sys/time.h>
31 #endif
32 
33 #include "Timestamp.h"
34 
35 using namespace std;
36 
37 using namespace DUtils;
38 
39 Timestamp::Timestamp(Timestamp::tOptions option)
40 {
41  if(option & CURRENT_TIME)
42  setToCurrentTime();
43  else if(option & ZERO)
44  setTime(0.);
45 }
46 
47 Timestamp::~Timestamp(void)
48 {
49 }
50 
51 bool Timestamp::empty() const
52 {
53  return m_secs == 0 && m_usecs == 0;
54 }
55 
56 void Timestamp::setToCurrentTime(){
57 
58 #ifdef WIN32
59  struct __timeb32 timebuffer;
60  _ftime32_s( &timebuffer ); // C4996
61  // Note: _ftime is deprecated; consider using _ftime_s instead
62  m_secs = timebuffer.time;
63  m_usecs = timebuffer.millitm * 1000;
64 #else
65  struct timeval now;
66  gettimeofday(&now, NULL);
67  m_secs = now.tv_sec;
68  m_usecs = now.tv_usec;
69 #endif
70 
71 }
72 
73 void Timestamp::setTime(const string &stime){
74  string::size_type p = stime.find('.');
75  if(p == string::npos){
76  m_secs = atol(stime.c_str());
77  m_usecs = 0;
78  }else{
79  m_secs = atol(stime.substr(0, p).c_str());
80 
81  string s_usecs = stime.substr(p+1, 6);
82  m_usecs = atol(stime.substr(p+1).c_str());
83  m_usecs *= (unsigned long)pow(10.0, double(6 - s_usecs.length()));
84  }
85 }
86 
87 void Timestamp::setTime(double s)
88 {
89  m_secs = (unsigned long)s;
90  m_usecs = (s - (double)m_secs) * 1e6;
91 }
92 
93 double Timestamp::getFloatTime() const {
94  return double(m_secs) + double(m_usecs)/1000000.0;
95 }
96 
97 string Timestamp::getStringTime() const {
98  char buf[32];
99  sprintf(buf, "%.6lf", this->getFloatTime());
100  return string(buf);
101 }
102 
103 double Timestamp::operator- (const Timestamp &t) const {
104  return this->getFloatTime() - t.getFloatTime();
105 }
106 
107 Timestamp& Timestamp::operator+= (double s)
108 {
109  *this = *this + s;
110  return *this;
111 }
112 
113 Timestamp& Timestamp::operator-= (double s)
114 {
115  *this = *this - s;
116  return *this;
117 }
118 
119 Timestamp Timestamp::operator+ (double s) const
120 {
121  unsigned long secs = (long)floor(s);
122  unsigned long usecs = (long)((s - (double)secs) * 1e6);
123 
124  return this->plus(secs, usecs);
125 }
126 
127 Timestamp Timestamp::plus(unsigned long secs, unsigned long usecs) const
128 {
129  Timestamp t;
130 
131  const unsigned long max = 1000000ul;
132 
133  if(m_usecs + usecs >= max)
134  t.setTime(m_secs + secs + 1, m_usecs + usecs - max);
135  else
136  t.setTime(m_secs + secs, m_usecs + usecs);
137 
138  return t;
139 }
140 
141 Timestamp Timestamp::operator- (double s) const
142 {
143  unsigned long secs = (long)floor(s);
144  unsigned long usecs = (long)((s - (double)secs) * 1e6);
145 
146  return this->minus(secs, usecs);
147 }
148 
149 Timestamp Timestamp::minus(unsigned long secs, unsigned long usecs) const
150 {
151  Timestamp t;
152 
153  const unsigned long max = 1000000ul;
154 
155  if(m_usecs < usecs)
156  t.setTime(m_secs - secs - 1, max - (usecs - m_usecs));
157  else
158  t.setTime(m_secs - secs, m_usecs - usecs);
159 
160  return t;
161 }
162 
163 bool Timestamp::operator> (const Timestamp &t) const
164 {
165  if(m_secs > t.m_secs) return true;
166  else if(m_secs == t.m_secs) return m_usecs > t.m_usecs;
167  else return false;
168 }
169 
170 bool Timestamp::operator>= (const Timestamp &t) const
171 {
172  if(m_secs > t.m_secs) return true;
173  else if(m_secs == t.m_secs) return m_usecs >= t.m_usecs;
174  else return false;
175 }
176 
177 bool Timestamp::operator< (const Timestamp &t) const
178 {
179  if(m_secs < t.m_secs) return true;
180  else if(m_secs == t.m_secs) return m_usecs < t.m_usecs;
181  else return false;
182 }
183 
184 bool Timestamp::operator<= (const Timestamp &t) const
185 {
186  if(m_secs < t.m_secs) return true;
187  else if(m_secs == t.m_secs) return m_usecs <= t.m_usecs;
188  else return false;
189 }
190 
191 bool Timestamp::operator== (const Timestamp &t) const
192 {
193  return(m_secs == t.m_secs && m_usecs == t.m_usecs);
194 }
195 
196 
197 string Timestamp::Format(bool machine_friendly) const
198 {
199  struct tm tm_time;
200 
201  time_t t = (time_t)getFloatTime();
202 
203 #ifdef WIN32
204  localtime_s(&tm_time, &t);
205 #else
206  localtime_r(&t, &tm_time);
207 #endif
208 
209  char buffer[128];
210 
211  if(machine_friendly)
212  {
213  strftime(buffer, 128, "%Y%m%d_%H%M%S", &tm_time);
214  }
215  else
216  {
217  strftime(buffer, 128, "%c", &tm_time); // Thu Aug 23 14:55:02 2001
218  }
219 
220  return string(buffer);
221 }
222 
223 string Timestamp::Format(double s) {
224  int days = int(s / (24. * 3600.0));
225  s -= days * (24. * 3600.0);
226  int hours = int(s / 3600.0);
227  s -= hours * 3600;
228  int minutes = int(s / 60.0);
229  s -= minutes * 60;
230  int seconds = int(s);
231  int ms = int((s - seconds)*1e6);
232 
233  stringstream ss;
234  ss.fill('0');
235  bool b;
236  if((b = (days > 0))) ss << days << "d ";
237  if((b = (b || hours > 0))) ss << setw(2) << hours << ":";
238  if((b = (b || minutes > 0))) ss << setw(2) << minutes << ":";
239  if(b) ss << setw(2);
240  ss << seconds;
241  if(!b) ss << "." << setw(6) << ms;
242 
243  return ss.str();
244 }
245 
246 
double getFloatTime() const
Definition: Timestamp.cpp:93
Definition: Random.h:18
tOptions
Options to initiate a timestamp.
Definition: Timestamp.h:24
Timestamp.
Definition: Timestamp.h:19
void setTime(unsigned long secs, unsigned long usecs)
Definition: Timestamp.h:61
bool operator<(const HyperDijkstra::AdjacencyMapEntry &a, const HyperDijkstra::AdjacencyMapEntry &b)
unsigned long m_secs
Seconds.
Definition: Timestamp.h:196
unsigned long m_usecs
Microseconds.
Definition: Timestamp.h:198


orb_slam2_with_maps_odom
Author(s): teng zhang
autogenerated on Fri Sep 25 2020 03:24:47