Class to contain a time.
More...
#include <Time.hpp>
Public Member Functions |
| Time (clockid_t clockType=CLOCK_MONOTONIC) |
| Default constructor.
|
virtual | ~Time () |
void | add (time_t sec, long nsec) |
| Method to add seconds and nseconds to the current value.
|
void | set (time_t sec, long nsec) |
| Method to set the time to a specific value.
|
void | resetToCurrentTime () |
| Method to reset the class to the current time.
|
bool | operator< (const Time &ref) const |
| Operator to compare two values.
|
bool | operator> (const Time &ref) const |
| Operator to compare two values.
|
bool | operator== (const Time &ref) const |
| Operator to compare two values.
|
void | getResolution (time_t *sec, long *nsec) |
| Method to get timer resolition.
|
time_t | getSeconds () const |
| Method to get the number of seconds.
|
long | getNSeconds () const |
| Method to get the number of microseconds.
|
Detailed Description
Class to contain a time.
This class wraps a time, with a resolution of nanoseconds. It is useful to get the current time and to make comparisons between times.
Definition at line 34 of file Time.hpp.
Constructor & Destructor Documentation
Time |
( |
clockid_t |
clockType = CLOCK_MONOTONIC | ) |
|
Default constructor.
It initializes the class with the current time (calling clock_gettime()
- Parameters
-
clockType,: | the type of the clock:
-
CLOCK_REALTIME: System-wide time.
-
CLOCK_MONOTONIC (default): Monotonic time since some unspecified starting point. It cannot be set.
-
CLOCK_PROCESS_CPUTIME_ID: Per-process cpu time.
-
CLOCK_THREAD_CPUTIME_ID: Per-thread cpu time.
This parameter affects the initial value and the result of resetToCurrentTime() |
- Exceptions
-
Definition at line 43 of file Time.cpp.
Member Function Documentation
void add |
( |
time_t |
sec, |
|
|
long |
nsec |
|
) |
| |
Method to add seconds and nseconds to the current value.
- Parameters
-
sec | Number of seconds to be added |
usec | Number of nseconds to be added |
Definition at line 54 of file Time.cpp.
{
time_.tv_nsec += nsec;
time_.tv_sec += sec;
}
long getNSeconds |
( |
| ) |
const |
|
inline |
Method to get the number of microseconds.
- Returns
- Number of microseconds
Definition at line 71 of file Time.hpp.
{
return time_.tv_nsec;
}
void getResolution |
( |
time_t * |
sec, |
|
|
long * |
nsec |
|
) |
| |
Method to get timer resolition.
- Parameters
-
sec,: | number of seconds of resolution. |
nsec,: | number of seconds of resolution. |
- Exceptions
-
std::runtime_error | in case of error |
Definition at line 129 of file Time.cpp.
{
timespec ret;
throw std::runtime_error("Can't get time resoultion");
*sec = ret.tv_sec;
*nsec = ret.tv_nsec;
}
time_t getSeconds |
( |
| ) |
const |
|
inline |
Method to get the number of seconds.
- Returns
- Number of seconds
Definition at line 62 of file Time.hpp.
bool operator< |
( |
const Time & |
ref | ) |
const |
Operator to compare two values.
Definition at line 87 of file Time.cpp.
{
if (time_.tv_sec < ref.time_.tv_sec)
return true;
else if ((time_.tv_sec == ref.time_.tv_sec) &&
(time_.tv_nsec < ref.time_.tv_nsec))
return true;
return false;
}
bool operator== |
( |
const Time & |
ref | ) |
const |
Operator to compare two values.
- Returns
- true if the times expressed by the classes are equal; false otherwise
Definition at line 114 of file Time.cpp.
{
if ((time_.tv_sec == ref.time_.tv_sec) &&
(time_.tv_nsec == ref.time_.tv_nsec))
return true;
return false;
}
bool operator> |
( |
const Time & |
ref | ) |
const |
Operator to compare two values.
Definition at line 100 of file Time.cpp.
{
if (time_.tv_sec > ref.time_.tv_sec)
return true;
else if ((time_.tv_sec == ref.time_.tv_sec) &&
(time_.tv_nsec > ref.time_.tv_nsec))
return true;
return false;
}
void resetToCurrentTime |
( |
| ) |
|
Method to reset the class to the current time.
This method sets the time equal to the value returned by gettimeofday().
- Exceptions
-
std::runtime_error | in case of error |
Definition at line 78 of file Time.cpp.
{
throw std::runtime_error("Can't get current time");
}
void set |
( |
time_t |
sec, |
|
|
long |
nsec |
|
) |
| |
Method to set the time to a specific value.
- Parameters
-
sec | Number of seconds to be set |
usec | Number of nseconds to be set |
Definition at line 66 of file Time.cpp.
{
time_.tv_nsec = nsec;
time_.tv_sec = sec;
}
Member Data Documentation
Type of clock.
Definition at line 44 of file Time.hpp.
The documentation for this class was generated from the following files:
- /home/cloud/work/onposix-code/include/Time.hpp
- /home/cloud/work/onposix-code/src/Time.cpp