Timespan.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Timespan.h
  3. //
  4. // Library: Foundation
  5. // Package: DateTime
  6. // Module: Timespan
  7. //
  8. // Definition of the Timespan 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_Timespan_INCLUDED
  16. #define Foundation_Timespan_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Timestamp.h"
  19. namespace Poco {
  20. class Foundation_API Timespan
  21. /// A class that represents time spans up to microsecond resolution.
  22. {
  23. public:
  24. typedef Timestamp::TimeDiff TimeDiff;
  25. Timespan();
  26. /// Creates a zero Timespan.
  27. Timespan(TimeDiff microseconds);
  28. /// Creates a Timespan.
  29. Timespan(long seconds, long microseconds);
  30. /// Creates a Timespan. Useful for creating
  31. /// a Timespan from a struct timeval.
  32. Timespan(int days, int hours, int minutes, int seconds, int microSeconds);
  33. /// Creates a Timespan.
  34. Timespan(const Timespan& timespan);
  35. /// Creates a Timespan from another one.
  36. ~Timespan();
  37. /// Destroys the Timespan.
  38. Timespan& operator = (const Timespan& timespan);
  39. /// Assignment operator.
  40. Timespan& operator = (TimeDiff microseconds);
  41. /// Assignment operator.
  42. Timespan& assign(int days, int hours, int minutes, int seconds, int microSeconds);
  43. /// Assigns a new span.
  44. Timespan& assign(long seconds, long microseconds);
  45. /// Assigns a new span. Useful for assigning
  46. /// from a struct timeval.
  47. void swap(Timespan& timespan);
  48. /// Swaps the Timespan with another one.
  49. bool operator == (const Timespan& ts) const;
  50. bool operator != (const Timespan& ts) const;
  51. bool operator > (const Timespan& ts) const;
  52. bool operator >= (const Timespan& ts) const;
  53. bool operator < (const Timespan& ts) const;
  54. bool operator <= (const Timespan& ts) const;
  55. bool operator == (TimeDiff microSeconds) const;
  56. bool operator != (TimeDiff microSeconds) const;
  57. bool operator > (TimeDiff microSeconds) const;
  58. bool operator >= (TimeDiff microSeconds) const;
  59. bool operator < (TimeDiff microSeconds) const;
  60. bool operator <= (TimeDiff microSeconds) const;
  61. Timespan operator + (const Timespan& d) const;
  62. Timespan operator - (const Timespan& d) const;
  63. Timespan& operator += (const Timespan& d);
  64. Timespan& operator -= (const Timespan& d);
  65. Timespan operator + (TimeDiff microSeconds) const;
  66. Timespan operator - (TimeDiff microSeconds) const;
  67. Timespan& operator += (TimeDiff microSeconds);
  68. Timespan& operator -= (TimeDiff microSeconds);
  69. int days() const;
  70. /// Returns the number of days.
  71. int hours() const;
  72. /// Returns the number of hours (0 to 23).
  73. int totalHours() const;
  74. /// Returns the total number of hours.
  75. int minutes() const;
  76. /// Returns the number of minutes (0 to 59).
  77. int totalMinutes() const;
  78. /// Returns the total number of minutes.
  79. int seconds() const;
  80. /// Returns the number of seconds (0 to 59).
  81. int totalSeconds() const;
  82. /// Returns the total number of seconds.
  83. int milliseconds() const;
  84. /// Returns the number of milliseconds (0 to 999).
  85. TimeDiff totalMilliseconds() const;
  86. /// Returns the total number of milliseconds.
  87. int microseconds() const;
  88. /// Returns the fractions of a millisecond
  89. /// in microseconds (0 to 999).
  90. int useconds() const;
  91. /// Returns the fractions of a second
  92. /// in microseconds (0 to 999999).
  93. TimeDiff totalMicroseconds() const;
  94. /// Returns the total number of microseconds.
  95. static const TimeDiff MILLISECONDS; /// The number of microseconds in a millisecond.
  96. static const TimeDiff SECONDS; /// The number of microseconds in a second.
  97. static const TimeDiff MINUTES; /// The number of microseconds in a minute.
  98. static const TimeDiff HOURS; /// The number of microseconds in a hour.
  99. static const TimeDiff DAYS; /// The number of microseconds in a day.
  100. private:
  101. TimeDiff _span;
  102. };
  103. //
  104. // inlines
  105. //
  106. inline int Timespan::days() const
  107. {
  108. return int(_span/DAYS);
  109. }
  110. inline int Timespan::hours() const
  111. {
  112. return int((_span/HOURS) % 24);
  113. }
  114. inline int Timespan::totalHours() const
  115. {
  116. return int(_span/HOURS);
  117. }
  118. inline int Timespan::minutes() const
  119. {
  120. return int((_span/MINUTES) % 60);
  121. }
  122. inline int Timespan::totalMinutes() const
  123. {
  124. return int(_span/MINUTES);
  125. }
  126. inline int Timespan::seconds() const
  127. {
  128. return int((_span/SECONDS) % 60);
  129. }
  130. inline int Timespan::totalSeconds() const
  131. {
  132. return int(_span/SECONDS);
  133. }
  134. inline int Timespan::milliseconds() const
  135. {
  136. return int((_span/MILLISECONDS) % 1000);
  137. }
  138. inline Timespan::TimeDiff Timespan::totalMilliseconds() const
  139. {
  140. return _span/MILLISECONDS;
  141. }
  142. inline int Timespan::microseconds() const
  143. {
  144. return int(_span % 1000);
  145. }
  146. inline int Timespan::useconds() const
  147. {
  148. return int(_span % 1000000);
  149. }
  150. inline Timespan::TimeDiff Timespan::totalMicroseconds() const
  151. {
  152. return _span;
  153. }
  154. inline bool Timespan::operator == (const Timespan& ts) const
  155. {
  156. return _span == ts._span;
  157. }
  158. inline bool Timespan::operator != (const Timespan& ts) const
  159. {
  160. return _span != ts._span;
  161. }
  162. inline bool Timespan::operator > (const Timespan& ts) const
  163. {
  164. return _span > ts._span;
  165. }
  166. inline bool Timespan::operator >= (const Timespan& ts) const
  167. {
  168. return _span >= ts._span;
  169. }
  170. inline bool Timespan::operator < (const Timespan& ts) const
  171. {
  172. return _span < ts._span;
  173. }
  174. inline bool Timespan::operator <= (const Timespan& ts) const
  175. {
  176. return _span <= ts._span;
  177. }
  178. inline bool Timespan::operator == (TimeDiff microSeconds) const
  179. {
  180. return _span == microSeconds;
  181. }
  182. inline bool Timespan::operator != (TimeDiff microSeconds) const
  183. {
  184. return _span != microSeconds;
  185. }
  186. inline bool Timespan::operator > (TimeDiff microSeconds) const
  187. {
  188. return _span > microSeconds;
  189. }
  190. inline bool Timespan::operator >= (TimeDiff microSeconds) const
  191. {
  192. return _span >= microSeconds;
  193. }
  194. inline bool Timespan::operator < (TimeDiff microSeconds) const
  195. {
  196. return _span < microSeconds;
  197. }
  198. inline bool Timespan::operator <= (TimeDiff microSeconds) const
  199. {
  200. return _span <= microSeconds;
  201. }
  202. inline void swap(Timespan& s1, Timespan& s2)
  203. {
  204. s1.swap(s2);
  205. }
  206. inline Timespan::~Timespan()
  207. {
  208. }
  209. } // namespace Poco
  210. #endif // Foundation_Timespan_INCLUDED