libassa  3.5.1
TimeVal.h
Go to the documentation of this file.
1 // -*- c++ -*-
2 //------------------------------------------------------------------------------
3 // TimeVal.h
4 //------------------------------------------------------------------------------
5 // Copyright (c) 1999 by Vladislav Grinchenko
6 //
7 // This library is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Library General Public
9 // License as published by the Free Software Foundation; either
10 // version 2 of the License, or (at your option) any later version.
11 //------------------------------------------------------------------------------
12 // Created: 09/28/1999
13 //------------------------------------------------------------------------------
14 #ifndef TIME_VAL_H
15 #define TIME_VAL_H
16 
17 #include <sys/time.h> // gettimeofday(3)
18 #include <unistd.h> // gettimeofday(3)
19 
20 #include <string>
21 using std::string;
22 
23 namespace ASSA {
24 
30 class TimeVal : public timeval
31 {
32 public:
33  enum {
34  gmt,
35  loc
36  };
37 
41  TimeVal ();
42 
45  TimeVal (long sec_, long msec_);
46 
49  TimeVal (double d_);
50 
53  TimeVal (const timeval& tv_);
54 
57  TimeVal (const TimeVal& tv_);
58 
61  operator double () const;
62 
64  void sec (long sec_) { tv_sec = sec_; }
65 
67  long sec (void) const { return tv_sec; }
68 
70  void msec (long msec_) { tv_usec = msec_; }
71 
73  long msec (void) const { return tv_usec; }
74 
78  long millisec () const;
79 
81  void tz (int tz_) { m_tz = tz_; }
82 
84  int tz (void) const { return m_tz; }
85 
86  TimeVal& operator= (const TimeVal& tv_);
87 
89  TimeVal& operator+= (const TimeVal& rhs_);
90 
92  TimeVal& operator-= (const TimeVal& rhs_);
93 
95  friend TimeVal operator+(const TimeVal& lhs_, const TimeVal& rhs_);
96 
98  friend TimeVal operator-(const TimeVal& lhs_, const TimeVal& rhs_);
99 
101  bool operator< (const TimeVal& rhs_) const;
102 
104  bool operator==(const TimeVal& rhs_) const;
105 
107  friend bool operator> (const TimeVal& lhs_, const TimeVal& rhs_);
108 
110  friend bool operator!=(const TimeVal& lhs_, const TimeVal& rhs_);
111 
113  friend bool operator<=(const TimeVal& lhs_, const TimeVal& rhs_);
114 
116  friend bool operator>=(const TimeVal& lhs_, const TimeVal& rhs_);
117 
128  string fmtString (const char* fmt_ = NULL) const;
129 
132  string fmt_hh_mm_ss () const;
133 
136  string fmt_hh_mm_ss_mls () const;
137 
140  string fmt_mm_ss () const;
141 
144  string fmt_mm_ss_mls () const;
145 
148  string fmt_ss_mls () const;
149 
153  void dump_to_log (const string& name_ = "") const;
154 
157  static TimeVal zeroTime () { return m_zero; }
158 
163  static TimeVal gettimeofday ();
164 
165 protected:
167  void init (long, long, int);
168 
169 private:
171  void normalize ();
172 
173 private:
175  int m_tz;
176 
178  static TimeVal m_zero;
179 };
180 //------------------------------------------------------------------------------
181 // Inlines
182 //------------------------------------------------------------------------------
183 
184 inline void
186 init (long s_, long ms_, int tz_)
187 {
188  tv_sec = s_;
189  tv_usec = ms_;
190  m_tz = tz_;
191  normalize ();
192 }
193 
194 inline
196 TimeVal ()
197 {
198  init (0, 0, gmt);
199 }
200 
201 inline
203 TimeVal (long sec_, long msec_)
204 {
205  init (sec_, msec_, gmt);
206 }
207 
208 inline
210 TimeVal (double d_)
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 }
218 
219 inline
221 TimeVal (const timeval& tv_)
222 {
223  init (tv_.tv_sec, tv_.tv_usec, gmt);
224 }
225 
226 inline
228 TimeVal (const TimeVal& tv_)
229 {
230  init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
231 }
232 
233 inline
234 TimeVal::operator double () const
235 {
236  return tv_sec + tv_usec / 1000000.0;
237 }
238 
239 inline long
241 millisec () const
242 {
243  return (msec () % 1000000) / 1000;
244 }
245 
246 inline string
248 fmt_hh_mm_ss () const
249 {
250  return fmtString ("%T");
251 }
252 
253 inline string
255 fmt_mm_ss () const
256 {
257  return fmtString ("%M:%S");
258 }
259 
260 //------------------------------------------------------------------------------
261 // Friend functions
262 //------------------------------------------------------------------------------
263 
264 inline TimeVal&
266 operator=(const TimeVal& tv_)
267 {
268  init (tv_.tv_sec, tv_.tv_usec, tv_.m_tz);
269  return *this;
270 }
271 
272 inline TimeVal
273 operator+(const TimeVal& lhs_, const TimeVal& rhs_)
274 {
275  TimeVal temp(lhs_);
276  temp += rhs_;
277  temp.normalize ();
278  return temp;
279 }
280 
281 inline TimeVal
282 operator-(const TimeVal& lhs_, const TimeVal& rhs_)
283 {
284  TimeVal temp(lhs_);
285  temp -= rhs_;
286  temp.normalize ();
287  return temp;
288 }
289 
290 inline bool
291 TimeVal::
292 operator<(const TimeVal& rhs_) const
293 {
294  return (tv_sec < rhs_.tv_sec
295  || (tv_sec == rhs_.tv_sec && tv_usec < rhs_.tv_usec) ) ;
296 }
297 
298 inline bool
300 operator==(const TimeVal& rhs_) const
301 {
302  return !(*this < rhs_ || rhs_ < *this);
303 }
304 
305 inline bool
306 operator> (const TimeVal& lhs_, const TimeVal& rhs_)
307 {
308  return rhs_ < lhs_;
309 }
310 
311 inline bool
312 operator!=(const TimeVal& lhs_, const TimeVal& rhs_)
313 {
314  return !( lhs_ == rhs_ );
315 }
316 
317 inline bool
318 operator<=(const TimeVal& lhs_, const TimeVal& rhs_)
319 {
320  return !(rhs_ < lhs_);
321 }
322 
323 inline bool
324 operator>=(const TimeVal& lhs_, const TimeVal& rhs_)
325 {
326  return !(lhs_ < rhs_);
327 }
328 
329 } // end namespace ASSA
330 
331 #endif /* TIME_VAL_H */
void tz(int tz_)
Set timezone.
Definition: TimeVal.h:81
static TimeVal m_zero
Zero time value.
Definition: TimeVal.h:178
string fmtString(const char *fmt_=NULL) const
Format timeval structure into readable format.
Definition: TimeVal.cpp:146
string fmt_mm_ss() const
Format timeval structure in readable format MM:SS.
Definition: TimeVal.h:255
void normalize()
Normalization after arithmetic operation.
Definition: TimeVal.cpp:112
int tz(void) const
Get timezone.
Definition: TimeVal.h:84
TimeVal & operator=(const TimeVal &tv_)
Definition: TimeVal.h:266
string fmt_ss_mls() const
Format timeval structure in readable format SS.MLS.
Definition: TimeVal.cpp:208
static TimeVal zeroTime()
Static that returns zero timeval: {0,0}.
Definition: TimeVal.h:157
friend TimeVal operator+(const TimeVal &lhs_, const TimeVal &rhs_)
Addition.
Definition: TimeVal.h:273
friend bool operator>=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition: TimeVal.h:324
friend bool operator>(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition: TimeVal.h:306
@ gmt
GMT.
Definition: TimeVal.h:34
@ loc
Local Time Zone.
Definition: TimeVal.h:35
friend bool operator<=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition: TimeVal.h:318
string fmt_mm_ss_mls() const
Format timeval structure in readable format MM:SS.MLS.
Definition: TimeVal.cpp:189
friend bool operator!=(const TimeVal &lhs_, const TimeVal &rhs_)
Comparison.
Definition: TimeVal.h:312
string fmt_hh_mm_ss() const
Format timeval structure in readable format HH:MM:SS.
Definition: TimeVal.h:248
long msec(void) const
Get microseconds.
Definition: TimeVal.h:73
TimeVal & operator+=(const TimeVal &rhs_)
Addition.
Definition: TimeVal.cpp:74
long millisec() const
Convert tv_usec's microseconds (=1/1,000,000 sec) to milliseconds (=1/1,000 sec).
Definition: TimeVal.h:241
friend TimeVal operator-(const TimeVal &lhs_, const TimeVal &rhs_)
Substraction.
Definition: TimeVal.h:282
void init(long, long, int)
Internal initialization common to most constructors.
Definition: TimeVal.h:186
void dump_to_log(const string &name_="") const
Dump value of struct timeval to the log file with mask TRACE = DBG_APP15.
Definition: TimeVal.cpp:227
void sec(long sec_)
Set seconds.
Definition: TimeVal.h:64
bool operator<(const TimeVal &rhs_) const
Comparison.
Definition: TimeVal.h:292
TimeVal()
Default constructor.
Definition: TimeVal.h:196
static TimeVal gettimeofday()
Shields off underlying OS differences in getting current time.
Definition: TimeVal.cpp:44
long sec(void) const
Get secons.
Definition: TimeVal.h:67
TimeVal & operator-=(const TimeVal &rhs_)
Substraction.
Definition: TimeVal.cpp:93
bool operator==(const TimeVal &rhs_) const
Equality.
Definition: TimeVal.h:300
void msec(long msec_)
Set microseconds.
Definition: TimeVal.h:70
string fmt_hh_mm_ss_mls() const
Format timeval structure in readable format HH:MM:SS.MLS.
Definition: TimeVal.cpp:170
int m_tz
Time zone.
Definition: TimeVal.h:175
Definition: Acceptor.h:40
bool operator!=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:312
bool operator>=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:324
TimeVal operator+(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:273
bool operator>(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:306
TimeVal operator-(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:282
bool operator<=(const TimeVal &lhs_, const TimeVal &rhs_)
Definition: TimeVal.h:318