Timestamp.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * File: Timestamp.h
  3. * Author: Dorian Galvez-Lopez
  4. * Date: March 2009
  5. * Description: timestamping functions
  6. * License: see the LICENSE.txt file
  7. *
  8. */
  9. #ifndef __D_TIMESTAMP__
  10. #define __D_TIMESTAMP__
  11. #include <iostream>
  12. using namespace std;
  13. namespace DUtils {
  14. /// Timestamp
  15. class Timestamp
  16. {
  17. public:
  18. /// Options to initiate a timestamp
  19. enum tOptions
  20. {
  21. NONE = 0,
  22. CURRENT_TIME = 0x1,
  23. ZERO = 0x2
  24. };
  25. public:
  26. /**
  27. * Creates a timestamp
  28. * @param option option to set the initial time stamp
  29. */
  30. Timestamp(Timestamp::tOptions option = NONE);
  31. /**
  32. * Destructor
  33. */
  34. virtual ~Timestamp(void);
  35. /**
  36. * Says if the timestamp is "empty": seconds and usecs are both 0, as
  37. * when initiated with the ZERO flag
  38. * @return true iif secs == usecs == 0
  39. */
  40. bool empty() const;
  41. /**
  42. * Sets this instance to the current time
  43. */
  44. void setToCurrentTime();
  45. /**
  46. * Sets the timestamp from seconds and microseconds
  47. * @param secs: seconds
  48. * @param usecs: microseconds
  49. */
  50. inline void setTime(unsigned long secs, unsigned long usecs){
  51. m_secs = secs;
  52. m_usecs = usecs;
  53. }
  54. /**
  55. * Returns the timestamp in seconds and microseconds
  56. * @param secs seconds
  57. * @param usecs microseconds
  58. */
  59. inline void getTime(unsigned long &secs, unsigned long &usecs) const
  60. {
  61. secs = m_secs;
  62. usecs = m_usecs;
  63. }
  64. /**
  65. * Sets the timestamp from a string with the time in seconds
  66. * @param stime: string such as "1235603336.036609"
  67. */
  68. void setTime(const string &stime);
  69. /**
  70. * Sets the timestamp from a number of seconds from the epoch
  71. * @param s seconds from the epoch
  72. */
  73. void setTime(double s);
  74. /**
  75. * Returns this timestamp as the number of seconds in (long) float format
  76. */
  77. double getFloatTime() const;
  78. /**
  79. * Returns this timestamp as the number of seconds in fixed length string format
  80. */
  81. string getStringTime() const;
  82. /**
  83. * Returns the difference in seconds between this timestamp (greater) and t (smaller)
  84. * If the order is swapped, a negative number is returned
  85. * @param t: timestamp to subtract from this timestamp
  86. * @return difference in seconds
  87. */
  88. double operator- (const Timestamp &t) const;
  89. /**
  90. * Returns a copy of this timestamp + s seconds + us microseconds
  91. * @param s seconds
  92. * @param us microseconds
  93. */
  94. Timestamp plus(unsigned long s, unsigned long us) const;
  95. /**
  96. * Returns a copy of this timestamp - s seconds - us microseconds
  97. * @param s seconds
  98. * @param us microseconds
  99. */
  100. Timestamp minus(unsigned long s, unsigned long us) const;
  101. /**
  102. * Adds s seconds to this timestamp and returns a reference to itself
  103. * @param s seconds
  104. * @return reference to this timestamp
  105. */
  106. Timestamp& operator+= (double s);
  107. /**
  108. * Substracts s seconds to this timestamp and returns a reference to itself
  109. * @param s seconds
  110. * @return reference to this timestamp
  111. */
  112. Timestamp& operator-= (double s);
  113. /**
  114. * Returns a copy of this timestamp + s seconds
  115. * @param s: seconds
  116. */
  117. Timestamp operator+ (double s) const;
  118. /**
  119. * Returns a copy of this timestamp - s seconds
  120. * @param s: seconds
  121. */
  122. Timestamp operator- (double s) const;
  123. /**
  124. * Returns whether this timestamp is at the future of t
  125. * @param t
  126. */
  127. bool operator> (const Timestamp &t) const;
  128. /**
  129. * Returns whether this timestamp is at the future of (or is the same as) t
  130. * @param t
  131. */
  132. bool operator>= (const Timestamp &t) const;
  133. /**
  134. * Returns whether this timestamp and t represent the same instant
  135. * @param t
  136. */
  137. bool operator== (const Timestamp &t) const;
  138. /**
  139. * Returns whether this timestamp is at the past of t
  140. * @param t
  141. */
  142. bool operator< (const Timestamp &t) const;
  143. /**
  144. * Returns whether this timestamp is at the past of (or is the same as) t
  145. * @param t
  146. */
  147. bool operator<= (const Timestamp &t) const;
  148. /**
  149. * Returns the timestamp in a human-readable string
  150. * @param machine_friendly if true, the returned string is formatted
  151. * to yyyymmdd_hhmmss, without weekday or spaces
  152. * @note This has not been tested under Windows
  153. * @note The timestamp is truncated to seconds
  154. */
  155. string Format(bool machine_friendly = false) const;
  156. /**
  157. * Returns a string version of the elapsed time in seconds, with the format
  158. * xd hh:mm:ss, hh:mm:ss, mm:ss or s.us
  159. * @param s: elapsed seconds (given by getFloatTime) to format
  160. */
  161. static string Format(double s);
  162. protected:
  163. /// Seconds
  164. unsigned long m_secs; // seconds
  165. /// Microseconds
  166. unsigned long m_usecs; // microseconds
  167. };
  168. }
  169. #endif