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
36namespace Pt {
37
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 Pt::int64_t toSeconds() const;
148
150 inline static Timespan fromSecs(Pt::int64_t s);
151
153 int msecs() const;
154
156 Pt::int64_t toMSecs() const;
157
159 inline static Timespan fromMSecs(Pt::int64_t ms);
160
162 int usecs() const
163 { return int(_span % 1000); }
164
166 inline Pt::int64_t toUSecs() const
167 { return _span; }
168
171 { return Timespan(us); }
172
173 // @internal
174 inline static Pt::int64_t maxMSecs()
175 { return std::numeric_limits<Pt::int64_t>::max() / 1000; }
176
177 private:
178 Pt::int64_t _span;
179};
180
185inline bool operator ==(const Timespan& a, const Timespan& b)
186{ return a.toUSecs() == b.toUSecs(); }
187
192inline bool operator !=(const Timespan& a, const Timespan& b)
193{ return a.toUSecs() != b.toUSecs(); }
194
195
200inline bool operator >(const Timespan& a, const Timespan& b)
201{ return a.toUSecs() > b.toUSecs(); }
202
207inline bool operator >=(const Timespan& a, const Timespan& b)
208{ return a.toUSecs() >= b.toUSecs(); }
209
214inline bool operator <(const Timespan& a, const Timespan& b)
215{ return a.toUSecs() < b.toUSecs(); }
216
221inline bool operator <=(const Timespan& a, const Timespan& b)
222{ return a.toUSecs() <= b.toUSecs(); }
223
228inline Timespan operator +(const Timespan& a, const Timespan& b)
229{ return Timespan(a.toUSecs() + b.toUSecs()); }
230
235inline Timespan operator -(const Timespan& a, const Timespan& b)
236{ return Timespan(a.toUSecs() - b.toUSecs()); }
237
238
240{
241 _span += d._span;
242 return *this;
243}
244
245
247{
248 _span -= d._span;
249 return *this;
250}
251
252
253inline int Timespan::days() const
254{
255 return int(_span/Days);
256}
257
258
259inline int Timespan::hours() const
260{
261 return int((_span/Hours) % 24);
262}
263
264
265inline int Timespan::toHours() const
266{
267 return int(_span/Hours);
268}
269
270
271inline int Timespan::minutes() const
272{
273 return int((_span/Minutes) % 60);
274}
275
276
277inline int Timespan::toMinutes() const
278{
279 return int(_span/Minutes);
280}
281
282
283inline int Timespan::seconds() const
284{
285 return int((_span/Seconds) % 60);
286}
287
288
290{
291 return _span/Seconds;
292}
293
294
296{
297 return Timespan(s * Seconds);
298}
299
300
301inline int Timespan::msecs() const
302{
303 return int((_span/Milliseconds) % 1000);
304}
305
306
308{
309 return _span/Milliseconds;
310}
311
312
314{
315 return Timespan(ms * Milliseconds);
316}
317
318
319inline Timespan::Timespan(int d, int h, int mins, int secs, int microsecs)
320: _span( Pt::int64_t(microsecs) +
321 Pt::int64_t(secs)*Seconds +
322 Pt::int64_t(mins)*Minutes +
323 Pt::int64_t(h)*Hours +
324 Pt::int64_t(d)*Days )
325{
326}
327
328
329inline Timespan::Timespan(const Timespan& timespan)
330: _span(timespan._span)
331{
332}
333
334
335inline Timespan& Timespan::operator=(const Timespan& timespan)
336{
337 _span = timespan._span;
338 return *this;
339}
340
341
342inline Timespan& Timespan::set(int d, int h, int mins, int secs, int microsecs)
343{
344 _span = Pt::int64_t(microsecs) +
345 Pt::int64_t(secs)*Seconds +
346 Pt::int64_t(mins)*Minutes +
347 Pt::int64_t(h)*Hours +
348 Pt::int64_t(d)*Days;
349 return *this;
350}
351
352
353inline Timespan& Timespan::set(long secs, long microseconds)
354{
355 _span = Pt::int64_t(secs)*Seconds + Pt::int64_t(microseconds);
356 return *this;
357}
358
359
360inline bool Timespan::isNull() const
361{
362 return _span == 0;
363}
364
365
366inline void Timespan::setNull()
367{
368 _span = 0;
369}
370
371} // namespace Pt
372
373#endif // Pt_Timespan_h
int operator-(const Date &a, const Date &b)
Subtract two dates.
Definition Date.h:468
bool operator!=(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:875
bool operator<=(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:977
bool operator==(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:824
bool operator<(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:926
bool operator>(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:1028
String operator+(const String &a, const String &b)
Concatenates two strings.
Definition Api-String.h:787
bool operator>=(const String &a, const String &b)
Compares two strings.
Definition Api-String.h:1079
Represents time spans in microsecond resolution.
Definition Timespan.h:63
Timespan()
Constructs a zero Timespan.
Definition Timespan.h:73
Pt::int64_t toSeconds() const
Returns the total number of seconds.
Definition Timespan.h:289
int days() const
Returns the number of days.
Definition Timespan.h:253
int minutes() const
Returns the number of minutes (0 to 59).
Definition Timespan.h:271
int usecs() const
Returns the fractions of a millisecond in microseconds (0 to 999).
Definition Timespan.h:162
int hours() const
Returns the number of hours (0 to 23).
Definition Timespan.h:259
static Timespan fromMSecs(Pt::int64_t ms)
Contructa Timespan from milli seconds.
Definition Timespan.h:313
static Timespan fromSecs(Pt::int64_t s)
Contructa Timespan from seconds.
Definition Timespan.h:295
Timespan & set(int days, int hours, int minutes, int seconds, int microseconds)
Sets the time span.
Definition Timespan.h:342
static Timespan fromUSecs(Pt::int64_t us)
Contructa Timespan from micro seconds.
Definition Timespan.h:170
Timespan(long secs, long microsecs)
Constructs a Timespan.
Definition Timespan.h:86
Pt::int64_t toUSecs() const
Returns the total number of microseconds.
Definition Timespan.h:166
void setNull()
Sets to null.
Definition Timespan.h:366
int seconds() const
Returns the number of seconds (0 to 59).
Definition Timespan.h:283
Timespan & operator-=(const Timespan &d)
Assignment by difference operator.
Definition Timespan.h:246
int toHours() const
Returns the total number of hours.
Definition Timespan.h:265
int toMinutes() const
Returns the total number of minutes.
Definition Timespan.h:277
bool isNull() const
Returns true if null.
Definition Timespan.h:360
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition Timespan.h:307
int msecs() const
Returns the number of milliseconds (0 to 999).
Definition Timespan.h:301
Timespan & operator+=(const Timespan &d)
Assignment by sum operator.
Definition Timespan.h:239
Timespan(Pt::int64_t microseconds)
Constructs a Timespan.
Definition Timespan.h:78
Timespan & operator=(const Timespan &timespan)
Assignment operator.
Definition Timespan.h:335
~Timespan()
Destructor.
Definition Timespan.h:97
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
Core module.
Definition Allocator.h:33