timespan.h
Go to the documentation of this file.
1 /*
2  * This library is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU Lesser General Public
4  * License as published by the Free Software Foundation; either
5  * version 2.1 of the License, or (at your option) any later version.
6  *
7  * As a special exception, you may use this file as part of a free
8  * software library without restriction. Specifically, if other files
9  * instantiate templates or use macros or inline functions from this
10  * file, or you compile this file and link it with other files to
11  * produce an executable, this file does not by itself cause the
12  * resulting executable to be covered by the GNU General Public
13  * License. This exception does not however invalidate any other
14  * reasons why the executable file might be covered by the GNU Library
15  * General Public License.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25  */
26 #ifndef cxxtools_Timespan_h
27 #define cxxtools_Timespan_h
28 
29 #include <stdint.h>
30 #include <algorithm>
31 #include <iosfwd>
32 
33 namespace cxxtools {
34 
38 class Timespan
39 {
40  public:
43  : _span(0)
44  {}
45 
48  : _span(microseconds)
49  { }
50 
55  : _span(int64_t(seconds)*Seconds + microseconds)
56  {
57  }
59  Timespan(int days, int hours, int minutes, int seconds, int microseconds);
60 
62  Timespan(const Timespan& timespan);
63 
66  {}
67 
69  Timespan& operator=(const Timespan& timespan);
70 
73 
75  Timespan& set(int days, int hours, int minutes, int seconds, int microseconds);
76 
80  Timespan& set(long seconds, long microseconds);
81 
83  void swap(Timespan& timespan);
84 
85  bool operator==(const Timespan& ts) const;
86 
87  bool operator!=(const Timespan& ts) const;
88 
89  bool operator>(const Timespan& ts) const;
90 
91  bool operator>=(const Timespan& ts) const;
92 
93  bool operator<(const Timespan& ts) const;
94 
95  bool operator<=(const Timespan& ts) const;
96 
97  bool operator==(int64_t microseconds) const;
98 
99  bool operator!=(int64_t microseconds) const;
100 
101  bool operator>(int64_t microseconds) const;
102 
103  bool operator>=(int64_t microseconds) const;
104 
105  bool operator<(int64_t microseconds) const;
106 
107  bool operator<=(int64_t microseconds) const;
108 
109  Timespan operator+(const Timespan& d) const;
110 
111  Timespan operator-(const Timespan& d) const;
112 
113  Timespan& operator+=(const Timespan& d);
114 
115  Timespan& operator-=(const Timespan& d);
116 
117  Timespan operator+(int64_t microseconds) const;
118 
119  Timespan operator-(int64_t microseconds) const;
120 
121  Timespan& operator+=(int64_t microseconds);
122 
123  Timespan& operator-=(int64_t microseconds);
124 
126  int days() const;
127 
129  int hours() const;
130 
132  int totalHours() const;
133 
135  int minutes() const;
136 
138  int totalMinutes() const;
139 
141  int seconds() const;
142 
144  int totalSeconds() const;
145 
147  int msecs() const;
148 
150  int64_t totalMSecs() const;
151 
153  int64_t totalUSecs() const
154  { return _span; }
155 
158  int microseconds() const;
159 
162  int useconds() const;
163 
165  int64_t toUSecs() const;
166 
168  //static const int64_t Milliseconds;
169 
171  //static const int64_t Seconds;
172 
174  //static const int64_t Minutes;
175 
177  //static const int64_t Hours;
178 
180  //static const int64_t Days;
181 
182  static const int64_t Milliseconds = 1000;
183  static const int64_t Seconds = 1000 * Timespan::Milliseconds;
184  static const int64_t Minutes = 60 * Timespan::Seconds;
185  static const int64_t Hours = 60 * Timespan::Minutes;
186  static const int64_t Days = 24 * Timespan::Hours;
187 
189  static Timespan gettimeofday();
190 
191  private:
192  int64_t _span;
193 };
194 
195 
196 inline int Timespan::days() const
197 {
198  return int(_span/Days);
199 }
200 
201 
202 inline int Timespan::hours() const
203 {
204  return int((_span/Hours) % 24);
205 }
206 
207 
208 inline int Timespan::totalHours() const
209 {
210  return int(_span/Hours);
211 }
212 
213 
214 inline int Timespan::minutes() const
215 {
216  return int((_span/Minutes) % 60);
217 }
218 
219 
220 inline int Timespan::totalMinutes() const
221 {
222  return int(_span/Minutes);
223 }
224 
225 
226 inline int Timespan::seconds() const
227 {
228  return int((_span/Seconds) % 60);
229 }
230 
231 
232 inline int Timespan::totalSeconds() const
233 {
234  return int(_span/Seconds);
235 }
236 
237 
238 inline int Timespan::msecs() const
239 {
240  return int((_span/Milliseconds) % 1000);
241 }
242 
243 
244 inline int64_t Timespan::totalMSecs() const
245 {
246  return _span/Milliseconds;
247 }
248 
249 
250 inline int Timespan::microseconds() const
251 {
252  return int(_span % 1000);
253 }
254 
255 
256 inline int Timespan::useconds() const
257 {
258  return int(_span % 1000000);
259 }
260 
261 
262 inline int64_t Timespan::toUSecs() const
263 {
264  return _span;
265 }
266 
267 
268 inline bool Timespan::operator == (const Timespan& ts) const
269 {
270  return _span == ts._span;
271 }
272 
273 
274 inline bool Timespan::operator != (const Timespan& ts) const
275 {
276  return _span != ts._span;
277 }
278 
279 
280 inline bool Timespan::operator > (const Timespan& ts) const
281 {
282  return _span > ts._span;
283 }
284 
285 
286 inline bool Timespan::operator >= (const Timespan& ts) const
287 {
288  return _span >= ts._span;
289 }
290 
291 
292 inline bool Timespan::operator < (const Timespan& ts) const
293 {
294  return _span < ts._span;
295 }
296 
297 
298 inline bool Timespan::operator <= (const Timespan& ts) const
299 {
300  return _span <= ts._span;
301 }
302 
303 
304 inline bool Timespan::operator == (int64_t microseconds) const
305 {
306  return _span == microseconds;
307 }
308 
309 
310 inline bool Timespan::operator != (int64_t microseconds) const
311 {
312  return _span != microseconds;
313 }
314 
315 
316 inline bool Timespan::operator > (int64_t microseconds) const
317 {
318  return _span > microseconds;
319 }
320 
321 
322 inline bool Timespan::operator >= (int64_t microseconds) const
323 {
324  return _span >= microseconds;
325 }
326 
327 
328 inline bool Timespan::operator < (int64_t microseconds) const
329 {
330  return _span < microseconds;
331 }
332 
333 
334 inline bool Timespan::operator <= (int64_t microseconds) const
335 {
336  return _span <= microseconds;
337 }
338 
339 
340 inline void swap(Timespan& s1, Timespan& s2)
341 {
342  s1.swap(s2);
343 }
344 
345 
346 inline Timespan::Timespan(int days, int hours, int minutes, int seconds, int microseconds)
347 : _span( int64_t(microseconds) +
348  int64_t(seconds)*Seconds +
349  int64_t(minutes)*Minutes +
350  int64_t(hours)*Hours +
351  int64_t(days)*Days )
352 {
353 }
354 
355 
356 inline Timespan::Timespan(const Timespan& timespan)
357 : _span(timespan._span)
358 {
359 }
360 
361 
362 inline Timespan& Timespan::operator=(const Timespan& timespan)
363 {
364  _span = timespan._span;
365  return *this;
366 }
367 
368 
369 inline Timespan& Timespan::operator=(int64_t microseconds)
370 {
371  _span = microseconds;
372  return *this;
373 }
374 
375 
376 inline Timespan& Timespan::set(int days, int hours, int minutes, int seconds, int microseconds)
377 {
378  _span = int64_t(microseconds) +
379  int64_t(seconds)*Seconds +
380  int64_t(minutes)*Minutes +
381  int64_t(hours)*Hours +
382  int64_t(days)*Days;
383  return *this;
384 }
385 
386 
387 inline Timespan& Timespan::set(long seconds, long microseconds)
388 {
389  _span = int64_t(seconds)*Seconds + int64_t(microseconds);
390  return *this;
391 }
392 
393 
394 inline void Timespan::swap(Timespan& timespan)
395 {
396  std::swap(_span, timespan._span);
397 }
398 
399 
401 {
402  return Timespan(_span + d._span);
403 }
404 
405 
407 {
408  return Timespan(_span - d._span);
409 }
410 
411 
413 {
414  _span += d._span;
415  return *this;
416 }
417 
418 
420 {
421  _span -= d._span;
422  return *this;
423 }
424 
425 
426 inline Timespan Timespan::operator + (int64_t microseconds) const
427 {
428  return Timespan(_span + microseconds);
429 }
430 
431 
432 inline Timespan Timespan::operator - (int64_t microseconds) const
433 {
434  return Timespan(_span - microseconds);
435 }
436 
437 
438 inline Timespan& Timespan::operator += (int64_t microseconds)
439 {
440  _span += microseconds;
441  return *this;
442 }
443 
444 
445 inline Timespan& Timespan::operator -= (int64_t microseconds)
446 {
447  _span -= microseconds;
448  return *this;
449 }
450 
451 std::ostream& operator<< (std::ostream& out, const Timespan& ht);
452 
453 } // namespace cxxtools
454 
455 #endif