General IRI utilities
Public Member Functions
CTime Class Reference

Implementation of a time class and operations. More...

#include <ctime.h>

Public Member Functions

 CTime ()
 Constructor. More...
 
 CTime (double relative)
 Constructor with parameters. More...
 
 ~CTime ()
 Destructor. More...
 
Get time functions
unsigned long seconds (void)
 Get Seconds. More...
 
unsigned long nanoseconds (void)
 Get Nanoseconds. More...
 
double getTimeInSeconds (void)
 Get time in seconds. More...
 
double getTimeInMilliseconds (void)
 Get time in milliseconds. More...
 
long getTimeInMicroseconds (void)
 Get time in microseconds. More...
 
timespec getTimeInTimespec (void)
 Get time in a timespec strcuture. More...
 
timeval getTimeInTimeval (void)
 Get time in a timeval strcuture. More...
 
time_t getTimeInTime_t (void)
 Get time in time_t. More...
 
CTime getRef ()
 Get the reference internal time. More...
 
void useRef (bool use)
 sets if use or not the reference internal time More...
 
bool isRefUsed ()
 Get if is used or not the reference internal time. More...
 
Set Time
void set (double milliseconds=-1.0)
 Sets the internal time. More...
 
void setRef ()
 Sets the internal reference time. More...
 
void resetRef ()
 Resets the reference time to the epoch. More...
 
CTime diff (CTime &t1, CTime &t0)
 

Static Public Member Functions

Conversion functions
static double timespecToMs (timespec t)
 conversion from timespec to milliseconds More...
 
static timespec msToTimespec (double ms)
 conversion from milliseconds to timespec More...
 

Operators

CTime operator- (CTime &t)
 Calculates time difference. More...
 
CTime operator+ (CTime &t)
 Calculates time addition. More...
 
CTime operator/ (int div)
 Calculates division by integer. More...
 
bool operator== (CTime &t)
 Comparison operator. More...
 
bool operator!= (CTime &t)
 Comparison operator. More...
 
bool operator> (CTime &t)
 Comparison > operator. More...
 
bool operator< (CTime &t)
 Comparison < operator. More...
 
bool operator>= (CTime &t)
 Comparison >= operator. More...
 
bool operator<= (CTime &t)
 Comparison <= operator. More...
 
std::string getString (void)
 Gives a formatted string of this time. More...
 
void setFormat (ctimeformat format)
 Sets output string format. More...
 
ctimeformat getFormat ()
 Gets output string format. More...
 
std::ostream & operator<< (std::ostream &o, CTime &t)
 outputs time into a ostream More...
 

Detailed Description

This class is designed to easily handle time within a program. It provides to the user a friendly interface in miliseconds and allows him to get the current time or convert any given time in any of the system dependant structures.

Each CTime object has a time since the Epoch. This class can also work with user provided times which are not referenced to the previous zero time, but can be used as timeouts or to monitor time between events.

Also, given a time in any format it provides a set of static functions to convert from that format to any other format in order to simplify the time handling issues without having to create an object of this class.

Finally, some basic arithmetic operators are provided (addition, substraction, ..) as well as relational operators are provided to perform basic operations on objects of this class.

bug.png
Author
Martí Morta (mmort.nosp@m.a@ir.nosp@m.i.upc.nosp@m..edu)
Examples:
test_time.cpp.

Definition at line 75 of file ctime.h.

Constructor & Destructor Documentation

CTime::CTime ( )

This constructor creates a new object of this class initialized with the current time.

Definition at line 30 of file ctime.cpp.

CTime::CTime ( double  relative)

This constructor creates a new object of this class initialized with the value specified by the user. This value is a relative time in miliseconds.

Parameters
relativethe desired relative time in miliseconds. This value must be always positive since it represents time.

Definition at line 37 of file ctime.cpp.

CTime::~CTime ( )

This destructor frees all the resources associated with the object.

Definition at line 44 of file ctime.cpp.

Member Function Documentation

unsigned long CTime::seconds ( void  )

This function returns the internal seconds

Returns
the internal seconds
Examples:
test_time.cpp.

Definition at line 52 of file ctime.cpp.

unsigned long CTime::nanoseconds ( void  )

This function returns the internal nanoseconds

Returns
the internal nanoseconds

Definition at line 57 of file ctime.cpp.

double CTime::getTimeInSeconds ( void  )

This function returns the internal time as a double seconds.nanoseconds. If this value is very high the resolution after the comma could be changed

Returns
the internal time in seconds

Definition at line 64 of file ctime.cpp.

double CTime::getTimeInMilliseconds ( void  )

This function returns the internal time in milliseconds with microseconds resolution.

Returns
the internal time in milliseconds with microseconds resolution (only the first 3 decimal places have real meaning)

Definition at line 70 of file ctime.cpp.

long CTime::getTimeInMicroseconds ( void  )

This function returns the internal time in microseconds as an integer.

Returns
the internal time in microseconds.

Definition at line 75 of file ctime.cpp.

timespec CTime::getTimeInTimespec ( void  )

This function returns the internal time using a timespec Linux structure. This may prove usefulll when using some Linux system functions which require this kind of structure.

The format of the timespec structure is as follows:

time_t tv_sec
long tv_nsec
Returns
the internal time within a timespec strucure.

Definition at line 82 of file ctime.cpp.

timeval CTime::getTimeInTimeval ( void  )

This function returns the internalCTime CTime::zero; time using a timeval Linux structure. This may prove usefulll when using some Linux system functions which require this kind of structure.

The format of the timeval structure is as follows:

time_t tv_sec s
suseconds_t tv_usec
Returns
the internal time within a timeval strucure.

Definition at line 92 of file ctime.cpp.

Referenced by CEventServer::wait_all(), and CEventServer::wait_first().

Here is the caller graph for this function:

time_t CTime::getTimeInTime_t ( void  )

This function returns the internal time as a time_t strcuture. This may prove usefulll when using some Linux system functions which require this kind of structure.

Returns
the internal time as a time_t structure

Definition at line 102 of file ctime.cpp.

CTime CTime::getRef ( )

This function returns the internal time as a CTime object

Returns
the internal ref time as a CTime object
Examples:
test_time.cpp.

Definition at line 147 of file ctime.cpp.

void CTime::useRef ( bool  use)
Parameters
use

Definition at line 158 of file ctime.cpp.

bool CTime::isRefUsed ( )
Returns
use of the internal ref time

Definition at line 170 of file ctime.cpp.

void CTime::set ( double  milliseconds = -1.0)

This function sets the internal time of the object once it is already created. If no parameter is passed (or a negative value is provided), the current computer time is used. Otherwise, the time provided by the user in milliseconds is used to initialize the internal time.

Parameters
millisecondsthe desired time in milliseconds to initialize the object. If this parameter is not provided or it has a negative value, the current computer time will be used.
Examples:
test_time.cpp.

Definition at line 110 of file ctime.cpp.

Referenced by CLog::log(), CLog::log_vector(), CEventServer::wait_all(), and CEventServer::wait_first().

Here is the caller graph for this function:

void CTime::setRef ( )

This function sets the internal time of the object.

Definition at line 141 of file ctime.cpp.

void CTime::resetRef ( )

This function resets the reference internal time of the object to the epoch time.

Definition at line 152 of file ctime.cpp.

CTime CTime::operator- ( CTime t)

This operator computes the difference between two time objects. The resulting time object may have a negative value because it represents a difference.

Parameters
ta second time object used to perform the desired arithmetic operation.
Returns
a new object initialized with the time difference between the object itself and the one provided as an argument.
return = this - t2

Definition at line 199 of file ctime.cpp.

CTime CTime::operator+ ( CTime t)

This operator computes the addition of two time objects. Depending on the values of the time objects to add, it is possible the resulting value overflows the internal attributes. In this case an exception is thrown.

Parameters
ta second time object used to perform the desired arithmetic operation.
Returns
a new object initialized with the time addition of the object itself and the one provided as an argument.
return = this + t

Definition at line 208 of file ctime.cpp.

CTime CTime::operator/ ( int  div)

This operator computes the division of the time object by an integer value. It can be usefull when computing averages of time objects. The value of the resulting time object is rounded to the nanoseconds.

Parameters
divan integer value used to dived the internal time value. This value can be negative, in which case the resulting time object will also have a negative value.

return a new object initialized with the time of the object itself divided by the provided integer.

Definition at line 229 of file ctime.cpp.

bool CTime::operator== ( CTime t)

This operator is used to compare two time objects. If the two values are the same it returns true, and false otherwise.

Parameters
ta second time object used to perform the comparation.
Returns
returns true if the two objects have the same value and false otherwise.

Definition at line 244 of file ctime.cpp.

bool CTime::operator!= ( CTime t)

This operator is used to compare two time objects. If the two values are the same it returns true, and false otherwise.

Parameters
ta second time object used to perform the comparation.
Returns
returns true if the two objects have different values and false otherwise.

Definition at line 249 of file ctime.cpp.

bool CTime::operator> ( CTime t)
Parameters
ta second time object used to perform the comparation.
Returns
returns t0 > t1

Definition at line 270 of file ctime.cpp.

bool CTime::operator< ( CTime t)
Parameters
ta second time object used to perform the comparation.
Returns
returns t0 < t1

Definition at line 278 of file ctime.cpp.

bool CTime::operator>= ( CTime t)
Parameters
ta second time object used to perform the comparation.
Returns
returns t0 >= t1

Definition at line 254 of file ctime.cpp.

bool CTime::operator<= ( CTime t)
Parameters
ta second time object used to perform the comparation.
Returns
returns t0 <= t1

Definition at line 262 of file ctime.cpp.

std::string CTime::getString ( void  )

This function is similar to the << operator, but it returns a formatted string instead of outputting it into an ostream object. In this case, the output format is also determined by the current value of the print_format attribute.

Available formats are:

Seconds Nanoseconds ( ctf_secnano )

1265731752 81195677

Date,Time ( ctf_datetime )

2010-02-09,17:09:12

Milliseconds ( ctf_ms )

1265731752.812
Returns
a string with the time information in the desired format.

Definition at line 293 of file ctime.cpp.

Referenced by CLog::log(), and CLog::log_vector().

Here is the caller graph for this function:

void CTime::setFormat ( ctimeformat  format)

This function sets the desired ouptu format for the getString() function and the << operator.

Parameters
formatan identifier of the desired format. It must belong to the ctimeformat enummeration type. Currently, the only supported formats are:
  • ctf_secnano -> for seconds and nanoseconds
  • ctf_datatime -> for a strign with the human redable date and time
  • ctf_ms -> for miliseconds

For more information see the documentation on the ctimeformat enummeration type.

Definition at line 351 of file ctime.cpp.

Referenced by CLog::set_time_format().

Here is the caller graph for this function:

ctimeformat CTime::getFormat ( )

This function return the current output format being used in the getString() function and the << operator.

Returns
the current output format, which is one of the ctimeformat enummeration type. See the documentation on that type for more information.

Definition at line 356 of file ctime.cpp.

Referenced by CLog::get_time_format().

Here is the caller graph for this function:

double CTime::timespecToMs ( timespec  t)
static

This function converts a time in a timespec structure into a double value with the same time in miliseconds. This function is static, so it can be used even if there exist no object of this class.

Parameters
ta valid timespec structure already initialized.
Returns
the time in the timespec structure represented in milliseconds.

Definition at line 364 of file ctime.cpp.

timespec CTime::msToTimespec ( double  ms)
static

This function converts a time in milliseconds into a timespec structure (seconds and nanoseconds). This function is static, so it can be used even if there exist no object of this class.

Parameters
msthe time in milliseconds.
Returns
a new timespec structure initialized with the time passed as argument.

Definition at line 370 of file ctime.cpp.

Friends And Related Function Documentation

std::ostream& operator<< ( std::ostream &  o,
CTime t 
)
friend

This function is used to output formatted time information into a ostream object, such as the standard output or a file. This function uses the current value of the print_format attribute to provided teh desired output format.

Use the setFormat() and getFormat() functions to change and check the current output format respectively. See the documentation on the ctimeformat enummeration for information on the supported formats.

Parameters
othe ostream object to ouput the formatted information. This can be the standard output or a file.
ta time object whose time information is to be formatted and output into the ostream obejct.
Returns
the same ostream object passed as an argument.

Definition at line 286 of file ctime.cpp.


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