Timespan.h
1 /*
2  * Copyright (C) 2006-2013 by Marc Boris Duerner
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef Pt_Timespan_h
30 #define Pt_Timespan_h
31 
32 #include <Pt/Api.h>
33 #include <Pt/Types.h>
34 #include <limits>
35 
36 namespace Pt {
37 
62 class Timespan
63 {
64  private:
65  static const Pt::int64_t Milliseconds = 1000;
66  static const Pt::int64_t Seconds = 1000 * Timespan::Milliseconds;
67  static const Pt::int64_t Minutes = 60 * Timespan::Seconds;
68  static const Pt::int64_t Hours = 60 * Timespan::Minutes;
69  static const Pt::int64_t Days = 24 * Timespan::Hours;
70 
71  public:
74  : _span(0)
75  {}
76 
78  explicit Timespan(Pt::int64_t microseconds)
79  : _span(microseconds)
80  { }
81 
86  Timespan(long secs, long microsecs)
87  : _span(Pt::int64_t(secs)*Seconds + microsecs)
88  { }
89 
91  Timespan(int days, int hours, int minutes, int secs, int microseconds);
92 
94  Timespan(const Timespan& timespan);
95 
98  {}
99 
101  Timespan& operator=(const Timespan& timespan);
102 
104  Timespan& set(int days, int hours, int minutes, int seconds, int microseconds);
105 
110  Timespan& set(long seconds, long microseconds);
111 
114  bool isNull() const;
115 
118  void setNull();
119 
122  Timespan& operator+=(const Timespan& d);
123 
126  Timespan& operator-=(const Timespan& d);
127 
129  int days() const;
130 
132  int hours() const;
133 
135  int toHours() const;
136 
138  int minutes() const;
139 
141  int toMinutes() const;
142 
144  int seconds() const;
145 
147  int toSeconds() const;
148 
150  int msecs() const;
151 
153  Pt::int64_t toMSecs() const;
154 
156  int usecs() const
157  { return int(_span % 1000); }
158 
160  inline Pt::int64_t toUSecs() const
161  { return _span; }
162 
163  // @internal
164  inline static Pt::int64_t maxMSecs()
165  { return std::numeric_limits<Pt::int64_t>::max() / 1000; }
166 
167  private:
168  Pt::int64_t _span;
169 };
170 
175 inline bool operator ==(const Timespan& a, const Timespan& b)
176 { return a.toUSecs() == b.toUSecs(); }
177 
182 inline bool operator !=(const Timespan& a, const Timespan& b)
183 { return a.toUSecs() != b.toUSecs(); }
184 
185 
190 inline bool operator >(const Timespan& a, const Timespan& b)
191 { return a.toUSecs() > b.toUSecs(); }
192 
197 inline bool operator >=(const Timespan& a, const Timespan& b)
198 { return a.toUSecs() >= b.toUSecs(); }
199 
204 inline bool operator <(const Timespan& a, const Timespan& b)
205 { return a.toUSecs() < b.toUSecs(); }
206 
211 inline bool operator <=(const Timespan& a, const Timespan& b)
212 { return a.toUSecs() <= b.toUSecs(); }
213 
218 inline Timespan operator +(const Timespan& a, const Timespan& b)
219 { return Timespan(a.toUSecs() + b.toUSecs()); }
220 
225 inline Timespan operator -(const Timespan& a, const Timespan& b)
226 { return Timespan(a.toUSecs() - b.toUSecs()); }
227 
228 
230 {
231  _span += d._span;
232  return *this;
233 }
234 
235 
237 {
238  _span -= d._span;
239  return *this;
240 }
241 
242 
243 inline int Timespan::days() const
244 {
245  return int(_span/Days);
246 }
247 
248 
249 inline int Timespan::hours() const
250 {
251  return int((_span/Hours) % 24);
252 }
253 
254 
255 inline int Timespan::toHours() const
256 {
257  return int(_span/Hours);
258 }
259 
260 
261 inline int Timespan::minutes() const
262 {
263  return int((_span/Minutes) % 60);
264 }
265 
266 
267 inline int Timespan::toMinutes() const
268 {
269  return int(_span/Minutes);
270 }
271 
272 
273 inline int Timespan::seconds() const
274 {
275  return int((_span/Seconds) % 60);
276 }
277 
278 
279 inline int Timespan::toSeconds() const
280 {
281  return int(_span/Seconds);
282 }
283 
284 
285 inline int Timespan::msecs() const
286 {
287  return int((_span/Milliseconds) % 1000);
288 }
289 
290 
292 {
293  return _span/Milliseconds;
294 }
295 
296 
297 inline Timespan::Timespan(int d, int h, int mins, int secs, int microsecs)
298 : _span( Pt::int64_t(microsecs) +
299  Pt::int64_t(secs)*Seconds +
300  Pt::int64_t(mins)*Minutes +
301  Pt::int64_t(h)*Hours +
302  Pt::int64_t(d)*Days )
303 {
304 }
305 
306 
307 inline Timespan::Timespan(const Timespan& timespan)
308 : _span(timespan._span)
309 {
310 }
311 
312 
313 inline Timespan& Timespan::operator=(const Timespan& timespan)
314 {
315  _span = timespan._span;
316  return *this;
317 }
318 
319 
320 inline Timespan& Timespan::set(int d, int h, int mins, int secs, int microsecs)
321 {
322  _span = Pt::int64_t(microsecs) +
323  Pt::int64_t(secs)*Seconds +
324  Pt::int64_t(mins)*Minutes +
325  Pt::int64_t(h)*Hours +
326  Pt::int64_t(d)*Days;
327  return *this;
328 }
329 
330 
331 inline Timespan& Timespan::set(long secs, long microseconds)
332 {
333  _span = Pt::int64_t(secs)*Seconds + Pt::int64_t(microseconds);
334  return *this;
335 }
336 
337 
338 inline bool Timespan::isNull() const
339 {
340  return _span == 0;
341 }
342 
343 
344 inline void Timespan::setNull()
345 {
346  _span = 0;
347 }
348 
349 } // namespace Pt
350 
351 #endif // Pt_Timespan_h
Timespan & operator-=(const Timespan &d)
Assignment by difference operator.
Definition: Timespan.h:236
Timespan(Pt::int64_t microseconds)
Constructs a Timespan.
Definition: Timespan.h:78
bool isNull() const
Returns true if null.
Definition: Timespan.h:338
Represents time spans in microsecond resolution.
Definition: Timespan.h:62
Timespan & operator=(const Timespan &timespan)
Assignment operator.
Definition: Timespan.h:313
int msecs() const
Returns the number of milliseconds (0 to 999).
Definition: Timespan.h:285
Timespan(long secs, long microsecs)
Constructs a Timespan.
Definition: Timespan.h:86
int seconds() const
Returns the number of seconds (0 to 59).
Definition: Timespan.h:273
void setNull()
Sets to null.
Definition: Timespan.h:344
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
int toHours() const
Returns the total number of hours.
Definition: Timespan.h:255
int usecs() const
Returns the fractions of a millisecond in microseconds (0 to 999).
Definition: Timespan.h:156
int minutes() const
Returns the number of minutes (0 to 59).
Definition: Timespan.h:261
Timespan & operator+=(const Timespan &d)
Assignment by sum operator.
Definition: Timespan.h:229
int toMinutes() const
Returns the total number of minutes.
Definition: Timespan.h:267
int days() const
Returns the number of days.
Definition: Timespan.h:243
Timespan & set(int days, int hours, int minutes, int seconds, int microseconds)
Sets the time span.
Definition: Timespan.h:320
int hours() const
Returns the number of hours (0 to 23).
Definition: Timespan.h:249
int toSeconds() const
Returns the total number of seconds.
Definition: Timespan.h:279
~Timespan()
Destructor.
Definition: Timespan.h:97
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition: Timespan.h:291
Pt::int64_t toUSecs() const
Returns the total number of microseconds.
Definition: Timespan.h:160
Timespan()
Constructs a zero Timespan.
Definition: Timespan.h:73