ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
Time Class Reference

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.

Private Attributes

clockid_t clockType_

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
std::runtime_error,thrownby resetToCurrentTime()

Definition at line 43 of file Time.cpp.

: clockType_(clockType)
{
}

Here is the call graph for this function:

virtual ~Time ( )
inlinevirtual

Definition at line 47 of file Time.hpp.

{}

Member Function Documentation

void add ( time_t  sec,
long  nsec 
)

Method to add seconds and nseconds to the current value.

Parameters
secNumber of seconds to be added
usecNumber 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;
}

Here is the caller graph for this function:

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_errorin case of error

Definition at line 129 of file Time.cpp.

{
timespec ret;
if (clock_getres(clockType_, &ret) != 0)
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.

{
return time_.tv_sec;
}

Here is the caller graph for this function:

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_errorin case of error

Definition at line 78 of file Time.cpp.

{
if (clock_gettime(clockType_, &time_) != 0)
throw std::runtime_error("Can't get current time");
}

Here is the caller graph for this function:

void set ( time_t  sec,
long  nsec 
)

Method to set the time to a specific value.

Parameters
secNumber of seconds to be set
usecNumber of nseconds to be set

Definition at line 66 of file Time.cpp.

{
time_.tv_nsec = nsec;
time_.tv_sec = sec;
}

Member Data Documentation

clockid_t clockType_
private

Type of clock.

Definition at line 44 of file Time.hpp.


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