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 <iosfwd>
31 #include <cxxtools/config.h>
32 
33 namespace cxxtools {
34 
35 class SerializationInfo;
36 
40 class Timespan
41 {
42  public:
44  explicit Timespan(int64_t microseconds = 0)
45  : _span(microseconds)
46  { }
47 
51  Timespan(long seconds, long microseconds)
52  : _span(int64_t(seconds)*1000*1000 + microseconds)
53  {
54  }
56  Timespan(int days, int hours, int minutes, int seconds, int microseconds)
57  : _span( int64_t(microseconds) +
58  int64_t(seconds)*1000*1000 +
59  int64_t(minutes)*1000*1000*60 +
60  int64_t(hours)*1000*1000*60*60 +
61  int64_t(days)*1000*1000*60*60*24 )
62  {
63  }
64 
65  bool operator==(const Timespan& ts) const
66  { return _span == ts._span; }
67 
68  bool operator!=(const Timespan& ts) const
69  { return _span != ts._span; }
70 
71  bool operator>(const Timespan& ts) const
72  { return _span > ts._span; }
73 
74  bool operator>=(const Timespan& ts) const
75  { return _span >= ts._span; }
76 
77  bool operator<(const Timespan& ts) const
78  { return _span < ts._span; }
79 
80  bool operator<=(const Timespan& ts) const
81  { return _span <= ts._span; }
82 
83  Timespan operator+(const Timespan& d) const
84  { return Timespan(_span + d._span); }
85 
86  Timespan operator-(const Timespan& d) const
87  { return Timespan(_span - d._span); }
88 
89  Timespan& operator+=(const Timespan& d)
90  { _span += d._span; return *this; }
91 
92  Timespan& operator-=(const Timespan& d)
93  { _span -= d._span; return *this; }
94 
95  Timespan operator-() const
96  { return Timespan(-_span); }
97 
99  double totalDays() const
100  { return double(_span) / 1000 / 1000 / 60 / 60 / 24; }
101 
103  double totalHours() const
104  { return double(_span) / 1000 / 1000 / 60 / 60; }
105 
107  double totalMinutes() const
108  { return double(_span) / 1000 / 1000 / 60; }
109 
111  double totalSeconds() const
112  { return double(_span) / 1000 / 1000; }
113 
115  double totalMSecs() const
116  { return double(_span) / 1000; }
117 
119  int64_t totalUSecs() const
120  { return _span; }
121 
123  static Timespan gettimeofday();
124 
125  private:
126  int64_t _span;
127 };
128 
138 template <int64_t Resolution>
139 class WeakTimespan : public Timespan
140 {
141  public:
142  WeakTimespan()
143  : Timespan()
144  { }
145 
146  WeakTimespan(short units)
147  : Timespan(units * Resolution)
148  { }
149 
150  WeakTimespan(unsigned short units)
151  : Timespan(units * Resolution)
152  { }
153 
154  WeakTimespan(int units)
155  : Timespan(units * Resolution)
156  { }
157 
158  WeakTimespan(unsigned int units)
159  : Timespan(units * Resolution)
160  { }
161 
162  WeakTimespan(long units)
163  : Timespan(units * Resolution)
164  { }
165 
166  WeakTimespan(unsigned long units)
167  : Timespan(units * Resolution)
168  { }
169 
170 #ifdef HAVE_LONG_LONG
171  WeakTimespan(long long units)
172  : Timespan(units * Resolution)
173  { }
174 #endif
175 
176 #ifdef HAVE_UNSIGNED_LONG_LONG
177  WeakTimespan(unsigned long long units)
178  : Timespan(units * Resolution)
179  { }
180 #endif
181 
182  WeakTimespan(float units)
183  : Timespan(units * Resolution)
184  { }
185 
186  WeakTimespan(double units)
187  : Timespan(units * Resolution)
188  { }
189 
190  WeakTimespan(long double units)
191  : Timespan(units * Resolution)
192  { }
193 
194  WeakTimespan(const Timespan& ts)
195  : Timespan(ts)
196  { }
197 
198  WeakTimespan(long seconds, long microseconds)
199  : Timespan(seconds, microseconds)
200  { }
201 
202  WeakTimespan(int days, int hours, int minutes, int seconds, int microseconds)
203  : Timespan(days, hours, minutes, seconds, microseconds)
204  { }
205 
206  operator double() const
207  { return totalUSecs() / static_cast<double>(Resolution); }
208 
209  int64_t ceil() const
210  {
211  int64_t r = totalUSecs();
212  return r % Resolution == 0 || r < 0 ? r / Resolution
213  : r / Resolution + 1;
214  }
215 };
216 
222 template <>
223 class WeakTimespan<1> : public Timespan
224 {
225  public:
226  WeakTimespan()
227  : Timespan()
228  { }
229 
230  WeakTimespan(short units)
231  : Timespan(units)
232  { }
233 
234  WeakTimespan(unsigned short units)
235  : Timespan(units)
236  { }
237 
238  WeakTimespan(int units)
239  : Timespan(units)
240  { }
241 
242  WeakTimespan(unsigned int units)
243  : Timespan(units)
244  { }
245 
246  WeakTimespan(long units)
247  : Timespan(units)
248  { }
249 
250  WeakTimespan(unsigned long units)
251  : Timespan(units)
252  { }
253 
254 #ifdef HAVE_LONG_LONG
255  WeakTimespan(long long units)
256  : Timespan(units)
257  { }
258 #endif
259 
260 #ifdef HAVE_UNSIGNED_LONG_LONG
261  WeakTimespan(unsigned long long units)
262  : Timespan(units)
263  { }
264 #endif
265 
266  WeakTimespan(float units)
267  : Timespan(units)
268  { }
269 
270  WeakTimespan(double units)
271  : Timespan(units)
272  { }
273 
274  WeakTimespan(long double units)
275  : Timespan(units)
276  { }
277 
278  WeakTimespan(const Timespan& ts)
279  : Timespan(ts)
280  { }
281 
282  operator int64_t() const
283  { return totalUSecs(); }
284 
285 };
286 
288 namespace tshelper
289 {
290  void get(std::istream& in, Timespan& ts, uint64_t res);
291  void get(const SerializationInfo& si, Timespan& ts, uint64_t res);
292 }
294 
331 typedef WeakTimespan<1> Microseconds;
332 typedef WeakTimespan<int64_t(1000)> Milliseconds;
333 typedef WeakTimespan<int64_t(1000)*1000> Seconds;
334 typedef WeakTimespan<int64_t(1000)*1000*60> Minutes;
335 typedef WeakTimespan<int64_t(1000)*1000*60*60> Hours;
336 typedef WeakTimespan<int64_t(1000)*1000*60*60*24> Days;
337 
338 inline Timespan operator * (const Timespan& d, double fac)
339 {
340  return Timespan(d.totalUSecs() * fac);
341 }
342 
343 inline Timespan operator * (double fac, const Timespan& d)
344 {
345  return Timespan(d.totalUSecs() * fac);
346 }
347 
348 inline Timespan operator / (const Timespan& d, double fac)
349 {
350  return Timespan(d.totalUSecs() / fac);
351 }
352 
354 std::ostream& operator<< (std::ostream& out, const Timespan& ts);
355 
357 std::ostream& operator<< (std::ostream& out, const Microseconds& ts);
358 
360 std::ostream& operator<< (std::ostream& out, const Milliseconds& ts);
361 
363 std::ostream& operator<< (std::ostream& out, const Seconds& ts);
364 
366 std::ostream& operator<< (std::ostream& out, const Minutes& ts);
367 
369 std::ostream& operator<< (std::ostream& out, const Hours& ts);
370 
372 std::ostream& operator<< (std::ostream& out, const Days& ts);
373 
375 inline std::istream& operator>> (std::istream& in, Timespan& ts)
376 {
377  tshelper::get(in, ts, 1);
378  return in;
379 }
380 
382 inline std::istream& operator>> (std::istream& in, Microseconds& ts)
383 {
384  tshelper::get(in, ts, 1);
385  return in;
386 }
387 
389 inline std::istream& operator>> (std::istream& in, Milliseconds& ts)
390 {
391  tshelper::get(in, ts, int64_t(1000));
392  return in;
393 }
394 
396 inline std::istream& operator>> (std::istream& in, Seconds& ts)
397 {
398  tshelper::get(in, ts, int64_t(1000)*1000);
399  return in;
400 }
401 
403 inline std::istream& operator>> (std::istream& in, Minutes& ts)
404 {
405  tshelper::get(in, ts, int64_t(1000)*1000*60);
406  return in;
407 }
408 
410 inline std::istream& operator>> (std::istream& in, Hours& ts)
411 {
412  tshelper::get(in, ts, int64_t(1000)*1000*60*60);
413  return in;
414 }
415 
417 inline std::istream& operator>> (std::istream& in, Days& ts)
418 {
419  tshelper::get(in, ts, int64_t(1000)*1000*60*60*24);
420  return in;
421 }
422 
423 inline void operator >>=(const SerializationInfo& si, Timespan& timespan)
424 {
425  tshelper::get(si, timespan, 1);
426 }
427 
428 inline void operator >>=(const SerializationInfo& si, Milliseconds& timespan)
429 {
430  tshelper::get(si, timespan, int64_t(1000));
431 }
432 
433 inline void operator >>=(const SerializationInfo& si, Seconds& timespan)
434 {
435  tshelper::get(si, timespan, int64_t(1000)*1000);
436 }
437 
438 inline void operator >>=(const SerializationInfo& si, Minutes& timespan)
439 {
440  tshelper::get(si, timespan, int64_t(1000)*1000*60);
441 }
442 
443 inline void operator >>=(const SerializationInfo& si, Hours& timespan)
444 {
445  tshelper::get(si, timespan, int64_t(1000)*1000*60*60);
446 }
447 
448 inline void operator >>=(const SerializationInfo& si, Days& timespan)
449 {
450  tshelper::get(si, timespan, int64_t(1000)*1000*60*60*24);
451 }
452 
453 void operator <<=(SerializationInfo& si, const Timespan& timespan);
454 
455 void operator <<=(SerializationInfo& si, const Microseconds& timespan);
456 
457 void operator <<=(SerializationInfo& si, const Milliseconds& timespan);
458 
459 void operator <<=(SerializationInfo& si, const Seconds& timespan);
460 
461 void operator <<=(SerializationInfo& si, const Minutes& timespan);
462 
463 void operator <<=(SerializationInfo& si, const Hours& timespan);
464 
465 void operator <<=(SerializationInfo& si, const Days& timespan);
466 
467 } // namespace cxxtools
468 
469 #endif