Timestamp.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. #include <cstdio>
  13. #include <cstdlib>
  14. #include <ctime>
  15. #include <cmath>
  16. #include <sstream>
  17. #include <iomanip>
  18. #ifdef _WIN32
  19. #ifndef WIN32
  20. #define WIN32
  21. #endif
  22. #endif
  23. #ifdef WIN32
  24. #include <sys/timeb.h>
  25. #define sprintf sprintf_s
  26. #else
  27. #include <sys/time.h>
  28. #endif
  29. #include "Timestamp.h"
  30. using namespace std;
  31. using namespace DUtils;
  32. Timestamp::Timestamp(Timestamp::tOptions option)
  33. {
  34. if(option & CURRENT_TIME)
  35. setToCurrentTime();
  36. else if(option & ZERO)
  37. setTime(0.);
  38. }
  39. Timestamp::~Timestamp(void)
  40. {
  41. }
  42. bool Timestamp::empty() const
  43. {
  44. return m_secs == 0 && m_usecs == 0;
  45. }
  46. void Timestamp::setToCurrentTime(){
  47. #ifdef WIN32
  48. struct __timeb32 timebuffer;
  49. _ftime32_s( &timebuffer ); // C4996
  50. // Note: _ftime is deprecated; consider using _ftime_s instead
  51. m_secs = timebuffer.time;
  52. m_usecs = timebuffer.millitm * 1000;
  53. #else
  54. struct timeval now;
  55. gettimeofday(&now, NULL);
  56. m_secs = now.tv_sec;
  57. m_usecs = now.tv_usec;
  58. #endif
  59. }
  60. void Timestamp::setTime(const string &stime){
  61. string::size_type p = stime.find('.');
  62. if(p == string::npos){
  63. m_secs = atol(stime.c_str());
  64. m_usecs = 0;
  65. }else{
  66. m_secs = atol(stime.substr(0, p).c_str());
  67. string s_usecs = stime.substr(p+1, 6);
  68. m_usecs = atol(stime.substr(p+1).c_str());
  69. m_usecs *= (unsigned long)pow(10.0, double(6 - s_usecs.length()));
  70. }
  71. }
  72. void Timestamp::setTime(double s)
  73. {
  74. m_secs = (unsigned long)s;
  75. m_usecs = (s - (double)m_secs) * 1e6;
  76. }
  77. double Timestamp::getFloatTime() const {
  78. return double(m_secs) + double(m_usecs)/1000000.0;
  79. }
  80. string Timestamp::getStringTime() const {
  81. char buf[32];
  82. sprintf(buf, "%.6lf", this->getFloatTime());
  83. return string(buf);
  84. }
  85. double Timestamp::operator- (const Timestamp &t) const {
  86. return this->getFloatTime() - t.getFloatTime();
  87. }
  88. Timestamp& Timestamp::operator+= (double s)
  89. {
  90. *this = *this + s;
  91. return *this;
  92. }
  93. Timestamp& Timestamp::operator-= (double s)
  94. {
  95. *this = *this - s;
  96. return *this;
  97. }
  98. Timestamp Timestamp::operator+ (double s) const
  99. {
  100. unsigned long secs = (long)floor(s);
  101. unsigned long usecs = (long)((s - (double)secs) * 1e6);
  102. return this->plus(secs, usecs);
  103. }
  104. Timestamp Timestamp::plus(unsigned long secs, unsigned long usecs) const
  105. {
  106. Timestamp t;
  107. const unsigned long max = 1000000ul;
  108. if(m_usecs + usecs >= max)
  109. t.setTime(m_secs + secs + 1, m_usecs + usecs - max);
  110. else
  111. t.setTime(m_secs + secs, m_usecs + usecs);
  112. return t;
  113. }
  114. Timestamp Timestamp::operator- (double s) const
  115. {
  116. unsigned long secs = (long)floor(s);
  117. unsigned long usecs = (long)((s - (double)secs) * 1e6);
  118. return this->minus(secs, usecs);
  119. }
  120. Timestamp Timestamp::minus(unsigned long secs, unsigned long usecs) const
  121. {
  122. Timestamp t;
  123. const unsigned long max = 1000000ul;
  124. if(m_usecs < usecs)
  125. t.setTime(m_secs - secs - 1, max - (usecs - m_usecs));
  126. else
  127. t.setTime(m_secs - secs, m_usecs - usecs);
  128. return t;
  129. }
  130. bool Timestamp::operator> (const Timestamp &t) const
  131. {
  132. if(m_secs > t.m_secs) return true;
  133. else if(m_secs == t.m_secs) return m_usecs > t.m_usecs;
  134. else return false;
  135. }
  136. bool Timestamp::operator>= (const Timestamp &t) const
  137. {
  138. if(m_secs > t.m_secs) return true;
  139. else if(m_secs == t.m_secs) return m_usecs >= t.m_usecs;
  140. else return false;
  141. }
  142. bool Timestamp::operator< (const Timestamp &t) const
  143. {
  144. if(m_secs < t.m_secs) return true;
  145. else if(m_secs == t.m_secs) return m_usecs < t.m_usecs;
  146. else return false;
  147. }
  148. bool Timestamp::operator<= (const Timestamp &t) const
  149. {
  150. if(m_secs < t.m_secs) return true;
  151. else if(m_secs == t.m_secs) return m_usecs <= t.m_usecs;
  152. else return false;
  153. }
  154. bool Timestamp::operator== (const Timestamp &t) const
  155. {
  156. return(m_secs == t.m_secs && m_usecs == t.m_usecs);
  157. }
  158. string Timestamp::Format(bool machine_friendly) const
  159. {
  160. struct tm tm_time;
  161. time_t t = (time_t)getFloatTime();
  162. #ifdef WIN32
  163. localtime_s(&tm_time, &t);
  164. #else
  165. localtime_r(&t, &tm_time);
  166. #endif
  167. char buffer[128];
  168. if(machine_friendly)
  169. {
  170. strftime(buffer, 128, "%Y%m%d_%H%M%S", &tm_time);
  171. }
  172. else
  173. {
  174. strftime(buffer, 128, "%c", &tm_time); // Thu Aug 23 14:55:02 2001
  175. }
  176. return string(buffer);
  177. }
  178. string Timestamp::Format(double s) {
  179. int days = int(s / (24. * 3600.0));
  180. s -= days * (24. * 3600.0);
  181. int hours = int(s / 3600.0);
  182. s -= hours * 3600;
  183. int minutes = int(s / 60.0);
  184. s -= minutes * 60;
  185. int seconds = int(s);
  186. int ms = int((s - seconds)*1e6);
  187. stringstream ss;
  188. ss.fill('0');
  189. bool b;
  190. if((b = (days > 0))) ss << days << "d ";
  191. if((b = (b || hours > 0))) ss << setw(2) << hours << ":";
  192. if((b = (b || minutes > 0))) ss << setw(2) << minutes << ":";
  193. if(b) ss << setw(2);
  194. ss << seconds;
  195. if(!b) ss << "." << setw(6) << ms;
  196. return ss.str();
  197. }