Timespan Class Reference

#include <Pt/Timespan.h>

Represents time spans in microsecond resolution. More...

Public Member Functions

 Timespan ()
 Constructs a zero Timespan.
 
 Timespan (Pt::int64_t microseconds)
 Constructs a Timespan.
 
 Timespan (long secs, long microsecs)
 Constructs a Timespan. More...
 
 Timespan (int days, int hours, int minutes, int secs, int microseconds)
 Constructs a Timespan.
 
 Timespan (const Timespan &timespan)
 Copy Constructor.
 
 ~Timespan ()
 Destructor.
 
Timespanoperator= (const Timespan &timespan)
 Assignment operator.
 
Timespanset (int days, int hours, int minutes, int seconds, int microseconds)
 Sets the time span.
 
Timespanset (long seconds, long microseconds)
 Sets the time span. More...
 
bool isNull () const
 Returns true if null.
 
void setNull ()
 Sets to null.
 
Timespanoperator+= (const Timespan &d)
 Assignment by sum operator.
 
Timespanoperator-= (const Timespan &d)
 Assignment by difference operator.
 
int days () const
 Returns the number of days.
 
int hours () const
 Returns the number of hours (0 to 23).
 
int toHours () const
 Returns the total number of hours.
 
int minutes () const
 Returns the number of minutes (0 to 59).
 
int toMinutes () const
 Returns the total number of minutes.
 
int seconds () const
 Returns the number of seconds (0 to 59).
 
Pt::int64_t toSeconds () const
 Returns the total number of seconds.
 
int msecs () const
 Returns the number of milliseconds (0 to 999).
 
Pt::int64_t toMSecs () const
 Returns the total number of milliseconds.
 
int usecs () const
 Returns the fractions of a millisecond in microseconds (0 to 999).
 
Pt::int64_t toUSecs () const
 Returns the total number of microseconds.
 

Static Public Member Functions

static Timespan fromSecs (Pt::int64_t s)
 Contructa Timespan from seconds.
 
static Timespan fromMSecs (Pt::int64_t ms)
 Contructa Timespan from milli seconds.
 
static Timespan fromUSecs (Pt::int64_t us)
 Contructa Timespan from micro seconds.
 

Related Functions

bool operator== (const Timespan &a, const Timespan &b)
 Equal comparison operator.
 
bool operator!= (const Timespan &a, const Timespan &b)
 Inequal comparison operator.
 
bool operator> (const Timespan &a, const Timespan &b)
 Greater-than comparison operator.
 
bool operator>= (const Timespan &a, const Timespan &b)
 Greater-than-or-equal comparison operator.
 
bool operator< (const Timespan &a, const Timespan &b)
 Less-than comparison operator.
 
bool operator<= (const Timespan &a, const Timespan &b)
 Less-than-or-equal comparison operator.
 
Timespan operator+ (const Timespan &a, const Timespan &b)
 Addition operator.
 
Timespan operator- (const Timespan &a, const Timespan &b)
 Substraction operator.
 

Detailed Description

Time intervals can be represented by Pt::Timespan objects with microsecond accuracy. It is often the result of the calculations involving Pt::Date, Pt::Time ot Pt::DateTime. A Pt::Timespan can be constructed from the number of microseconds and then be converted to other time units with toHours(), toSeconds(), toMSecs() and toUSecs(). When two Pt::Timespans are compared, the shorter one is considered less. Addition and subtraction is supported as shown in the next example:

#include <Pt/Timespan.h>
#include <iostream>
Pt::Timespan ts1(10000);
Pt::Timespan ts2(1000);
Pt::Timespan d = ts2 - ts1;
std::cout << "The difference is " << d.toSecs() << " secs" << std::endl;

Constructor & Destructor Documentation

◆ Timespan()

Timespan ( long  secs,
long  microsecs 
)

Useful for assigning from a POSIX timeval struct.

Member Function Documentation

◆ set()

Timespan & set ( long  seconds,
long  microseconds 
)

Useful for assigning from a POSIX timeval struct.

Represents time spans in microsecond resolution.
Definition: Timespan.h:63