Time.h
1/*
2 * Copyright (C) 2006-2008 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_TIME_H
30#define PT_TIME_H
31
32#include <Pt/Api.h>
33#include <Pt/Types.h>
34#include <Pt/String.h>
35#include <Pt/Timespan.h>
36#include <string>
37#include <stdexcept>
38
39namespace Pt {
40
42
50class PT_API InvalidTime : public std::runtime_error
51{
52 public:
55
57 ~InvalidTime() throw()
58 {}
59};
60
62PT_API std::string timeToString(const Time& t);
63
65PT_API Time timeFromString(const std::string& s);
66
97class Time
98{
99 public:
100 static const uint32_t MaxHours = 23;
101 static const uint32_t HoursPerDay = 24;
102 static const uint32_t MaxMinutes = 59;
103 static const uint32_t MinutesPerHour = 60;
104 static const uint32_t MinutesPerDay = 1440;
105 static const uint32_t MaxSeconds = 59;
106 static const uint32_t SecondsPerDay = 86400;
107 static const uint32_t SecondsPerHour = 3600;
108 static const uint32_t SecondsPerMinute = 60;
109 static const uint32_t MSecsPerDay = 86400000;
110 static const uint32_t MSecsPerHour = 3600000;
111 static const uint32_t MSecsPerMinute = 60000;
112 static const uint32_t MSecsPerSecond = 1000;
113
114 public:
118 : _msecs(0)
119 {}
120
132 inline Time(unsigned h, unsigned m, unsigned s = 0, unsigned ms = 0)
133 : _msecs(0)
134 {
135 set(h, m, s, ms);
136 }
137
140 Time& operator=(const Time& other)
141 { _msecs=other._msecs; return *this; }
142
145 unsigned hour() const
146 {
147 return _msecs / MSecsPerHour;
148 }
149
152 unsigned minute() const
153 {
154 return (_msecs % MSecsPerHour) / MSecsPerMinute;
155 }
156
159 unsigned second() const
160 {
161 return (_msecs / 1000) % SecondsPerMinute;
162 }
163
166 unsigned msec() const
167 {
168 return _msecs % 1000;
169 }
170
173 inline uint32_t toMSecs() const
174 { return _msecs; }
175
179 { _msecs = msecs; }
180
192 void set(unsigned h, unsigned m, unsigned s, unsigned ms = 0)
193 {
194 if ( ! isValid(h, m, s , ms) )
195 {
196 throw InvalidTime();
197 }
198
199 _msecs = (h * SecondsPerHour + m * SecondsPerMinute + s) * 1000 + ms;
200 }
201
206 void get(unsigned& h, unsigned& m, unsigned& s, unsigned& ms) const
207 {
208 h = hour();
209 m = minute();
210 s = second();
211 ms = msec();
212 }
213
219 Time addSecs(int secs) const
220 {
221 return addMSecs(secs * 1000);
222 }
223
226 int secsUntil(const Time &t) const
227 {
228 return static_cast<int>( msecsUntil(t) / 1000 );
229 }
230
236 inline Time addMSecs(Pt::int64_t ms) const
237 {
238 Time t;
239 if (ms < 0)
240 {
241 Pt::int64_t negdays = (MSecsPerDay - ms) / MSecsPerDay;
242 t._msecs = static_cast<uint32_t>((_msecs + ms + negdays * MSecsPerDay) % MSecsPerDay);
243 }
244 else
245 {
246 t._msecs = static_cast<uint32_t>((_msecs + ms) % MSecsPerDay);
247 }
248
249 return t;
250 }
251
255 {
256 if(t._msecs > _msecs)
257 return t._msecs - _msecs;
258
259 return MSecsPerDay - (_msecs - t._msecs);
260 }
261
264 std::string toIsoString() const
265 { return timeToString(*this); }
266
273 static Time fromIsoString(const std::string& s)
274 { return timeFromString(s); }
275
279 {
280 Pt::int64_t msecs = ( _msecs + ts.toMSecs() ) % MSecsPerDay;
281 msecs = msecs < 0 ? MSecsPerDay + msecs : msecs;
282 _msecs = static_cast<uint32_t>(msecs);
283 return *this;
284 }
285
289 {
290 Pt::int64_t msecs = ( _msecs - ts.toMSecs() ) % MSecsPerDay;
291 msecs = msecs < 0 ? MSecsPerDay + msecs : msecs;
292 _msecs = static_cast<uint32_t>(msecs);
293 return *this;
294 }
295
298 static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
299 {
300 return h < 24 && m < 60 && s < 60 && ms < 1000;
301 }
302
303 private:
304 Pt::uint32_t _msecs;
305};
306
307
312PT_API void operator >>=(const SerializationInfo& si, Time& time);
313
318PT_API void operator <<=(SerializationInfo& si, const Time& time);
319
324inline bool operator==(const Time& a, const Time& b)
325{ return a.toMSecs() == b.toMSecs(); }
326
331inline bool operator!=(const Time& a, const Time& b)
332{ return a.toMSecs() != b.toMSecs(); }
333
338inline bool operator<(const Time& a, const Time& b)
339{ return a.toMSecs() < b.toMSecs(); }
340
345inline bool operator<=(const Time& a, const Time& b)
346{ return a.toMSecs() <= b.toMSecs(); }
347
352inline bool operator>(const Time& a, const Time& b)
353{ return a.toMSecs() > b.toMSecs(); }
354
359inline bool operator>=(const Time& a, const Time& b)
360{ return a.toMSecs() >= b.toMSecs(); }
361
366inline Time operator+(const Time& time, const Timespan& ts)
367{ return time.addMSecs( ts.toMSecs() ); }
368
373inline Time operator-(const Time& time, const Timespan& ts)
374{ return time.addMSecs( -ts.toMSecs() ); }
375
380inline Timespan operator-(const Time& a, const Time& b)
381{ return Timespan( b.msecsUntil(a) * 1000 ); }
382
383} // namespace Pt
384
385#endif // PT_TIME_H
~InvalidTime()
Destructor.
Definition Time.h:57
InvalidTime()
Constructor.
Represents arbitrary types during serialization.
Definition SerializationInfo.h:59
Time & operator=(const Time &other)
Assignment operator.
Definition Time.h:140
bool operator<(const Time &a, const Time &b)
Less-than comparison operator.
Definition Time.h:338
void set(unsigned h, unsigned m, unsigned s, unsigned ms=0)
Sets the time.
Definition Time.h:192
unsigned hour() const
Returns the hour-part.
Definition Time.h:145
Time()
Construct a Time set to zero.
Definition Time.h:117
Time addSecs(int secs) const
Adds seconds to the time.
Definition Time.h:219
Time & operator+=(const Timespan &ts)
Assignment by sum operator.
Definition Time.h:278
unsigned second() const
Returns the second-part.
Definition Time.h:159
bool operator>=(const Time &a, const Time &b)
Greater-than-or-equal comparison operator.
Definition Time.h:359
unsigned msec() const
Returns the millisecond-part.
Definition Time.h:166
static Time fromIsoString(const std::string &s)
Convert from an ISO time string.
Definition Time.h:273
bool operator<=(const Time &a, const Time &b)
Less-than-or-equal comparison operator.
Definition Time.h:345
std::string toIsoString() const
Returns the time in ISO-format (hh:mm:ss.hhh).
Definition Time.h:264
bool operator==(const Time &a, const Time &b)
Equal comparison operator.
Definition Time.h:324
bool operator>(const Time &a, const Time &b)
Greater-than comparison operator.
Definition Time.h:352
Time(unsigned h, unsigned m, unsigned s=0, unsigned ms=0)
Construct from time values.
Definition Time.h:132
Timespan operator-(const Time &a, const Time &b)
Substraction operator.
Definition Time.h:380
Time operator+(const Time &time, const Timespan &ts)
Addition operator.
Definition Time.h:366
bool operator!=(const Time &a, const Time &b)
Inequal comparison operator.
Definition Time.h:331
int secsUntil(const Time &t) const
Determines seconds until another time.
Definition Time.h:226
uint32_t toMSecs() const
Converts to milliseconds.
Definition Time.h:173
static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
Returns true if values are a valid time.
Definition Time.h:298
unsigned minute() const
Returns the minute-part.
Definition Time.h:152
void setTotalMSecs(uint32_t msecs)
Sets to total milliseconds.
Definition Time.h:178
Pt::int64_t msecsUntil(const Time &t) const
Calculates the milliseconds until another time.
Definition Time.h:254
Time operator-(const Time &time, const Timespan &ts)
Substraction operator.
Definition Time.h:373
Time & operator-=(const Timespan &ts)
Assignment by difference operator.
Definition Time.h:288
Time addMSecs(Pt::int64_t ms) const
Adds milliseconds to the time.
Definition Time.h:236
void get(unsigned &h, unsigned &m, unsigned &s, unsigned &ms) const
Get the time values.
Definition Time.h:206
Represents time spans in microsecond resolution.
Definition Timespan.h:63
Timespan()
Constructs a zero Timespan.
Definition Timespan.h:73
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition Timespan.h:307
int_type int64_t
Signed 64-bit integer type.
Definition Api-Types.h:59
uint_type uint32_t
Unsigned 32-bit integer type.
Definition Api-Types.h:52
Core module.
Definition Allocator.h:33