Time Class Reference

#include <Pt/Time.h>

Time expressed in hours, minutes, seconds and milliseconds. More...

Public Member Functions

 Time ()
 Construct a Time set to zero.
 
 Time (unsigned h, unsigned m, unsigned s=0, unsigned ms=0)
 Construct from time values. More...
 
Time addMSecs (Pt::int64_t ms) const
 Adds milliseconds to the time. More...
 
Time addSecs (int secs) const
 Adds seconds to the time. More...
 
void get (unsigned &h, unsigned &m, unsigned &s, unsigned &ms) const
 Get the time values. More...
 
unsigned hour () const
 Returns the hour-part.
 
unsigned minute () const
 Returns the minute-part.
 
unsigned msec () const
 Returns the millisecond-part.
 
Pt::int64_t msecsUntil (const Time &t) const
 Calculates the milliseconds until another time.
 
Timeoperator+= (const Timespan &ts)
 Assignment by sum operator.
 
Timeoperator-= (const Timespan &ts)
 Assignment by difference operator.
 
Timeoperator= (const Time &other)
 Assignment operator.
 
unsigned second () const
 Returns the second-part.
 
int secsUntil (const Time &t) const
 Determines seconds until another time.
 
void set (unsigned h, unsigned m, unsigned s, unsigned ms=0)
 Sets the time. More...
 
void setTotalMSecs (uint32_t msecs)
 Sets to total milliseconds.
 
std::string toIsoString () const
 Returns the time in ISO-format (hh:mm:ss.hhh).
 
uint32_t toMSecs () const
 Converts to milliseconds.
 

Static Public Member Functions

static Time fromIsoString (const std::string &s)
 Convert from an ISO time string. More...
 
static bool isValid (unsigned h, unsigned m, unsigned s, unsigned ms)
 Returns true if values are a valid time.
 

Related Functions

bool operator!= (const Time &a, const Time &b)
 Inequal comparison operator.
 
Time operator+ (const Time &time, const Timespan &ts)
 Addition operator.
 
Time operator- (const Time &time, const Timespan &ts)
 Substraction operator.
 
Timespan operator- (const Time &a, const Time &b)
 Substraction operator.
 
bool operator< (const Time &a, const Time &b)
 Less-than comparison operator.
 
void operator<<= (SerializationInfo &si, const Time &time)
 Serialize a Time.
 
bool operator<= (const Time &a, const Time &b)
 Less-than-or-equal comparison operator.
 
bool operator== (const Time &a, const Time &b)
 Equal comparison operator.
 
bool operator> (const Time &a, const Time &b)
 Greater-than comparison operator.
 
bool operator>= (const Time &a, const Time &b)
 Greater-than-or-equal comparison operator.
 
void operator>>= (const SerializationInfo &si, Time &time)
 Deserialize a Time.
 

Detailed Description

A Pt::Time object contains a wall-clock time in hours, minutes, seconds amd milliseconds. It can be constructed either from the numeric time values or from a string in ISO format using fromIsoString(). The separate time values can be accessed with hour(), minute(), second() and msec(). Times can be compared and a Time is considered less, if it is earlier than another Time. Subtracting a time from another yields a Pt::Timespan as the result. Pt::Timespans can also be added or subtracted from a Time, yielding a new Time or modifying it, as shown in the following example:

#include <Pt/Date.h>
#include <iostream>
Pt::Timespan untilMidnight(const std::string& timeStr)
{
Pt::Date midnight(0 ,0, 0);
return midnight - now;
}

Pt::InvalidTime is thrown, if a time could not be constructed, for example if one of the time values is out of range. To avoid an exception, time values can be validated with isValid().

Constructor & Destructor Documentation

Time ( unsigned  h,
unsigned  m,
unsigned  s = 0,
unsigned  ms = 0 
)

Construct the time to a hour, minute, second and milli-second.

Parameters
hHours
mMinutes
sSeconds
msMilliseconds
Exceptions
InvalidTimeOne or more of the values are out of range.

Member Function Documentation

void set ( unsigned  h,
unsigned  m,
unsigned  s,
unsigned  ms = 0 
)

Sets the time to a new hour, minute, second, milli-second.

Parameters
hHours
mMinutes
sSeconds
msMilliseconds
Exceptions
InvalidTimeOne or more of the values are out of range.
void get ( unsigned &  h,
unsigned &  m,
unsigned &  s,
unsigned &  ms 
) const

Gets the hour, minute, second and millisecond parts of the time.

Time addSecs ( int  secs) const

This method does not change the time, but returns the time with the seconds added.

Time addMSecs ( Pt::int64_t  ms) const

This method does not change the time, but returns the time with the milliseconds added.

static Time fromIsoString ( const std::string &  s)
static

Interprets the passed string as a time-string in ISO-format (hh:mm:ss.hhh) and returns a Time-object. If the string is not in ISO-format, InvalidTime is thrown.