27 #include <sys/timeb.h> 28 #define sprintf sprintf_s 41 if(option & CURRENT_TIME)
43 else if(option & ZERO)
47 Timestamp::~Timestamp(
void)
51 bool Timestamp::empty()
const 53 return m_secs == 0 && m_usecs == 0;
56 void Timestamp::setToCurrentTime(){
59 struct __timeb32 timebuffer;
60 _ftime32_s( &timebuffer );
62 m_secs = timebuffer.time;
63 m_usecs = timebuffer.millitm * 1000;
66 gettimeofday(&now, NULL);
68 m_usecs = now.tv_usec;
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());
79 m_secs = atol(stime.substr(0, p).c_str());
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()));
87 void Timestamp::setTime(
double s)
89 m_secs = (
unsigned long)s;
90 m_usecs = (s - (double)m_secs) * 1e6;
93 double Timestamp::getFloatTime()
const {
94 return double(m_secs) + double(m_usecs)/1000000.0;
97 string Timestamp::getStringTime()
const {
99 sprintf(buf,
"%.6lf", this->getFloatTime());
103 double Timestamp::operator- (
const Timestamp &t)
const {
121 unsigned long secs = (long)floor(s);
122 unsigned long usecs = (long)((s - (
double)secs) * 1e6);
124 return this->plus(secs, usecs);
127 Timestamp Timestamp::plus(
unsigned long secs,
unsigned long usecs)
const 131 const unsigned long max = 1000000ul;
133 if(m_usecs + usecs >= max)
134 t.
setTime(m_secs + secs + 1, m_usecs + usecs - max);
136 t.
setTime(m_secs + secs, m_usecs + usecs);
143 unsigned long secs = (long)floor(s);
144 unsigned long usecs = (long)((s - (
double)secs) * 1e6);
146 return this->minus(secs, usecs);
149 Timestamp Timestamp::minus(
unsigned long secs,
unsigned long usecs)
const 153 const unsigned long max = 1000000ul;
156 t.
setTime(m_secs - secs - 1, max - (usecs - m_usecs));
158 t.
setTime(m_secs - secs, m_usecs - usecs);
165 if(m_secs > t.
m_secs)
return true;
172 if(m_secs > t.
m_secs)
return true;
179 if(m_secs < t.
m_secs)
return true;
186 if(m_secs < t.
m_secs)
return true;
197 string Timestamp::Format(
bool machine_friendly)
const 201 time_t t = (time_t)getFloatTime();
204 localtime_s(&tm_time, &t);
206 localtime_r(&t, &tm_time);
213 strftime(buffer, 128,
"%Y%m%d_%H%M%S", &tm_time);
217 strftime(buffer, 128,
"%c", &tm_time);
220 return string(buffer);
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);
228 int minutes = int(s / 60.0);
230 int seconds = int(s);
231 int ms = int((s - seconds)*1e6);
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 <<
":";
241 if(!b) ss <<
"." << setw(6) << ms;
double getFloatTime() const
tOptions
Options to initiate a timestamp.
void setTime(unsigned long secs, unsigned long usecs)
bool operator<(const HyperDijkstra::AdjacencyMapEntry &a, const HyperDijkstra::AdjacencyMapEntry &b)
unsigned long m_secs
Seconds.
unsigned long m_usecs
Microseconds.