BitmapToolkit Scol plugin
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
35using namespace std;
36
37using namespace DUtils;
38
40{
41 if (option & CURRENT_TIME)
43 else if (option & ZERO)
44 setTime(0.);
45}
46
50
51bool Timestamp::empty() const
52{
53 return m_secs == 0 && m_usecs == 0;
54}
55
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
73void 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 }
79 else {
80 m_secs = atol(stime.substr(0, p).c_str());
81
82 string s_usecs = stime.substr(p + 1, 6);
83 m_usecs = atol(stime.substr(p + 1).c_str());
84 m_usecs *= (unsigned long)pow(10.0, double(6 - s_usecs.length()));
85 }
86}
87
88void Timestamp::setTime(double s)
89{
90 m_secs = (unsigned long)s;
91 m_usecs = (s - (double)m_secs) * 1e6;
92}
93
95 return double(m_secs) + double(m_usecs) / 1000000.0;
96}
97
99 char buf[32];
100 sprintf(buf, "%.6lf", this->getFloatTime());
101 return string(buf);
102}
103
104double Timestamp::operator- (const Timestamp &t) const {
105 return this->getFloatTime() - t.getFloatTime();
106}
107
109{
110 *this = *this + s;
111 return *this;
112}
113
115{
116 *this = *this - s;
117 return *this;
118}
119
121{
122 unsigned long secs = (long)floor(s);
123 unsigned long usecs = (long)((s - (double)secs) * 1e6);
124
125 return this->plus(secs, usecs);
126}
127
128Timestamp Timestamp::plus(unsigned long secs, unsigned long usecs) const
129{
130 Timestamp t;
131
132 const unsigned long max = 1000000ul;
133
134 if (m_usecs + usecs >= max)
135 t.setTime(m_secs + secs + 1, m_usecs + usecs - max);
136 else
137 t.setTime(m_secs + secs, m_usecs + usecs);
138
139 return t;
140}
141
143{
144 unsigned long secs = (long)floor(s);
145 unsigned long usecs = (long)((s - (double)secs) * 1e6);
146
147 return this->minus(secs, usecs);
148}
149
150Timestamp Timestamp::minus(unsigned long secs, unsigned long usecs) const
151{
152 Timestamp t;
153
154 const unsigned long max = 1000000ul;
155
156 if (m_usecs < usecs)
157 t.setTime(m_secs - secs - 1, max - (usecs - m_usecs));
158 else
159 t.setTime(m_secs - secs, m_usecs - usecs);
160
161 return t;
162}
163
165{
166 if (m_secs > t.m_secs) return true;
167 else if (m_secs == t.m_secs) return m_usecs > t.m_usecs;
168 else return false;
169}
170
172{
173 if (m_secs > t.m_secs) return true;
174 else if (m_secs == t.m_secs) return m_usecs >= t.m_usecs;
175 else return false;
176}
177
179{
180 if (m_secs < t.m_secs) return true;
181 else if (m_secs == t.m_secs) return m_usecs < t.m_usecs;
182 else return false;
183}
184
186{
187 if (m_secs < t.m_secs) return true;
188 else if (m_secs == t.m_secs) return m_usecs <= t.m_usecs;
189 else return false;
190}
191
193{
194 return(m_secs == t.m_secs && m_usecs == t.m_usecs);
195}
196
197
198string Timestamp::Format(bool machine_friendly) const
199{
200 struct tm tm_time;
201
202 time_t t = (time_t)getFloatTime();
203
204#ifdef WIN32
205 localtime_s(&tm_time, &t);
206#else
207 localtime_r(&t, &tm_time);
208#endif
209
210 char buffer[128];
211
212 if (machine_friendly)
213 {
214 strftime(buffer, 128, "%Y%m%d_%H%M%S", &tm_time);
215 }
216 else
217 {
218 strftime(buffer, 128, "%c", &tm_time); // Thu Aug 23 14:55:02 2001
219 }
220
221 return string(buffer);
222}
223
224string Timestamp::Format(double s) {
225 int days = int(s / (24. * 3600.0));
226 s -= days * (24. * 3600.0);
227 int hours = int(s / 3600.0);
228 s -= hours * 3600;
229 int minutes = int(s / 60.0);
230 s -= minutes * 60;
231 int seconds = int(s);
232 int ms = int((s - seconds)*1e6);
233
234 stringstream ss;
235 ss.fill('0');
236 bool b;
237 if ((b = (days > 0))) ss << days << "d ";
238 if ((b = (b || hours > 0))) ss << setw(2) << hours << ":";
239 if ((b = (b || minutes > 0))) ss << setw(2) << minutes << ":";
240 if (b) ss << setw(2);
241 ss << seconds;
242 if (!b) ss << "." << setw(6) << ms;
243
244 return ss.str();
245}
246
247
Timestamp.
Definition Timestamp.h:20
virtual ~Timestamp(void)
Definition Timestamp.cpp:47
bool empty() const
Definition Timestamp.cpp:51
Timestamp operator+(double s) const
double getFloatTime() const
Definition Timestamp.cpp:94
bool operator==(const Timestamp &t) const
void setTime(unsigned long secs, unsigned long usecs)
Definition Timestamp.h:61
bool operator<=(const Timestamp &t) const
Timestamp minus(unsigned long s, unsigned long us) const
tOptions
Options to initiate a timestamp.
Definition Timestamp.h:25
bool operator<(const Timestamp &t) const
Timestamp & operator+=(double s)
Timestamp & operator-=(double s)
Timestamp(Timestamp::tOptions option=NONE)
Definition Timestamp.cpp:39
unsigned long m_secs
Seconds.
Definition Timestamp.h:196
Timestamp plus(unsigned long s, unsigned long us) const
double operator-(const Timestamp &t) const
string Format(bool machine_friendly=false) const
bool operator>(const Timestamp &t) const
bool operator>=(const Timestamp &t) const
void setToCurrentTime()
Definition Timestamp.cpp:56
string getStringTime() const
Definition Timestamp.cpp:98
unsigned long m_usecs
Microseconds.
Definition Timestamp.h:198