PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
PTimeInterval Class Reference

This class defines an arbitrary time interval to millisecond accuracy. More...

#include <timeint.h>

Inheritance diagram for PTimeInterval:
Collaboration diagram for PTimeInterval:

Public Member Functions

Overrides from class PObject
PObjectClone () const
 Create a new copy of the time interval. More...
 
virtual Comparison Compare (const PObject &obj) const
 Rank the two time intervals. More...
 
virtual void PrintOn (ostream &strm) const
 Output the time interval to the I/O stream. More...
 
virtual void ReadFrom (istream &strm)
 Input the time interval from the I/O stream. More...
 
Comparison functions
bool operator== (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator== (long msecs) const
 
bool operator!= (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator!= (long msecs) const
 
bool operator> (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator> (long msecs) const
 
bool operator>= (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator>= (long msecs) const
 
bool operator< (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator< (long msecs) const
 
bool operator<= (const PTimeInterval &interval) const
 Compare to the two time intervals. More...
 
bool operator<= (long msecs) const
 
- Public Member Functions inherited from PObject
__inline unsigned GetTraceContextIdentifier () const
 Get PTRACE context identifier. More...
 
__inline void SetTraceContextIdentifier (unsigned id)
 
__inline void SetTraceContextIdentifier (const PObject &obj)
 
__inline void SetTraceContextIdentifier (const PObject *obj)
 
__inline void CopyTraceContextIdentifier (PObject &obj) const
 
__inline void CopyTraceContextIdentifier (PObject *obj) const
 
virtual ~PObject ()
 
__inline const char * GetClass () const
 
__inline bool IsClass (const char *name) const
 
__inline const PObjectPTraceObjectInstance () const
 
template<class CLS >
CLS * CloneAs () const
 As for Clone() but converts to specified type. More...
 
virtual PINDEX HashFunction () const
 This function yields a hash value required by the PDictionary class. More...
 
virtual Comparison CompareObjectMemoryDirect (const PObject &obj) const
 Determine the byte wise comparison of two objects. More...
 
bool operator== (const PObject &obj) const
 Compare the two objects. More...
 
bool operator!= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator< (const PObject &obj) const
 Compare the two objects. More...
 
bool operator> (const PObject &obj) const
 Compare the two objects. More...
 
bool operator<= (const PObject &obj) const
 Compare the two objects. More...
 
bool operator>= (const PObject &obj) const
 Compare the two objects. More...
 
virtual void PrintOn (ostream &strm) const
 Output the contents of the object to the stream. More...
 
virtual void ReadFrom (istream &strm)
 Input the contents of the object from the stream. More...
 

Static Public Attributes

static int64_t const MicroToNano = 1000
 
static int64_t const MilliToNano = MicroToNano*1000
 
static int64_t const SecsToNano = MilliToNano*1000
 
static int64_t const MinsToNano = SecsToNano*60
 
static int64_t const HoursToNano = MinsToNano*60
 
static int64_t const DaysToNano = HoursToNano*24
 

Protected Member Functions

virtual int64_t InternalGet () const
 
virtual void InternalSet (int64_t t)
 
- Protected Member Functions inherited from PObject
 PObject ()
 Constructor for PObject, made protected so cannot ever create one on its own. More...
 

Construction

 PTimeInterval (PInt64 millisecs=0)
 Create a new time interval object. More...
 
 PTimeInterval (PInt64 millisecs, long seconds, long minutes=0, long hours=0, int days=0)
 
 PTimeInterval (const PString &str)
 
 PTimeInterval (const PTimeInterval &other)
 
PTimeIntervaloperator= (const PTimeInterval &other)
 
static PTimeInterval NanoSeconds (int64_t nsecs, int secs=0)
 Return a PTimeInterval in nanoseconds. More...
 
static PTimeInterval MicroSeconds (int64_t usecs, int secs=0)
 Return a PTimeInterval in microseconds. More...
 
static PTimeInterval Seconds (double secs)
 Return a PTimeInterval in seconds. More...
 
static PTimeInterval Frequency (double frequency)
 Return a PTimeInterval from a frequency. More...
 

Conversion functions

enum  Formats { NormalFormat, IncludeDays, SecondsOnly, SecondsSI }
 
PString AsString (int decimals=3, Formats format=NormalFormat, int width=1) const
 Output the time interval as a string. More...
 

Access functions

typedef int IntervalType
 
PInt64 GetNanoSeconds () const
 Get the number of nanoseconds for the time interval. More...
 
void SetNanoSeconds (int64_t nsecs, int secs=0)
 Set the number of nanoseconds for the time interval. More...
 
PInt64 GetMicroSeconds () const
 Get the number of microseconds for the time interval. More...
 
void SetMicroSeconds (int64_t usecs, int secs=0)
 Set the number of microseconds for the time interval. More...
 
double GetFrequency () const
 Get time interval as a frequency. More...
 
void SetFrequency (double frequency)
 Set time interval from a frequency. More...
 
PInt64 GetMilliSeconds () const
 Get the number of milliseconds for the time interval. More...
 
void SetMilliSeconds (PInt64 msecs)
 Set the number of milliseconds for the time interval. More...
 
long GetSeconds () const
 Get the number of whole seconds for the time interval. More...
 
double GetSecondsAsDouble () const
 Get the number of seconds, and partial seconds, as floating point, for the time interval. More...
 
void SetSeconds (double secs)
 Set the number of seconds for the time interval. More...
 
long GetMinutes () const
 Get the number of whole minutes for the time interval. More...
 
int GetHours () const
 Get the number of whole hours for the time interval. More...
 
int GetDays () const
 Get the number of whole days for the time interval. More...
 
IntervalType GetInterval () const
 Get the "clamped" number of milliseconds for the time interval. More...
 
void SetInterval (PInt64 milliseconds=0, long seconds=0, long minutes=0, long hours=0, int days=0)
 Set the value of the time interval. More...
 

Operations

PTimeInterval operator- () const
 Unary minus, get negative of time interval. More...
 
PTimeInterval operator+ (const PTimeInterval &interval) const
 Add the two time intervals yielding a third time interval. More...
 
PTimeIntervaloperator+= (const PTimeInterval &interval)
 Add the second time interval to the first time interval. More...
 
PTimeInterval operator- (const PTimeInterval &interval) const
 Subtract the two time intervals yielding a third time interval. More...
 
PTimeIntervaloperator-= (const PTimeInterval &interval)
 Subtract the second time interval from the first time interval. More...
 
PTimeInterval operator* (int factor) const
 Multiply the time interval by a factor yielding a third time interval. More...
 
PTimeIntervaloperator*= (int factor)
 Multiply the time interval by a factor. More...
 
int operator/ (const PTimeInterval &smaller) const
 Divide the time interval by another interval yielding a count. More...
 
PTimeInterval operator/ (int factor) const
 Divide the time interval by a factor yielding a third time interval. More...
 
PTimeIntervaloperator/= (int factor)
 Divide the time interval by a factor. More...
 
PTimeInterval operator+ (int64_t left, const PTimeInterval &right)
 
PTimeInterval operator- (int64_t left, const PTimeInterval &right)
 
PTimeInterval operator* (int left, const PTimeInterval &right)
 
PTimeInterval operator/ (int64_t left, const PTimeInterval &right)
 

Additional Inherited Members

- Public Types inherited from PObject
enum  Comparison { LessThan = -1, EqualTo = 0, GreaterThan = 1 }
 Result of the comparison operation performed by the Compare() function. More...
 
- Static Public Member Functions inherited from PObject
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject &to, const PObject *from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject &from)
 
static __inline void CopyTraceContextIdentifier (PObject *to, const PObject *from)
 
static __inline const char * Class ()
 
static __inline const PObjectPTraceObjectInstance (const char *)
 
static __inline const PObjectPTraceObjectInstance (const PObject *obj)
 
template<typename T >
static Comparison Compare2 (T v1, T v2)
 Compare two types, returning Comparison type. More...
 
static Comparison InternalCompareObjectMemoryDirect (const PObject *obj1, const PObject *obj2, PINDEX size)
 Internal function caled from CompareObjectMemoryDirect() More...
 
- Protected Attributes inherited from PObject
unsigned m_traceContextIdentifier
 

Detailed Description

This class defines an arbitrary time interval to millisecond accuracy.

The interval can be both positive and negative.

A long int is used to store the time interval so it is limited to LONG_MAX (found in the standard C header file limits.h) milliseconds. This is approximately 596 hours for 32 bit integers.

There is a constant, PMaxTimeInterval which defines the maximum number of milliseconds that a time interval may be.

Member Typedef Documentation

Member Enumeration Documentation

Enumerator
NormalFormat 

Output as "hh:mm:ss.uuu".

IncludeDays 

Output as "d:hh:mm:ss.uuu".

SecondsOnly 

Output as "s.uuu".

SecondsSI 

Ouptut using SI units, ns, us, ms, s, though not ks, Ms etc.

Constructor & Destructor Documentation

PTimeInterval::PTimeInterval ( PInt64  millisecs = 0)

Create a new time interval object.

The time interval, in milliseconds, is the sum of all of the parameters. For example all of the following are equivalent:


              PTimeInterval(120000)
              PTimeInterval(60000, 60)
              PTimeInterval(60000, 0, 1)
              PTimeInterval(0, 60, 1)
              PTimeInterval(0, 0, 2)
Parameters
millisecsNumber of milliseconds for interval.
PTimeInterval::PTimeInterval ( PInt64  millisecs,
long  seconds,
long  minutes = 0,
long  hours = 0,
int  days = 0 
)
Parameters
millisecsNumber of milliseconds for interval.
secondsNumber of seconds for interval.
minutesNumber of minutes for interval.
hoursNumber of hours for interval.
daysNumber of days for interval.
PTimeInterval::PTimeInterval ( const PString str)
Parameters
strString representation of time interval.
PTimeInterval::PTimeInterval ( const PTimeInterval other)

Member Function Documentation

PString PTimeInterval::AsString ( int  decimals = 3,
Formats  format = NormalFormat,
int  width = 1 
) const

Output the time interval as a string.

If precision is <= 0 and format is NormalFormat then the format used will be IncludeDays, and the positive value for precision is then used as number of decimals.

Parameters
decimalsDecimals for milliseconds part
formatOutput format options
widthWidth of output
PObject* PTimeInterval::Clone ( ) const
virtual

Create a new copy of the time interval.

It is the responsibility of the called to delete the object.

Returns
new time interval on heap.

Reimplemented from PObject.

virtual Comparison PTimeInterval::Compare ( const PObject obj) const
virtual

Rank the two time intervals.

This ranks the intervals as you would expect for two integers.

Returns
EqualTo, LessThan or GreaterThan depending on their relative rank.
Parameters
objTime interval to compare against.

Reimplemented from PObject.

static PTimeInterval PTimeInterval::Frequency ( double  frequency)
static

Return a PTimeInterval from a frequency.

int PTimeInterval::GetDays ( ) const

Get the number of whole days for the time interval.

Returns
integer number of days.
double PTimeInterval::GetFrequency ( ) const

Get time interval as a frequency.

int PTimeInterval::GetHours ( ) const

Get the number of whole hours for the time interval.

Returns
integer number of hours.
IntervalType PTimeInterval::GetInterval ( ) const

Get the "clamped" number of milliseconds for the time interval.

This returns an operating system dependent value for use in system calls. For Windows, a value of PMaxTimeInterval returns INFINITE. For unix systems, PMaxTimeInterval returns -1. In both cases a negative value returns zero.

Returns
number of milliseconds.
PInt64 PTimeInterval::GetMicroSeconds ( ) const

Get the number of microseconds for the time interval.

Returns
very long integer number of microseconds.
PInt64 PTimeInterval::GetMilliSeconds ( ) const

Get the number of milliseconds for the time interval.

Returns
very long integer number of milliseconds.
long PTimeInterval::GetMinutes ( ) const

Get the number of whole minutes for the time interval.

Returns
integer number of minutes.
PInt64 PTimeInterval::GetNanoSeconds ( ) const

Get the number of nanoseconds for the time interval.

Returns
very long integer number of nanoseconds.
long PTimeInterval::GetSeconds ( ) const

Get the number of whole seconds for the time interval.

Returns
long integer number of seconds.

Referenced by PHTTPConnectionInfo::SetPersistenceTimeout().

double PTimeInterval::GetSecondsAsDouble ( ) const

Get the number of seconds, and partial seconds, as floating point, for the time interval.

Returns
number of seconds as double.
virtual int64_t PTimeInterval::InternalGet ( ) const
protectedvirtual
virtual void PTimeInterval::InternalSet ( int64_t  t)
protectedvirtual
static PTimeInterval PTimeInterval::MicroSeconds ( int64_t  usecs,
int  secs = 0 
)
static

Return a PTimeInterval in microseconds.

bool PTimeInterval::operator!= ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are not equal.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator!= ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
PTimeInterval PTimeInterval::operator* ( int  factor) const

Multiply the time interval by a factor yielding a third time interval.

Returns
the time intervals times the factor.
Parameters
factorfactor to multiply.
PTimeInterval& PTimeInterval::operator*= ( int  factor)

Multiply the time interval by a factor.

Returns
reference to time interval.
Parameters
factorfactor to multiply.
PTimeInterval PTimeInterval::operator+ ( const PTimeInterval interval) const

Add the two time intervals yielding a third time interval.

Returns
sum of the time intervals.
Parameters
intervalTime interval to add.
PTimeInterval& PTimeInterval::operator+= ( const PTimeInterval interval)

Add the second time interval to the first time interval.

Returns
reference to first time interval.
Parameters
intervalTime interval to add.
PTimeInterval PTimeInterval::operator- ( ) const

Unary minus, get negative of time interval.

Returns
difference of the time intervals.
PTimeInterval PTimeInterval::operator- ( const PTimeInterval interval) const

Subtract the two time intervals yielding a third time interval.

Returns
difference of the time intervals.
Parameters
intervalTime interval to subtract.
PTimeInterval& PTimeInterval::operator-= ( const PTimeInterval interval)

Subtract the second time interval from the first time interval.

Returns
reference to first time interval.
Parameters
intervalTime interval to subtract.
int PTimeInterval::operator/ ( const PTimeInterval smaller) const

Divide the time interval by another interval yielding a count.

Returns
the number of times the second interval occurs in the larger.
Parameters
smallerfactor to divide.
PTimeInterval PTimeInterval::operator/ ( int  factor) const

Divide the time interval by a factor yielding a third time interval.

Returns
the time intervals divided by the factor.
Parameters
factorfactor to divide.
PTimeInterval& PTimeInterval::operator/= ( int  factor)

Divide the time interval by a factor.

Returns
reference to time interval.
Parameters
factorfactor to divide.
bool PTimeInterval::operator< ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are less than.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator< ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
bool PTimeInterval::operator<= ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are less than or equal.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator<= ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
PTimeInterval& PTimeInterval::operator= ( const PTimeInterval other)
bool PTimeInterval::operator== ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are equal.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator== ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
bool PTimeInterval::operator> ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are greater than.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator> ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
bool PTimeInterval::operator>= ( const PTimeInterval interval) const

Compare to the two time intervals.

This is provided as an override to the default in PObject so that comparisons can be made to integer literals that represent milliseconds.

Returns
true if intervals are greater than or equal.
Parameters
intervalTime interval to compare.
bool PTimeInterval::operator>= ( long  msecs) const
Parameters
msecsTime interval as integer milliseconds to compare.
virtual void PTimeInterval::PrintOn ( ostream &  strm) const
virtual

Output the time interval to the I/O stream.

The output is controlled by various stream flags: ios::scientific, ios::showbase Output if < 1 then output SI notation, e.g. "234ms", >1 is as below ios::scientific, ios::noshowbase Output as per a floating point number, e.g. -314.1592 ios::fixed, ios::noshowbase Output as hours/minutes/seconds, e.g. "12:34.567". ios::fixed, ios::showbase Output as days/hours/minutes/seconds, e.g. "2d13:25:47.312141". The strm.precision() is honoured as expected, controlling number of decimals, clamped to 9. A width() greater than needed, with fillchar('0') will also be honoured, e.g. "01:23:56.678901"

Parameters
strmI/O stream to output the time interval.
virtual void PTimeInterval::ReadFrom ( istream &  strm)
virtual

Input the time interval from the I/O stream.

This expects the input to be a signed decimal integer number.

Parameters
strmI/O stream to input the time interval from.
static PTimeInterval PTimeInterval::Seconds ( double  secs)
static

Return a PTimeInterval in seconds.

void PTimeInterval::SetFrequency ( double  frequency)

Set time interval from a frequency.

Referenced by PFrequencyTime::PFrequencyTime().

void PTimeInterval::SetInterval ( PInt64  milliseconds = 0,
long  seconds = 0,
long  minutes = 0,
long  hours = 0,
int  days = 0 
)

Set the value of the time interval.

The time interval, in milliseconds, is the sum of all of the parameters. For example all of the following are equivalent:


              SetInterval(120000)
              SetInterval(60000, 60)
              SetInterval(60000, 0, 1)
              SetInterval(0, 60, 1)
              SetInterval(0, 0, 2)
Parameters
millisecondsNumber of milliseconds for interval.
secondsNumber of seconds for interval.
minutesNumber of minutes for interval.
hoursNumber of hours for interval.
daysNumber of days for interval.
void PTimeInterval::SetMicroSeconds ( int64_t  usecs,
int  secs = 0 
)

Set the number of microseconds for the time interval.

Referenced by PMicroSeconds::PMicroSeconds().

void PTimeInterval::SetMilliSeconds ( PInt64  msecs)

Set the number of milliseconds for the time interval.

void PTimeInterval::SetNanoSeconds ( int64_t  nsecs,
int  secs = 0 
)

Set the number of nanoseconds for the time interval.

Referenced by PNanoSeconds::PNanoSeconds().

void PTimeInterval::SetSeconds ( double  secs)

Set the number of seconds for the time interval.

Friends And Related Function Documentation

PTimeInterval operator* ( int  left,
const PTimeInterval right 
)
friend
PTimeInterval operator+ ( int64_t  left,
const PTimeInterval right 
)
friend
PTimeInterval operator- ( int64_t  left,
const PTimeInterval right 
)
friend
PTimeInterval operator/ ( int64_t  left,
const PTimeInterval right 
)
friend

Member Data Documentation

int64_t const PTimeInterval::DaysToNano = HoursToNano*24
static
int64_t const PTimeInterval::HoursToNano = MinsToNano*60
static
int64_t const PTimeInterval::MicroToNano = 1000
static
int64_t const PTimeInterval::MilliToNano = MicroToNano*1000
static
int64_t const PTimeInterval::MinsToNano = SecsToNano*60
static
int64_t const PTimeInterval::SecsToNano = MilliToNano*1000
static

The documentation for this class was generated from the following file: