Loading...
Searching...
No Matches
ASSA::TimeVal Class Reference

`#include <TimeVal.h>`

Inheritance diagram for ASSA::TimeVal:

## Public Types

enum | { gmt , loc }

## Public Member Functions

| TimeVal ()
| Default constructor.
| TimeVal (long sec_, long msec_)
| Constructor from seconds/microseconds pair.
| TimeVal (double d_)
| Constructor from double.
| TimeVal (const timeval &tv_)
| Constructor from struct timeval.
| TimeVal (const TimeVal &tv_)
| Copy constructor.
| operator double () const
| Implicit conversion to double.
void | sec (long sec_)
| Set seconds.
long | sec (void) const
| Get secons.
void | msec (long msec_)
| Set microseconds.
long | msec (void) const
| Get microseconds.
long | millisec () const
| Convert tv_usec's microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec).
void | tz (int tz_)
| Set timezone.
int | tz (void) const
| Get timezone.
TimeVal & | operator= (const TimeVal &tv_)
TimeVal & | operator+= (const TimeVal &rhs_)
| Addition.
TimeVal & | operator-= (const TimeVal &rhs_)
| Substraction.
bool | operator< (const TimeVal &rhs_) const
| Comparison.
bool | operator== (const TimeVal &rhs_) const
| Equality.
string | fmtString (const char *fmt_=NULL) const
| Format timeval structure into readable format.
string | fmt_hh_mm_ss () const
| Format timeval structure in readable format HH:MM:SS.
string | fmt_hh_mm_ss_mls () const
| Format timeval structure in readable format HH:MM:SS.MLS.
string | fmt_mm_ss () const
| Format timeval structure in readable format MM:SS.
string | fmt_mm_ss_mls () const
| Format timeval structure in readable format MM:SS.MLS.
string | fmt_ss_mls () const
| Format timeval structure in readable format SS.MLS.
void | dump_to_log (const string &name_="") const
| Dump value of struct timeval to the log file with mask TRACE = DBG_APP15.

## Static Public Member Functions

static TimeVal | zeroTime ()
| Static that returns zero timeval: {0,0}.
static TimeVal | gettimeofday ()
| Shields off underlying OS differences in getting current time.

## Protected Member Functions

void | init (long, long, int)
| Internal initialization common to most constructors.

## Private Member Functions

void | normalize ()
| Normalization after arithmetic operation.

## Private Attributes

int | m_tz
| Time zone.

## Static Private Attributes

static TimeVal | m_zero
| Zero time value.

## Friends

TimeVal | operator+ (const TimeVal &lhs_, const TimeVal &rhs_)
| Addition.
TimeVal | operator- (const TimeVal &lhs_, const TimeVal &rhs_)
| Substraction.
bool | operator> (const TimeVal &lhs_, const TimeVal &rhs_)
| Comparison.
bool | operator!= (const TimeVal &lhs_, const TimeVal &rhs_)
| Comparison.
bool | operator<= (const TimeVal &lhs_, const TimeVal &rhs_)
| Comparison.
bool | operator>= (const TimeVal &lhs_, const TimeVal &rhs_)
| Comparison.

## Detailed Description

Definition at line 30 of file TimeVal.h.

## Member Enumeration Documentation

## anonymous enum

anonymous enum

Enumerator
gmt | GMT.
loc | Local Time Zone.

Definition at line 33 of file TimeVal.h.

{
gmt,
loc
};
ASSA::TimeVal::gmt
@ gmt
GMT.
Definition TimeVal.h:34
ASSA::TimeVal::loc
@ loc
Local Time Zone.
Definition TimeVal.h:35

## Constructor & Destructor Documentation

## TimeVal() [1/5]

ASSA::TimeVal::TimeVal | ( |  | ) |

inline

Default constructor.

Sets time to 0 sec. 0 usecs. To get current time, use TimeVal (gettimeofday());

Definition at line 195 of file TimeVal.h.

{
init (0, 0, gmt);
}
ASSA::TimeVal::init
void init(long, long, int)
Internal initialization common to most constructors.
Definition TimeVal.h:186

References gmt, and init().

Referenced by operator!=, operator+, operator+=(), operator-, operator-=(), operator<(), operator<=, operator=(), operator==(), operator>, operator>=, TimeVal(), and zeroTime().

## TimeVal() [2/5]

ASSA::TimeVal::TimeVal | ( | long | sec_,
|  | long | msec_ )

inline

Constructor from seconds/microseconds pair.

Definition at line 202 of file TimeVal.h.

{
init (sec_, msec_, gmt);
}

References gmt, and init().

## TimeVal() [3/5]

ASSA::TimeVal::TimeVal | ( | double | d_ | ) |

inline

Constructor from double.

Definition at line 209 of file TimeVal.h.

: m_tz (gmt)
{
long l = long(d_);
tv_sec = l;
tv_usec = (long) ((d_ - double(l))*1000000.0);
normalize();
}
ASSA::TimeVal::normalize
void normalize()
Normalization after arithmetic operation.
Definition TimeVal.cpp:112
ASSA::TimeVal::m_tz
int m_tz
Time zone.
Definition TimeVal.h:175

References gmt, m_tz, and normalize().

## TimeVal() [4/5]

ASSA::TimeVal::TimeVal | ( | const timeval & | tv_ | ) |

inline

Constructor from struct timeval.

Definition at line 220 of file TimeVal.h.

{
init (tv_.tv_sec, tv_.tv_usec, gmt);
}

References gmt, and init().

## TimeVal() [5/5]

ASSA::TimeVal::TimeVal | ( | const TimeVal & | tv_ | ) |

inline

Copy constructor.

Definition at line 227 of file TimeVal.h.

{
init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
}

References init(), m_tz, and TimeVal().

## Member Function Documentation

## dump_to_log()

void TimeVal::dump_to_log | ( | const string & | name_ = "" | ) | const

Dump value of struct timeval to the log file with mask TRACE = DBG_APP15.

Definition at line 226 of file TimeVal.cpp.

{
static const char self []="TimeVal::dump_to_log"; trace(self);
if (Logger::get_instance ()->group_enabled (REACT))
{
DL((REACT,"=== TimeVal %s ===\n", var_name_.c_str ()));
DL((REACT,"MM:SS:MLS = %s\n", fmt_mm_ss_mls ().c_str ()));
DL((REACT,"tv_sec = %d, tv_msec = %d, tv_mls = %d\n",
sec (), msec (), millisec ()));
DL((REACT,"(double) = %7.4f\n", double (*this)));
DL((REACT,"==================\n"));
}
}
trace
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition Logger.h:429
DL
#define DL(X)
A macro for writing debug message to the Logger.
Definition Logger.h:273
ASSA::Singleton< Logger >::get_instance
static Logger * get_instance()
Definition Singleton.h:47
ASSA::TimeVal::fmt_mm_ss_mls
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition TimeVal.cpp:189
ASSA::TimeVal::millisec
long millisec() const
Convert tv_usec's microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec).
Definition TimeVal.h:241
ASSA::TimeVal::sec
void sec(long sec_)
Set seconds.
Definition TimeVal.h:64
ASSA::TimeVal::msec
void msec(long msec_)
Set microseconds.
Definition TimeVal.h:70
ASSA::REACT
@ REACT
Class Reactor/PrioriyQueue messages.
Definition LogMask.h:39

References DL, fmt_mm_ss_mls(), ASSA::Singleton< Logger >::get_instance(), millisec(), msec(), ASSA::REACT, sec(), and trace.

## fmt_hh_mm_ss()

string ASSA::TimeVal::fmt_hh_mm_ss | ( |  | ) | const

inline

Format timeval structure in readable format HH:MM:SS.

Definition at line 247 of file TimeVal.h.

{
return fmtString ("%T");
}
ASSA::TimeVal::fmtString
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition TimeVal.cpp:146

References fmtString().

## fmt_hh_mm_ss_mls()

string TimeVal::fmt_hh_mm_ss_mls | ( |  | ) | const

Format timeval structure in readable format HH:MM:SS.MLS.

Definition at line 169 of file TimeVal.cpp.

{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%H:%M:%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}

References gmt, m_tz, and millisec().

## fmt_mm_ss()

string ASSA::TimeVal::fmt_mm_ss | ( |  | ) | const

inline

Format timeval structure in readable format MM:SS.

Definition at line 254 of file TimeVal.h.

{
return fmtString ("%M:%S");
}

References fmtString().

## fmt_mm_ss_mls()

string TimeVal::fmt_mm_ss_mls | ( |  | ) | const

Format timeval structure in readable format MM:SS.MLS.

Definition at line 188 of file TimeVal.cpp.

{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%M:%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}

References gmt, m_tz, and millisec().

Referenced by dump_to_log().

## fmt_ss_mls()

string TimeVal::fmt_ss_mls | ( |  | ) | const

Format timeval structure in readable format SS.MLS.

Definition at line 207 of file TimeVal.cpp.

{
struct tm ct;
char buf [80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
strftime (buf, 80, "%S", &ct);
sprintf (buf + strlen(buf), ".%03ld", millisec ());
return string (buf);
}

References gmt, m_tz, and millisec().

## fmtString()

string TimeVal::fmtString | ( | const char * | fmt_ = NULL | ) | const

Format timeval structure into readable format.

Default format is CCYY/DDD HH:MM:SS.MMM which is de fasco for the software. To get something different, pass fmt_ format string as specified by strftime(3). Popular format is "%c" which will return something like: "Fri Oct 1 10:54:27 1999". Note that timezone aspect of formatting time is controlled by tz() member function.

Parameters

  fmt_ | Format string as in strftime(3)

Returns
  Formatted string.

Definition at line 145 of file TimeVal.cpp.

{
struct tm ct;
char buf[80];
memset (buf, 0, 80);
if (m_tz == gmt)
ct = *( localtime ((const time_t*) &tv_sec) );
else
ct = *( gmtime ((const time_t*) &tv_sec) );
if (fmt_ == NULL) {
strftime (buf, 80, "%Y/%j %H:%M:%S", &ct);
sprintf (buf + strlen(buf),
".%03ld", (tv_usec %1000000)/1000);
}
else {
strftime(buf, 80, fmt_, &ct);
}
return string (buf);
}

References gmt, and m_tz.

Referenced by ASSA::Logger_Impl::add_timestamp(), ASSA::TimerQueue::expire(), fmt_hh_mm_ss(), fmt_mm_ss(), and ASSA::Reactor::registerTimerHandler().

## gettimeofday()

TimeVal TimeVal::gettimeofday | ( |  | ) |

static

Shields off underlying OS differences in getting current time.

Returns
  time of the day as timeval

Definition at line 43 of file TimeVal.cpp.

{
timeval tv;
#ifdef WIN32
FILETIME ft;
LARGE_INTEGER li;
__int64 t;
static int tzflag;
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;
t = li.QuadPart; /* In 100-nanosecond intervals */
t -= EPOCHFILETIME; /* Offset to the Epoch time */
t /= 10; /* In microseconds */
tv.tv_sec = (long)(t / 1000000);
tv.tv_usec = (long)(t % 1000000);
#else
::gettimeofday (&tv, 0);
#endif
return tv;
}
EPOCHFILETIME
#define EPOCHFILETIME
Definition TimeVal.cpp:34
ASSA::TimeVal::gettimeofday
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition TimeVal.cpp:44

References EPOCHFILETIME, and gettimeofday().

Referenced by ASSA::Logger_Impl::add_timestamp(), ASSA::Reactor::calculateTimeout(), ASSA::Reactor::dispatch(), gettimeofday(), ASSA::Reactor::registerTimerHandler(), ASSA::Timer::rescheduleExpirationTime(), ASSA::Reactor::waitForEvents(), and ASSA::TimerCountdown::~TimerCountdown().

## init()

void ASSA::TimeVal::init | ( | long | s_,
|  | long | ms_,
|  | int | tz_ )

inline protected

Internal initialization common to most constructors.

Definition at line 185 of file TimeVal.h.

{
tv_sec = s_;
tv_usec = ms_;
m_tz = tz_;
normalize ();
}

References m_tz, and normalize().

Referenced by operator=(), TimeVal(), TimeVal(), TimeVal(), and TimeVal().

## millisec()

long ASSA::TimeVal::millisec | ( |  | ) | const

inline

Convert tv_usec's microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec).

Definition at line 240 of file TimeVal.h.

{
return (msec () % 1000000) / 1000;
}

References msec().

Referenced by dump_to_log(), fmt_hh_mm_ss_mls(), fmt_mm_ss_mls(), and fmt_ss_mls().

## msec() [1/2]

void ASSA::TimeVal::msec | ( | long | msec_ | ) |

inline

Set microseconds.

Definition at line 70 of file TimeVal.h.

{ tv_usec = msec_; }

Referenced by ASSA::Logger_Impl::add_timestamp(), dump_to_log(), millisec(), and ASSA::Reactor::registerTimerHandler().

## msec() [2/2]

long ASSA::TimeVal::msec | ( | void |  | ) | const

inline

Get microseconds.

Definition at line 73 of file TimeVal.h.

{ return tv_usec; }

## normalize()

void TimeVal::normalize | ( |  | ) |

private

Normalization after arithmetic operation.

Definition at line 111 of file TimeVal.cpp.

{
if (tv_usec >= ONE_SECOND) {
do {
tv_sec++;
tv_usec -= ONE_SECOND;
}
while (tv_usec >= ONE_SECOND);
}
else if (tv_usec <= -ONE_SECOND) {
do {
tv_sec--;
tv_usec += ONE_SECOND;
}
while (tv_usec <= -ONE_SECOND);
}
if (tv_sec >= 1 && tv_usec < 0) {
tv_sec--;
tv_usec += ONE_SECOND;
}
else if (tv_sec < 0 && tv_usec > 0) {
tv_sec++;
tv_usec -= ONE_SECOND;
}
}
ONE_SECOND
static const long ONE_SECOND
Definition TimeVal.cpp:31

References ONE_SECOND.

Referenced by init(), operator+, operator+=(), operator-, operator-=(), and TimeVal().

## operator double()

ASSA::TimeVal::operator double | ( |  | ) | const

inline

Implicit conversion to double.

Definition at line 234 of file TimeVal.h.

{
return tv_sec + tv_usec / 1000000.0;
}

## operator+=()

TimeVal & TimeVal::operator+= | ( | const TimeVal & | rhs_ | ) |

Addition.

Definition at line 73 of file TimeVal.cpp.

{
tv_sec += rhs_.tv_sec;
tv_usec += rhs_.tv_usec;
if (tv_usec >= ONE_SECOND) {
tv_usec -= ONE_SECOND;
tv_sec++;
}
else if (tv_sec >= 1 && tv_usec < 0) {
tv_usec += ONE_SECOND;
tv_sec--;
}
normalize ();
return *this;
}

References normalize(), ONE_SECOND, and TimeVal().

## operator-=()

TimeVal & TimeVal::operator-= | ( | const TimeVal & | rhs_ | ) |

Substraction.

Definition at line 92 of file TimeVal.cpp.

{
tv_sec -= rhs_.tv_sec;
tv_usec -= rhs_.tv_usec;
if (tv_usec < 0) {
tv_usec += ONE_SECOND;
tv_sec--;
}
else if (tv_usec >= ONE_SECOND) {
tv_usec -= ONE_SECOND;
tv_sec++;
}
normalize ();
return *this;
}

References normalize(), ONE_SECOND, and TimeVal().

## operator<()

bool ASSA::TimeVal::operator< | ( | const TimeVal & | rhs_ | ) | const

inline

Comparison.

Definition at line 290 of file TimeVal.h.

{
return (tv_sec < rhs_.tv_sec
|| (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
}

References TimeVal().

## operator=()

TimeVal & ASSA::TimeVal::operator= | ( | const TimeVal & | tv_ | ) |

inline

Definition at line 265 of file TimeVal.h.

{
init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
return *this;
}

References init(), m_tz, and TimeVal().

## operator==()

bool ASSA::TimeVal::operator== | ( | const TimeVal & | rhs_ | ) | const

inline

Equality.

Definition at line 299 of file TimeVal.h.

{
return !(*this < rhs_ || rhs_ < *this);
}

References TimeVal().

## sec() [1/2]

void ASSA::TimeVal::sec | ( | long | sec_ | ) |

inline

Set seconds.

Definition at line 64 of file TimeVal.h.

{ tv_sec = sec_; }

Referenced by dump_to_log(), and ASSA::Reactor::registerTimerHandler().

## sec() [2/2]

long ASSA::TimeVal::sec | ( | void |  | ) | const

inline

Get secons.

Definition at line 67 of file TimeVal.h.

{ return tv_sec; }

## tz() [1/2]

void ASSA::TimeVal::tz | ( | int | tz_ | ) |

inline

Set timezone.

Definition at line 81 of file TimeVal.h.

{ m_tz = tz_; }

References m_tz.

Referenced by ASSA::Logger_Impl::add_timestamp().

## tz() [2/2]

int ASSA::TimeVal::tz | ( | void |  | ) | const

inline

Get timezone.

Definition at line 84 of file TimeVal.h.

{ return m_tz; }

References m_tz.

## zeroTime()

TimeVal ASSA::TimeVal::zeroTime | ( |  | ) |

inline static

Static that returns zero timeval: {0,0}.

Definition at line 157 of file TimeVal.h.

{ return m_zero; }
ASSA::TimeVal::m_zero
static TimeVal m_zero
Zero time value.
Definition TimeVal.h:178

References m_zero, and TimeVal().

Referenced by ASSA::Reactor::calculateTimeout(), and ASSA::TimerCountdown::~TimerCountdown().

## Friends And Related Symbol Documentation

## operator!=

bool operator!= | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Comparison.

Definition at line 312 of file TimeVal.h.

{
return !( lhs_ == rhs_ );
}

References TimeVal().

## operator+

TimeVal operator+ | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Addition.

Definition at line 273 of file TimeVal.h.

{
TimeVal temp(lhs_);
temp += rhs_;
temp.normalize ();
return temp;
}
ASSA::TimeVal::TimeVal
TimeVal()
Default constructor.
Definition TimeVal.h:196

References normalize(), and TimeVal().

## operator-

TimeVal operator- | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Substraction.

Definition at line 282 of file TimeVal.h.

{
TimeVal temp(lhs_);
temp -= rhs_;
temp.normalize ();
return temp;
}

References normalize(), and TimeVal().

## operator<=

bool operator<= | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Comparison.

Definition at line 317 of file TimeVal.h.

{
return !(rhs_ < lhs_);
}

References TimeVal().

## operator>

bool operator> | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Comparison.

Definition at line 306 of file TimeVal.h.

{
return rhs_ < lhs_;
}

References TimeVal().

## operator>=

bool operator>= | ( | const TimeVal & | lhs_,
|  | const TimeVal & | rhs_ )

friend

Comparison.

Definition at line 324 of file TimeVal.h.

{
return !(lhs_ < rhs_);
}

References TimeVal().

## Member Data Documentation

## m_tz

int ASSA::TimeVal::m_tz

private

Time zone.

Definition at line 175 of file TimeVal.h.

Referenced by fmt_hh_mm_ss_mls(), fmt_mm_ss_mls(), fmt_ss_mls(), fmtString(), init(), operator=(), TimeVal(), TimeVal(), tz(), and tz().

## m_zero

TimeVal TimeVal::m_zero

static private

Zero time value.

Definition at line 178 of file TimeVal.h.

Referenced by zeroTime().

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

- TimeVal.h
- TimeVal.cpp