34 #include <Pt/String.h>
35 #include <Pt/Timespan.h>
41 class SerializationInfo;
62 PT_API std::string timeToString(
const Time& t);
65 PT_API Time timeFromString(
const std::string& s);
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;
132 inline Time(
unsigned h,
unsigned m,
unsigned s = 0,
unsigned ms = 0)
141 { _msecs=other._msecs;
return *
this; }
147 return _msecs / MSecsPerHour;
154 return (_msecs % MSecsPerHour) / MSecsPerMinute;
161 return (_msecs / 1000) % SecondsPerMinute;
168 return _msecs % 1000;
192 void set(
unsigned h,
unsigned m,
unsigned s,
unsigned ms = 0)
199 _msecs = (h * SecondsPerHour + m * SecondsPerMinute + s) * 1000 + ms;
206 void get(
unsigned& h,
unsigned& m,
unsigned& s,
unsigned& ms)
const
228 return static_cast<int>(
msecsUntil(t) / 1000 );
241 Pt::int64_t negdays = (MSecsPerDay - ms) / MSecsPerDay;
242 t._msecs =
static_cast<uint32_t>((_msecs + ms + negdays * MSecsPerDay) % MSecsPerDay);
246 t._msecs =
static_cast<uint32_t>((_msecs + ms) % MSecsPerDay);
256 if(t._msecs > _msecs)
257 return t._msecs - _msecs;
259 return MSecsPerDay - (_msecs - t._msecs);
265 {
return timeToString(*
this); }
274 {
return timeFromString(s); }
281 msecs = msecs < 0 ? MSecsPerDay + msecs : msecs;
282 _msecs =
static_cast<uint32_t>(msecs);
291 msecs = msecs < 0 ? MSecsPerDay + msecs : msecs;
292 _msecs =
static_cast<uint32_t>(msecs);
298 static bool isValid(
unsigned h,
unsigned m,
unsigned s,
unsigned ms)
300 return h < 24 && m < 60 && s < 60 && ms < 1000;
312 PT_API
void operator >>=(
const SerializationInfo& si, Time& time);
318 PT_API
void operator <<=(SerializationInfo& si,
const Time& time);
Time addSecs(int secs) const
Adds seconds to the time.
Definition: Time.h:219
Timespan operator-(const Time &a, const Time &b)
Substraction operator.
Definition: Time.h:380
void setTotalMSecs(uint32_t msecs)
Sets to total milliseconds.
Definition: Time.h:178
bool operator>(const Time &a, const Time &b)
Greater-than comparison operator.
Definition: Time.h:352
int secsUntil(const Time &t) const
Determines seconds until another time.
Definition: Time.h:226
Represents time spans in microsecond resolution.
Definition: Timespan.h:62
uint32_t toMSecs() const
Converts to milliseconds.
Definition: Time.h:173
Time & operator+=(const Timespan &ts)
Assignment by sum operator.
Definition: Time.h:278
~InvalidTime()
Destructor.
Definition: Time.h:57
Pt::int64_t msecsUntil(const Time &t) const
Calculates the milliseconds until another time.
Definition: Time.h:254
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 comparison operator.
Definition: Time.h:338
bool operator>=(const Time &a, const Time &b)
Greater-than-or-equal comparison operator.
Definition: Time.h:359
unsigned hour() const
Returns the hour-part.
Definition: Time.h:145
static bool isValid(unsigned h, unsigned m, unsigned s, unsigned ms)
Returns true if values are a valid time.
Definition: Time.h:298
unsigned msec() const
Returns the millisecond-part.
Definition: Time.h:166
int_type int64_t
Signed 64-bit integer type.
Definition: Types.h:48
Time operator-(const Time &time, const Timespan &ts)
Substraction operator.
Definition: Time.h:373
Time & operator=(const Time &other)
Assignment operator.
Definition: Time.h:140
std::string toIsoString() const
Returns the time in ISO-format (hh:mm:ss.hhh).
Definition: Time.h:264
Time(unsigned h, unsigned m, unsigned s=0, unsigned ms=0)
Construct from time values.
Definition: Time.h:132
Time addMSecs(Pt::int64_t ms) const
Adds milliseconds to the time.
Definition: Time.h:236
unsigned minute() const
Returns the minute-part.
Definition: Time.h:152
void set(unsigned h, unsigned m, unsigned s, unsigned ms=0)
Sets the time.
Definition: Time.h:192
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
bool operator==(const Time &a, const Time &b)
Equal comparison operator.
Definition: Time.h:324
unsigned second() const
Returns the second-part.
Definition: Time.h:159
Time & operator-=(const Timespan &ts)
Assignment by difference operator.
Definition: Time.h:288
bool operator<=(const Time &a, const Time &b)
Less-than-or-equal comparison operator.
Definition: Time.h:345
Indicates an invalid time value.
Definition: Time.h:50
Pt::int64_t toMSecs() const
Returns the total number of milliseconds.
Definition: Timespan.h:291
bool operator!=(const Time &a, const Time &b)
Inequal comparison operator.
Definition: Time.h:331
Time()
Construct a Time set to zero.
Definition: Time.h:117
Time expressed in hours, minutes, seconds and milliseconds.
Definition: Time.h:97
Time operator+(const Time &time, const Timespan &ts)
Addition operator.
Definition: Time.h:366