libassa  3.5.1
Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | Friends | List of all members
ASSA::TimeVal Class Reference

#include <TimeVal.h>

Inheritance diagram for ASSA::TimeVal:

Public Types

enum  { gmt, loc }
 

Public Member Functions

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

Static Public Member Functions

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

Protected Member Functions

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

Private Member Functions

void normalize ()
 Normalization after arithmetic operation. More...
 

Private Attributes

int m_tz
 Time zone. More...
 

Static Private Attributes

static TimeVal m_zero
 Zero time value. More...
 

Friends

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

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.

33  {
34  gmt,
35  loc
36  };
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 196 of file TimeVal.h.

References gmt, and init().

Referenced by init(), and TimeVal().

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

◆ TimeVal() [2/5]

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

Constructor from seconds/microseconds pair.

Definition at line 203 of file TimeVal.h.

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

204 {
205  init (sec_, msec_, gmt);
206 }
void init(long, long, int)
Internal initialization common to most constructors.
Definition: TimeVal.h:186

◆ TimeVal() [3/5]

ASSA::TimeVal::TimeVal ( double  d_)
inline

Constructor from double.

Definition at line 210 of file TimeVal.h.

References normalize(), and TimeVal().

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

◆ TimeVal() [4/5]

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

Constructor from struct timeval.

Definition at line 221 of file TimeVal.h.

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

222 {
223  init (tv_.tv_sec, tv_.tv_usec, gmt);
224 }
void init(long, long, int)
Internal initialization common to most constructors.
Definition: TimeVal.h:186

◆ TimeVal() [5/5]

ASSA::TimeVal::TimeVal ( const TimeVal tv_)
inline

Copy constructor.

Definition at line 228 of file TimeVal.h.

References init(), and m_tz.

229 {
230  init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
231 }
void init(long, long, int)
Internal initialization common to most constructors.
Definition: TimeVal.h:186

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 227 of file TimeVal.cpp.

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

Referenced by fmt_ss_mls(), and tz().

228 {
229  static const char self []="TimeVal::dump_to_log"; trace(self);
230 
231  if (Logger::get_instance ()->group_enabled (REACT))
232  {
233  DL((REACT,"=== TimeVal %s ===\n", var_name_.c_str ()));
234  DL((REACT,"MM:SS:MLS = %s\n", fmt_mm_ss_mls ().c_str ()));
235  DL((REACT,"tv_sec = %d, tv_msec = %d, tv_mls = %d\n",
236  sec (), msec (), millisec ()));
237  DL((REACT,"(double) = %7.4f\n", double (*this)));
238  DL((REACT,"==================\n"));
239  }
240 }
long msec(void) const
Get microseconds.
Definition: TimeVal.h:73
#define trace(s)
trace() is used to trace function call chain in C++ program.
Definition: Logger.h:429
#define DL(X)
A macro for writing debug message to the Logger.
Definition: Logger.h:273
Class Reactor/PrioriyQueue messages.
Definition: LogMask.h:39
long millisec() const
Convert tv_usec&#39;s microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec). ...
Definition: TimeVal.h:241
long sec(void) const
Get secons.
Definition: TimeVal.h:67
static Logger * get_instance()
Return an instance of templated class T.
Definition: Singleton.h:47
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition: TimeVal.cpp:189

◆ 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 248 of file TimeVal.h.

References fmt_mm_ss(), and fmtString().

Referenced by millisec(), and tz().

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

◆ 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 170 of file TimeVal.cpp.

References fmt_mm_ss_mls(), gmt, m_tz, and millisec().

Referenced by fmtString(), and tz().

171 {
172  struct tm ct;
173  char buf [80];
174  memset (buf, 0, 80);
175 
176  if (m_tz == gmt)
177  ct = *( localtime ((const time_t*) &tv_sec) );
178  else
179  ct = *( gmtime ((const time_t*) &tv_sec) );
180 
181  strftime (buf, 80, "%H:%M:%S", &ct);
182  sprintf (buf + strlen(buf), ".%03ld", millisec ());
183 
184  return string (buf);
185 }
long millisec() const
Convert tv_usec&#39;s microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec). ...
Definition: TimeVal.h:241
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ fmt_mm_ss()

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

Format timeval structure in readable format MM:SS.

Definition at line 255 of file TimeVal.h.

References fmtString(), and operator=().

Referenced by fmt_hh_mm_ss(), and tz().

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

◆ fmt_mm_ss_mls()

string TimeVal::fmt_mm_ss_mls ( ) const

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

Definition at line 189 of file TimeVal.cpp.

References fmt_ss_mls(), gmt, m_tz, and millisec().

Referenced by ASSA::Timer::dump(), dump_to_log(), fmt_hh_mm_ss_mls(), and tz().

190 {
191  struct tm ct;
192  char buf [80];
193  memset (buf, 0, 80);
194 
195  if (m_tz == gmt)
196  ct = *( localtime ((const time_t*) &tv_sec) );
197  else
198  ct = *( gmtime ((const time_t*) &tv_sec) );
199 
200  strftime (buf, 80, "%M:%S", &ct);
201  sprintf (buf + strlen(buf), ".%03ld", millisec ());
202 
203  return string (buf);
204 }
long millisec() const
Convert tv_usec&#39;s microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec). ...
Definition: TimeVal.h:241
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ fmt_ss_mls()

string TimeVal::fmt_ss_mls ( ) const

Format timeval structure in readable format SS.MLS.

Definition at line 208 of file TimeVal.cpp.

References dump_to_log(), gmt, m_tz, and millisec().

Referenced by fmt_mm_ss_mls(), and tz().

209 {
210  struct tm ct;
211  char buf [80];
212  memset (buf, 0, 80);
213 
214  if (m_tz == gmt)
215  ct = *( localtime ((const time_t*) &tv_sec) );
216  else
217  ct = *( gmtime ((const time_t*) &tv_sec) );
218 
219  strftime (buf, 80, "%S", &ct);
220  sprintf (buf + strlen(buf), ".%03ld", millisec ());
221 
222  return string (buf);
223 }
long millisec() const
Convert tv_usec&#39;s microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec). ...
Definition: TimeVal.h:241
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ 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 146 of file TimeVal.cpp.

References fmt_hh_mm_ss_mls(), gmt, and m_tz.

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

147 {
148  struct tm ct;
149  char buf[80];
150  memset (buf, 0, 80);
151 
152  if (m_tz == gmt)
153  ct = *( localtime ((const time_t*) &tv_sec) );
154  else
155  ct = *( gmtime ((const time_t*) &tv_sec) );
156 
157  if (fmt_ == NULL) {
158  strftime (buf, 80, "%Y/%j %H:%M:%S", &ct);
159  sprintf (buf + strlen(buf),
160  ".%03ld", (tv_usec %1000000)/1000);
161  }
162  else {
163  strftime(buf, 80, fmt_, &ct);
164  }
165  return string (buf);
166 }
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ gettimeofday()

TimeVal TimeVal::gettimeofday ( )
static

Shields off underlying OS differences in getting current time.

Returns
time of the day as timeval

Definition at line 44 of file TimeVal.cpp.

References EPOCHFILETIME, and operator+=().

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

45 {
46  timeval tv;
47 
48 #ifdef WIN32
49  FILETIME ft;
50  LARGE_INTEGER li;
51  __int64 t;
52  static int tzflag;
53 
54  GetSystemTimeAsFileTime(&ft);
55  li.LowPart = ft.dwLowDateTime;
56  li.HighPart = ft.dwHighDateTime;
57  t = li.QuadPart; /* In 100-nanosecond intervals */
58  t -= EPOCHFILETIME; /* Offset to the Epoch time */
59  t /= 10; /* In microseconds */
60  tv.tv_sec = (long)(t / 1000000);
61  tv.tv_usec = (long)(t % 1000000);
62 #else
63  ::gettimeofday (&tv, 0);
64 #endif
65  return tv;
66 }
#define EPOCHFILETIME
Definition: TimeVal.cpp:34
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition: TimeVal.cpp:44

◆ init()

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

Internal initialization common to most constructors.

Definition at line 186 of file TimeVal.h.

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

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

187 {
188  tv_sec = s_;
189  tv_usec = ms_;
190  m_tz = tz_;
191  normalize ();
192 }
void normalize()
Normalization after arithmetic operation.
Definition: TimeVal.cpp:112
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ 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 241 of file TimeVal.h.

References fmt_hh_mm_ss(), and msec().

Referenced by dump_to_log(), fmt_hh_mm_ss_mls(), fmt_mm_ss_mls(), fmt_ss_mls(), msec(), and operator double().

242 {
243  return (msec () % 1000000) / 1000;
244 }
long msec(void) const
Get microseconds.
Definition: TimeVal.h:73

◆ msec() [1/2]

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

Set microseconds.

Definition at line 70 of file TimeVal.h.

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

70 { tv_usec = msec_; }

◆ msec() [2/2]

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

Get microseconds.

Definition at line 73 of file TimeVal.h.

References millisec().

Referenced by dump_to_log(), and millisec().

73 { return tv_usec; }

◆ normalize()

void TimeVal::normalize ( )
private

Normalization after arithmetic operation.

Definition at line 112 of file TimeVal.cpp.

References fmtString(), and ONE_SECOND.

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

113 {
114  if (tv_usec >= ONE_SECOND) {
115  do {
116  tv_sec++;
117  tv_usec -= ONE_SECOND;
118  }
119  while (tv_usec >= ONE_SECOND);
120  }
121  else if (tv_usec <= -ONE_SECOND) {
122  do {
123  tv_sec--;
124  tv_usec += ONE_SECOND;
125  }
126  while (tv_usec <= -ONE_SECOND);
127  }
128 
129  if (tv_sec >= 1 && tv_usec < 0) {
130  tv_sec--;
131  tv_usec += ONE_SECOND;
132  }
133  else if (tv_sec < 0 && tv_usec > 0) {
134  tv_sec++;
135  tv_usec -= ONE_SECOND;
136  }
137 }
static const long ONE_SECOND
Definition: TimeVal.cpp:31

◆ operator double()

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

Implicit conversion to double.

Definition at line 234 of file TimeVal.h.

References millisec().

235 {
236  return tv_sec + tv_usec / 1000000.0;
237 }

◆ operator+=()

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

Addition.

Definition at line 74 of file TimeVal.cpp.

References normalize(), ONE_SECOND, and operator-=().

Referenced by gettimeofday(), and tz().

75 {
76  tv_sec += rhs_.tv_sec;
77  tv_usec += rhs_.tv_usec;
78 
79  if (tv_usec >= ONE_SECOND) {
80  tv_usec -= ONE_SECOND;
81  tv_sec++;
82  }
83  else if (tv_sec >= 1 && tv_usec < 0) {
84  tv_usec += ONE_SECOND;
85  tv_sec--;
86  }
87  normalize ();
88  return *this;
89 }
void normalize()
Normalization after arithmetic operation.
Definition: TimeVal.cpp:112
static const long ONE_SECOND
Definition: TimeVal.cpp:31

◆ operator-=()

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

Substraction.

Definition at line 93 of file TimeVal.cpp.

References normalize(), and ONE_SECOND.

Referenced by operator+=(), and tz().

94 {
95  tv_sec -= rhs_.tv_sec;
96  tv_usec -= rhs_.tv_usec;
97 
98  if (tv_usec < 0) {
99  tv_usec += ONE_SECOND;
100  tv_sec--;
101  }
102  else if (tv_usec >= ONE_SECOND) {
103  tv_usec -= ONE_SECOND;
104  tv_sec++;
105  }
106  normalize ();
107  return *this;
108 }
void normalize()
Normalization after arithmetic operation.
Definition: TimeVal.cpp:112
static const long ONE_SECOND
Definition: TimeVal.cpp:31

◆ operator<()

bool ASSA::TimeVal::operator< ( const TimeVal rhs_) const
inline

Comparison.

Definition at line 292 of file TimeVal.h.

References operator==().

Referenced by ASSA::operator-(), and tz().

293 {
294  return (tv_sec < rhs_.tv_sec
295  || (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
296 }

◆ operator=()

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

Definition at line 266 of file TimeVal.h.

References init(), and m_tz.

Referenced by fmt_mm_ss(), and tz().

267 {
268  init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
269  return *this;
270 }
void init(long, long, int)
Internal initialization common to most constructors.
Definition: TimeVal.h:186

◆ operator==()

bool ASSA::TimeVal::operator== ( const TimeVal rhs_) const
inline

Equality.

Definition at line 300 of file TimeVal.h.

Referenced by operator<(), and tz().

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

◆ sec() [1/2]

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

Set seconds.

Definition at line 64 of file TimeVal.h.

Referenced by ASSA::Reactor::registerTimerHandler().

64 { tv_sec = sec_; }

◆ sec() [2/2]

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

Get secons.

Definition at line 67 of file TimeVal.h.

Referenced by dump_to_log().

67 { return tv_sec; }

◆ tz() [1/2]

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

Set timezone.

Definition at line 81 of file TimeVal.h.

References m_tz.

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

81 { m_tz = tz_; }
int m_tz
Time zone.
Definition: TimeVal.h:175

◆ tz() [2/2]

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

◆ zeroTime()

static TimeVal ASSA::TimeVal::zeroTime ( )
inlinestatic

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

Definition at line 157 of file TimeVal.h.

References gettimeofday(), init(), m_zero, and normalize().

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

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

Friends And Related Function Documentation

◆ operator!=

bool operator!= ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Comparison.

Definition at line 312 of file TimeVal.h.

Referenced by tz().

313 {
314  return !( lhs_ == rhs_ );
315 }

◆ operator+

TimeVal operator+ ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Addition.

Definition at line 273 of file TimeVal.h.

Referenced by tz().

274 {
275  TimeVal temp(lhs_);
276  temp += rhs_;
277  temp.normalize ();
278  return temp;
279 }
TimeVal()
Default constructor.
Definition: TimeVal.h:196

◆ operator-

TimeVal operator- ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Substraction.

Definition at line 282 of file TimeVal.h.

Referenced by tz().

283 {
284  TimeVal temp(lhs_);
285  temp -= rhs_;
286  temp.normalize ();
287  return temp;
288 }
TimeVal()
Default constructor.
Definition: TimeVal.h:196

◆ operator<=

bool operator<= ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Comparison.

Definition at line 318 of file TimeVal.h.

Referenced by tz().

319 {
320  return !(rhs_ < lhs_);
321 }

◆ operator>

bool operator> ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Comparison.

Definition at line 306 of file TimeVal.h.

Referenced by tz().

307 {
308  return rhs_ < lhs_;
309 }

◆ operator>=

bool operator>= ( const TimeVal lhs_,
const TimeVal rhs_ 
)
friend

Comparison.

Definition at line 324 of file TimeVal.h.

Referenced by tz().

325 {
326  return !(lhs_ < rhs_);
327 }

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(), and tz().

◆ m_zero

TimeVal TimeVal::m_zero
staticprivate

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: