Timestamp.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Timestamp.h
  3. //
  4. // Library: Foundation
  5. // Package: DateTime
  6. // Module: Timestamp
  7. //
  8. // Definition of the Timestamp class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_Timestamp_INCLUDED
  16. #define Foundation_Timestamp_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include <ctime>
  19. namespace Poco {
  20. class Timespan;
  21. class Foundation_API Timestamp
  22. /// A Timestamp stores a monotonic* time value
  23. /// with (theoretical) microseconds resolution.
  24. /// Timestamps can be compared with each other
  25. /// and simple arithmetics are supported.
  26. ///
  27. /// [*] Note that Timestamp values are only monotonic as
  28. /// long as the systems's clock is monotonic as well
  29. /// (and not, e.g. set back due to time synchronization
  30. /// or other reasons).
  31. ///
  32. /// Timestamps are UTC (Coordinated Universal Time)
  33. /// based and thus independent of the timezone
  34. /// in effect on the system.
  35. ///
  36. /// The internal reference time is the Unix epoch,
  37. /// midnight, January 1, 1970.
  38. {
  39. public:
  40. typedef Int64 TimeVal;
  41. /// Monotonic UTC time value in microsecond resolution,
  42. /// with base time midnight, January 1, 1970.
  43. typedef Int64 UtcTimeVal;
  44. /// Monotonic UTC time value in 100 nanosecond resolution,
  45. /// with base time midnight, October 15, 1582.
  46. typedef Int64 TimeDiff;
  47. /// Difference between two TimeVal values in microseconds.
  48. static const TimeVal TIMEVAL_MIN; /// Minimum timestamp value.
  49. static const TimeVal TIMEVAL_MAX; /// Maximum timestamp value.
  50. Timestamp();
  51. /// Creates a timestamp with the current time.
  52. Timestamp(TimeVal tv);
  53. /// Creates a timestamp from the given time value
  54. /// (microseconds since midnight, January 1, 1970).
  55. Timestamp(const Timestamp& other);
  56. /// Copy constructor.
  57. ~Timestamp();
  58. /// Destroys the timestamp
  59. Timestamp& operator = (const Timestamp& other);
  60. Timestamp& operator = (TimeVal tv);
  61. void swap(Timestamp& timestamp);
  62. /// Swaps the Timestamp with another one.
  63. void update();
  64. /// Updates the Timestamp with the current time.
  65. bool operator == (const Timestamp& ts) const;
  66. bool operator != (const Timestamp& ts) const;
  67. bool operator > (const Timestamp& ts) const;
  68. bool operator >= (const Timestamp& ts) const;
  69. bool operator < (const Timestamp& ts) const;
  70. bool operator <= (const Timestamp& ts) const;
  71. Timestamp operator + (TimeDiff d) const;
  72. Timestamp operator + (const Timespan& span) const;
  73. Timestamp operator - (TimeDiff d) const;
  74. Timestamp operator - (const Timespan& span) const;
  75. TimeDiff operator - (const Timestamp& ts) const;
  76. Timestamp& operator += (TimeDiff d);
  77. Timestamp& operator += (const Timespan& span);
  78. Timestamp& operator -= (TimeDiff d);
  79. Timestamp& operator -= (const Timespan& span);
  80. std::time_t epochTime() const;
  81. /// Returns the timestamp expressed in time_t.
  82. /// time_t base time is midnight, January 1, 1970.
  83. /// Resolution is one second.
  84. UtcTimeVal utcTime() const;
  85. /// Returns the timestamp expressed in UTC-based
  86. /// time. UTC base time is midnight, October 15, 1582.
  87. /// Resolution is 100 nanoseconds.
  88. TimeVal epochMicroseconds() const;
  89. /// Returns the timestamp expressed in microseconds
  90. /// since the Unix epoch, midnight, January 1, 1970.
  91. TimeDiff elapsed() const;
  92. /// Returns the time elapsed since the time denoted by
  93. /// the timestamp. Equivalent to Timestamp() - *this.
  94. bool isElapsed(TimeDiff interval) const;
  95. /// Returns true iff the given interval has passed
  96. /// since the time denoted by the timestamp.
  97. TimeVal raw() const;
  98. /// Returns the raw time value.
  99. ///
  100. /// Same as epochMicroseconds().
  101. static Timestamp fromEpochTime(std::time_t t);
  102. /// Creates a timestamp from a std::time_t.
  103. static Timestamp fromUtcTime(UtcTimeVal val);
  104. /// Creates a timestamp from a UTC time value
  105. /// (100 nanosecond intervals since midnight,
  106. /// October 15, 1582).
  107. static TimeDiff resolution();
  108. /// Returns the resolution in units per second.
  109. /// Since the timestamp has microsecond resolution,
  110. /// the returned value is always 1000000.
  111. #if defined(_WIN32)
  112. static Timestamp fromFileTimeNP(UInt32 fileTimeLow, UInt32 fileTimeHigh);
  113. void toFileTimeNP(UInt32& fileTimeLow, UInt32& fileTimeHigh) const;
  114. #endif
  115. private:
  116. TimeVal _ts;
  117. };
  118. //
  119. // inlines
  120. //
  121. inline bool Timestamp::operator == (const Timestamp& ts) const
  122. {
  123. return _ts == ts._ts;
  124. }
  125. inline bool Timestamp::operator != (const Timestamp& ts) const
  126. {
  127. return _ts != ts._ts;
  128. }
  129. inline bool Timestamp::operator > (const Timestamp& ts) const
  130. {
  131. return _ts > ts._ts;
  132. }
  133. inline bool Timestamp::operator >= (const Timestamp& ts) const
  134. {
  135. return _ts >= ts._ts;
  136. }
  137. inline bool Timestamp::operator < (const Timestamp& ts) const
  138. {
  139. return _ts < ts._ts;
  140. }
  141. inline bool Timestamp::operator <= (const Timestamp& ts) const
  142. {
  143. return _ts <= ts._ts;
  144. }
  145. inline Timestamp Timestamp::operator + (Timestamp::TimeDiff d) const
  146. {
  147. return Timestamp(_ts + d);
  148. }
  149. inline Timestamp Timestamp::operator - (Timestamp::TimeDiff d) const
  150. {
  151. return Timestamp(_ts - d);
  152. }
  153. inline Timestamp::TimeDiff Timestamp::operator - (const Timestamp& ts) const
  154. {
  155. return _ts - ts._ts;
  156. }
  157. inline Timestamp& Timestamp::operator += (Timestamp::TimeDiff d)
  158. {
  159. _ts += d;
  160. return *this;
  161. }
  162. inline Timestamp& Timestamp::operator -= (Timestamp::TimeDiff d)
  163. {
  164. _ts -= d;
  165. return *this;
  166. }
  167. inline std::time_t Timestamp::epochTime() const
  168. {
  169. return std::time_t(_ts/resolution());
  170. }
  171. inline Timestamp::UtcTimeVal Timestamp::utcTime() const
  172. {
  173. return _ts*10 + (TimeDiff(0x01b21dd2) << 32) + 0x13814000;
  174. }
  175. inline Timestamp::TimeVal Timestamp::epochMicroseconds() const
  176. {
  177. return _ts;
  178. }
  179. inline Timestamp::TimeDiff Timestamp::elapsed() const
  180. {
  181. Timestamp now;
  182. return now - *this;
  183. }
  184. inline bool Timestamp::isElapsed(Timestamp::TimeDiff interval) const
  185. {
  186. Timestamp now;
  187. Timestamp::TimeDiff diff = now - *this;
  188. return diff >= interval;
  189. }
  190. inline Timestamp::TimeDiff Timestamp::resolution()
  191. {
  192. return 1000000;
  193. }
  194. inline void swap(Timestamp& s1, Timestamp& s2)
  195. {
  196. s1.swap(s2);
  197. }
  198. inline Timestamp::TimeVal Timestamp::raw() const
  199. {
  200. return _ts;
  201. }
  202. } // namespace Poco
  203. #endif // Foundation_Timestamp_INCLUDED