| 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 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef Foundation_Timespan_INCLUDED | 
|---|
| 18 | #define Foundation_Timespan_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include "Poco/Timestamp.h" | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | namespace Poco { | 
|---|
| 26 |  | 
|---|
| 27 |  | 
|---|
| 28 | class Foundation_API Timespan | 
|---|
| 29 | /// A class that represents time spans up to microsecond resolution. | 
|---|
| 30 | { | 
|---|
| 31 | public: | 
|---|
| 32 | typedef Timestamp::TimeDiff TimeDiff; | 
|---|
| 33 |  | 
|---|
| 34 | Timespan(); | 
|---|
| 35 | /// Creates a zero Timespan. | 
|---|
| 36 |  | 
|---|
| 37 | Timespan(TimeDiff microseconds); | 
|---|
| 38 | /// Creates a Timespan. | 
|---|
| 39 |  | 
|---|
| 40 | Timespan(long seconds, long microseconds); | 
|---|
| 41 | /// Creates a Timespan. Useful for creating | 
|---|
| 42 | /// a Timespan from a struct timeval. | 
|---|
| 43 |  | 
|---|
| 44 | Timespan(int days, int hours, int minutes, int seconds, int microSeconds); | 
|---|
| 45 | /// Creates a Timespan. | 
|---|
| 46 |  | 
|---|
| 47 | Timespan(const Timespan& timespan); | 
|---|
| 48 | /// Creates a Timespan from another one. | 
|---|
| 49 |  | 
|---|
| 50 | ~Timespan(); | 
|---|
| 51 | /// Destroys the Timespan. | 
|---|
| 52 |  | 
|---|
| 53 | Timespan& operator = (const Timespan& timespan); | 
|---|
| 54 | /// Assignment operator. | 
|---|
| 55 |  | 
|---|
| 56 | Timespan& operator = (TimeDiff microseconds); | 
|---|
| 57 | /// Assignment operator. | 
|---|
| 58 |  | 
|---|
| 59 | Timespan& assign(int days, int hours, int minutes, int seconds, int microSeconds); | 
|---|
| 60 | /// Assigns a new span. | 
|---|
| 61 |  | 
|---|
| 62 | Timespan& assign(long seconds, long microseconds); | 
|---|
| 63 | /// Assigns a new span. Useful for assigning | 
|---|
| 64 | /// from a struct timeval. | 
|---|
| 65 |  | 
|---|
| 66 | void swap(Timespan& timespan); | 
|---|
| 67 | /// Swaps the Timespan with another one. | 
|---|
| 68 |  | 
|---|
| 69 | bool operator == (const Timespan& ts) const; | 
|---|
| 70 | bool operator != (const Timespan& ts) const; | 
|---|
| 71 | bool operator >  (const Timespan& ts) const; | 
|---|
| 72 | bool operator >= (const Timespan& ts) const; | 
|---|
| 73 | bool operator <  (const Timespan& ts) const; | 
|---|
| 74 | bool operator <= (const Timespan& ts) const; | 
|---|
| 75 |  | 
|---|
| 76 | bool operator == (TimeDiff microSeconds) const; | 
|---|
| 77 | bool operator != (TimeDiff microSeconds) const; | 
|---|
| 78 | bool operator >  (TimeDiff microSeconds) const; | 
|---|
| 79 | bool operator >= (TimeDiff microSeconds) const; | 
|---|
| 80 | bool operator <  (TimeDiff microSeconds) const; | 
|---|
| 81 | bool operator <= (TimeDiff microSeconds) const; | 
|---|
| 82 |  | 
|---|
| 83 | Timespan operator + (const Timespan& d) const; | 
|---|
| 84 | Timespan operator - (const Timespan& d) const; | 
|---|
| 85 | Timespan& operator += (const Timespan& d); | 
|---|
| 86 | Timespan& operator -= (const Timespan& d); | 
|---|
| 87 |  | 
|---|
| 88 | Timespan operator + (TimeDiff microSeconds) const; | 
|---|
| 89 | Timespan operator - (TimeDiff microSeconds) const; | 
|---|
| 90 | Timespan& operator += (TimeDiff microSeconds); | 
|---|
| 91 | Timespan& operator -= (TimeDiff microSeconds); | 
|---|
| 92 |  | 
|---|
| 93 | int days() const; | 
|---|
| 94 | /// Returns the number of days. | 
|---|
| 95 |  | 
|---|
| 96 | int hours() const; | 
|---|
| 97 | /// Returns the number of hours (0 to 23). | 
|---|
| 98 |  | 
|---|
| 99 | int totalHours() const; | 
|---|
| 100 | /// Returns the total number of hours. | 
|---|
| 101 |  | 
|---|
| 102 | int minutes() const; | 
|---|
| 103 | /// Returns the number of minutes (0 to 59). | 
|---|
| 104 |  | 
|---|
| 105 | int totalMinutes() const; | 
|---|
| 106 | /// Returns the total number of minutes. | 
|---|
| 107 |  | 
|---|
| 108 | int seconds() const; | 
|---|
| 109 | /// Returns the number of seconds (0 to 59). | 
|---|
| 110 |  | 
|---|
| 111 | int totalSeconds() const; | 
|---|
| 112 | /// Returns the total number of seconds. | 
|---|
| 113 |  | 
|---|
| 114 | int milliseconds() const; | 
|---|
| 115 | /// Returns the number of milliseconds (0 to 999). | 
|---|
| 116 |  | 
|---|
| 117 | TimeDiff totalMilliseconds() const; | 
|---|
| 118 | /// Returns the total number of milliseconds. | 
|---|
| 119 |  | 
|---|
| 120 | int microseconds() const; | 
|---|
| 121 | /// Returns the fractions of a millisecond | 
|---|
| 122 | /// in microseconds (0 to 999). | 
|---|
| 123 |  | 
|---|
| 124 | int useconds() const; | 
|---|
| 125 | /// Returns the fractions of a second | 
|---|
| 126 | /// in microseconds (0 to 999999). | 
|---|
| 127 |  | 
|---|
| 128 | TimeDiff totalMicroseconds() const; | 
|---|
| 129 | /// Returns the total number of microseconds. | 
|---|
| 130 |  | 
|---|
| 131 | static const TimeDiff MILLISECONDS; /// The number of microseconds in a millisecond. | 
|---|
| 132 | static const TimeDiff SECONDS;      /// The number of microseconds in a second. | 
|---|
| 133 | static const TimeDiff MINUTES;      /// The number of microseconds in a minute. | 
|---|
| 134 | static const TimeDiff HOURS;        /// The number of microseconds in a hour. | 
|---|
| 135 | static const TimeDiff DAYS;         /// The number of microseconds in a day. | 
|---|
| 136 |  | 
|---|
| 137 | private: | 
|---|
| 138 | TimeDiff _span; | 
|---|
| 139 | }; | 
|---|
| 140 |  | 
|---|
| 141 |  | 
|---|
| 142 | // | 
|---|
| 143 | // inlines | 
|---|
| 144 | // | 
|---|
| 145 | inline int Timespan::days() const | 
|---|
| 146 | { | 
|---|
| 147 | return int(_span/DAYS); | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 |  | 
|---|
| 151 | inline int Timespan::hours() const | 
|---|
| 152 | { | 
|---|
| 153 | return int((_span/HOURS) % 24); | 
|---|
| 154 | } | 
|---|
| 155 |  | 
|---|
| 156 |  | 
|---|
| 157 | inline int Timespan::totalHours() const | 
|---|
| 158 | { | 
|---|
| 159 | return int(_span/HOURS); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 |  | 
|---|
| 163 | inline int Timespan::minutes() const | 
|---|
| 164 | { | 
|---|
| 165 | return int((_span/MINUTES) % 60); | 
|---|
| 166 | } | 
|---|
| 167 |  | 
|---|
| 168 |  | 
|---|
| 169 | inline int Timespan::totalMinutes() const | 
|---|
| 170 | { | 
|---|
| 171 | return int(_span/MINUTES); | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 |  | 
|---|
| 175 | inline int Timespan::seconds() const | 
|---|
| 176 | { | 
|---|
| 177 | return int((_span/SECONDS) % 60); | 
|---|
| 178 | } | 
|---|
| 179 |  | 
|---|
| 180 |  | 
|---|
| 181 | inline int Timespan::totalSeconds() const | 
|---|
| 182 | { | 
|---|
| 183 | return int(_span/SECONDS); | 
|---|
| 184 | } | 
|---|
| 185 |  | 
|---|
| 186 |  | 
|---|
| 187 | inline int Timespan::milliseconds() const | 
|---|
| 188 | { | 
|---|
| 189 | return int((_span/MILLISECONDS) % 1000); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 |  | 
|---|
| 193 | inline Timespan::TimeDiff Timespan::totalMilliseconds() const | 
|---|
| 194 | { | 
|---|
| 195 | return _span/MILLISECONDS; | 
|---|
| 196 | } | 
|---|
| 197 |  | 
|---|
| 198 |  | 
|---|
| 199 | inline int Timespan::microseconds() const | 
|---|
| 200 | { | 
|---|
| 201 | return int(_span % 1000); | 
|---|
| 202 | } | 
|---|
| 203 |  | 
|---|
| 204 |  | 
|---|
| 205 | inline int Timespan::useconds() const | 
|---|
| 206 | { | 
|---|
| 207 | return int(_span % 1000000); | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 |  | 
|---|
| 211 | inline Timespan::TimeDiff Timespan::totalMicroseconds() const | 
|---|
| 212 | { | 
|---|
| 213 | return _span; | 
|---|
| 214 | } | 
|---|
| 215 |  | 
|---|
| 216 |  | 
|---|
| 217 | inline bool Timespan::operator == (const Timespan& ts) const | 
|---|
| 218 | { | 
|---|
| 219 | return _span == ts._span; | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 |  | 
|---|
| 223 | inline bool Timespan::operator != (const Timespan& ts) const | 
|---|
| 224 | { | 
|---|
| 225 | return _span != ts._span; | 
|---|
| 226 | } | 
|---|
| 227 |  | 
|---|
| 228 |  | 
|---|
| 229 | inline bool Timespan::operator >  (const Timespan& ts) const | 
|---|
| 230 | { | 
|---|
| 231 | return _span > ts._span; | 
|---|
| 232 | } | 
|---|
| 233 |  | 
|---|
| 234 |  | 
|---|
| 235 | inline bool Timespan::operator >= (const Timespan& ts) const | 
|---|
| 236 | { | 
|---|
| 237 | return _span >= ts._span; | 
|---|
| 238 | } | 
|---|
| 239 |  | 
|---|
| 240 |  | 
|---|
| 241 | inline bool Timespan::operator <  (const Timespan& ts) const | 
|---|
| 242 | { | 
|---|
| 243 | return _span < ts._span; | 
|---|
| 244 | } | 
|---|
| 245 |  | 
|---|
| 246 |  | 
|---|
| 247 | inline bool Timespan::operator <= (const Timespan& ts) const | 
|---|
| 248 | { | 
|---|
| 249 | return _span <= ts._span; | 
|---|
| 250 | } | 
|---|
| 251 |  | 
|---|
| 252 |  | 
|---|
| 253 | inline bool Timespan::operator == (TimeDiff microSeconds) const | 
|---|
| 254 | { | 
|---|
| 255 | return _span == microSeconds; | 
|---|
| 256 | } | 
|---|
| 257 |  | 
|---|
| 258 |  | 
|---|
| 259 | inline bool Timespan::operator != (TimeDiff microSeconds) const | 
|---|
| 260 | { | 
|---|
| 261 | return _span != microSeconds; | 
|---|
| 262 | } | 
|---|
| 263 |  | 
|---|
| 264 |  | 
|---|
| 265 | inline bool Timespan::operator >  (TimeDiff microSeconds) const | 
|---|
| 266 | { | 
|---|
| 267 | return _span > microSeconds; | 
|---|
| 268 | } | 
|---|
| 269 |  | 
|---|
| 270 |  | 
|---|
| 271 | inline bool Timespan::operator >= (TimeDiff microSeconds) const | 
|---|
| 272 | { | 
|---|
| 273 | return _span >= microSeconds; | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 |  | 
|---|
| 277 | inline bool Timespan::operator <  (TimeDiff microSeconds) const | 
|---|
| 278 | { | 
|---|
| 279 | return _span < microSeconds; | 
|---|
| 280 | } | 
|---|
| 281 |  | 
|---|
| 282 |  | 
|---|
| 283 | inline bool Timespan::operator <= (TimeDiff microSeconds) const | 
|---|
| 284 | { | 
|---|
| 285 | return _span <= microSeconds; | 
|---|
| 286 | } | 
|---|
| 287 |  | 
|---|
| 288 |  | 
|---|
| 289 | inline void swap(Timespan& s1, Timespan& s2) | 
|---|
| 290 | { | 
|---|
| 291 | s1.swap(s2); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 |  | 
|---|
| 295 | inline Timespan::~Timespan() | 
|---|
| 296 | { | 
|---|
| 297 | } | 
|---|
| 298 |  | 
|---|
| 299 |  | 
|---|
| 300 | } // namespace Poco | 
|---|
| 301 |  | 
|---|
| 302 |  | 
|---|
| 303 | #endif // Foundation_Timespan_INCLUDED | 
|---|
| 304 |  | 
|---|