#include <TimeVal.h>
Public Types | |
gmt | |
GMT. | |
loc | |
Local Time Zone. | |
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. |
Definition at line 30 of file TimeVal.h.
anonymous enum |
ASSA::TimeVal::TimeVal | ( | ) | [inline] |
ASSA::TimeVal::TimeVal | ( | long | sec_, | |
long | msec_ | |||
) | [inline] |
ASSA::TimeVal::TimeVal | ( | double | d_ | ) | [inline] |
ASSA::TimeVal::TimeVal | ( | const timeval & | tv_ | ) | [inline] |
ASSA::TimeVal::TimeVal | ( | const TimeVal & | tv_ | ) | [inline] |
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< T >::get_instance(), millisec(), msec(), ASSA::REACT, sec(), and trace.
00228 { 00229 static const char self []="TimeVal::dump_to_log"; trace(self); 00230 00231 if (Logger::get_instance ()->group_enabled (REACT)) 00232 { 00233 DL((REACT,"=== TimeVal %s ===\n", var_name_.c_str ())); 00234 DL((REACT,"MM:SS:MLS = %s\n", fmt_mm_ss_mls ().c_str ())); 00235 DL((REACT,"tv_sec = %d, tv_msec = %d, tv_mls = %d\n", 00236 sec (), msec (), millisec ())); 00237 DL((REACT,"(double) = %7.4f\n", double (*this))); 00238 DL((REACT,"==================\n")); 00239 } 00240 }
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 fmtString().
00249 { 00250 return fmtString ("%T"); 00251 }
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 gmt, m_tz, and millisec().
00171 { 00172 struct tm ct; 00173 char buf [80]; 00174 memset (buf, 0, 80); 00175 00176 if (m_tz == gmt) 00177 ct = *( localtime ((const time_t*) &tv_sec) ); 00178 else 00179 ct = *( gmtime ((const time_t*) &tv_sec) ); 00180 00181 strftime (buf, 80, "%H:%M:%S", &ct); 00182 sprintf (buf + strlen(buf), ".%03ld", millisec ()); 00183 00184 return string (buf); 00185 }
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().
00256 { 00257 return fmtString ("%M:%S"); 00258 }
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 gmt, m_tz, and millisec().
Referenced by ASSA::Timer::dump(), and dump_to_log().
00190 { 00191 struct tm ct; 00192 char buf [80]; 00193 memset (buf, 0, 80); 00194 00195 if (m_tz == gmt) 00196 ct = *( localtime ((const time_t*) &tv_sec) ); 00197 else 00198 ct = *( gmtime ((const time_t*) &tv_sec) ); 00199 00200 strftime (buf, 80, "%M:%S", &ct); 00201 sprintf (buf + strlen(buf), ".%03ld", millisec ()); 00202 00203 return string (buf); 00204 }
string TimeVal::fmt_ss_mls | ( | ) | const |
Format timeval structure in readable format SS.MLS.
Definition at line 208 of file TimeVal.cpp.
References gmt, m_tz, and millisec().
00209 { 00210 struct tm ct; 00211 char buf [80]; 00212 memset (buf, 0, 80); 00213 00214 if (m_tz == gmt) 00215 ct = *( localtime ((const time_t*) &tv_sec) ); 00216 else 00217 ct = *( gmtime ((const time_t*) &tv_sec) ); 00218 00219 strftime (buf, 80, "%S", &ct); 00220 sprintf (buf + strlen(buf), ".%03ld", millisec ()); 00221 00222 return string (buf); 00223 }
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.
fmt_ | Format string as in strftime(3) |
Definition at line 146 of file TimeVal.cpp.
Referenced by ASSA::Logger_Impl::add_timestamp(), ASSA::Timer::dump(), ASSA::TimerQueue::expire(), fmt_hh_mm_ss(), fmt_mm_ss(), and ASSA::Reactor::registerTimerHandler().
00147 { 00148 struct tm ct; 00149 char buf[80]; 00150 memset (buf, 0, 80); 00151 00152 if (m_tz == gmt) 00153 ct = *( localtime ((const time_t*) &tv_sec) ); 00154 else 00155 ct = *( gmtime ((const time_t*) &tv_sec) ); 00156 00157 if (fmt_ == NULL) { 00158 strftime (buf, 80, "%Y/%j %H:%M:%S", &ct); 00159 sprintf (buf + strlen(buf), 00160 ".%03ld", (tv_usec %1000000)/1000); 00161 } 00162 else { 00163 strftime(buf, 80, fmt_, &ct); 00164 } 00165 return string (buf); 00166 }
TimeVal TimeVal::gettimeofday | ( | ) | [static] |
Shields off underlying OS differences in getting current time.
Definition at line 44 of file TimeVal.cpp.
References EPOCHFILETIME.
Referenced by ASSA::Logger_Impl::add_timestamp(), ASSA::Reactor::calculateTimeout(), ASSA::Reactor::dispatch(), ASSA::Reactor::registerTimerHandler(), ASSA::Timer::rescheduleExpirationTime(), ASSA::Reactor::waitForEvents(), and ASSA::TimerCountdown::~TimerCountdown().
00045 { 00046 timeval tv; 00047 00048 #ifdef WIN32 00049 FILETIME ft; 00050 LARGE_INTEGER li; 00051 __int64 t; 00052 static int tzflag; 00053 00054 GetSystemTimeAsFileTime(&ft); 00055 li.LowPart = ft.dwLowDateTime; 00056 li.HighPart = ft.dwHighDateTime; 00057 t = li.QuadPart; /* In 100-nanosecond intervals */ 00058 t -= EPOCHFILETIME; /* Offset to the Epoch time */ 00059 t /= 10; /* In microseconds */ 00060 tv.tv_sec = (long)(t / 1000000); 00061 tv.tv_usec = (long)(t % 1000000); 00062 #else 00063 ::gettimeofday (&tv, 0); 00064 #endif 00065 return tv; 00066 }
void ASSA::TimeVal::init | ( | long | , | |
long | , | |||
int | ||||
) | [inline, protected] |
Internal initialization common to most constructors.
Definition at line 186 of file TimeVal.h.
References m_tz, and normalize().
Referenced by operator=(), and TimeVal().
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 msec().
Referenced by dump_to_log(), fmt_hh_mm_ss_mls(), fmt_mm_ss_mls(), and fmt_ss_mls().
00242 { 00243 return (msec () % 1000000) / 1000; 00244 }
long ASSA::TimeVal::msec | ( | void | ) | const [inline] |
Get microseconds.
Definition at line 73 of file TimeVal.h.
Referenced by dump_to_log(), and millisec().
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().
void TimeVal::normalize | ( | ) | [private] |
Normalization after arithmetic operation.
Definition at line 112 of file TimeVal.cpp.
References ONE_SECOND.
Referenced by init(), ASSA::operator+(), operator+=(), ASSA::operator-(), operator-=(), and TimeVal().
00113 { 00114 if (tv_usec >= ONE_SECOND) { 00115 do { 00116 tv_sec++; 00117 tv_usec -= ONE_SECOND; 00118 } 00119 while (tv_usec >= ONE_SECOND); 00120 } 00121 else if (tv_usec <= -ONE_SECOND) { 00122 do { 00123 tv_sec--; 00124 tv_usec += ONE_SECOND; 00125 } 00126 while (tv_usec <= -ONE_SECOND); 00127 } 00128 00129 if (tv_sec >= 1 && tv_usec < 0) { 00130 tv_sec--; 00131 tv_usec += ONE_SECOND; 00132 } 00133 else if (tv_sec < 0 && tv_usec > 0) { 00134 tv_sec++; 00135 tv_usec -= ONE_SECOND; 00136 } 00137 }
ASSA::TimeVal::operator double | ( | ) | const [inline] |
Addition.
Definition at line 74 of file TimeVal.cpp.
References normalize(), and ONE_SECOND.
00075 { 00076 tv_sec += rhs_.tv_sec; 00077 tv_usec += rhs_.tv_usec; 00078 00079 if (tv_usec >= ONE_SECOND) { 00080 tv_usec -= ONE_SECOND; 00081 tv_sec++; 00082 } 00083 else if (tv_sec >= 1 && tv_usec < 0) { 00084 tv_usec += ONE_SECOND; 00085 tv_sec--; 00086 } 00087 normalize (); 00088 return *this; 00089 }
Substraction.
Definition at line 93 of file TimeVal.cpp.
References normalize(), and ONE_SECOND.
00094 { 00095 tv_sec -= rhs_.tv_sec; 00096 tv_usec -= rhs_.tv_usec; 00097 00098 if (tv_usec < 0) { 00099 tv_usec += ONE_SECOND; 00100 tv_sec--; 00101 } 00102 else if (tv_usec >= ONE_SECOND) { 00103 tv_usec -= ONE_SECOND; 00104 tv_sec++; 00105 } 00106 normalize (); 00107 return *this; 00108 }
bool ASSA::TimeVal::operator< | ( | const TimeVal & | rhs_ | ) | const [inline] |
bool ASSA::TimeVal::operator== | ( | const TimeVal & | rhs_ | ) | const [inline] |
long ASSA::TimeVal::sec | ( | void | ) | const [inline] |
void ASSA::TimeVal::sec | ( | long | sec_ | ) | [inline] |
Set seconds.
Definition at line 64 of file TimeVal.h.
Referenced by ASSA::Reactor::registerTimerHandler().
int ASSA::TimeVal::tz | ( | void | ) | const [inline] |
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().
00081 { m_tz = tz_; }
static TimeVal ASSA::TimeVal::zeroTime | ( | ) | [inline, static] |
Static that returns zero timeval: {0,0}.
Definition at line 157 of file TimeVal.h.
References m_zero.
Referenced by ASSA::Reactor::calculateTimeout(), and ASSA::TimerCountdown::~TimerCountdown().
00157 { return m_zero; }
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().
TimeVal TimeVal::m_zero [static, private] |