Panda3D
time_clock.h
1 #ifndef __Time_H__
2 #define __Time_H__
3 //////////////////////////////////////////////////////
4 // Class : Time_Clock
5 //
6 // Description:
7 // This class is to provide a consistant interface and storage to
8 // clock time .. Epoch based time to the second
9 //
10 // jan-2000 .. rhh changinging all time to use sub second timing...
11 //
12 //
13 //////////////////////////////////////////////////////
14 
15 
16 #include <stdio.h>
17 
18 class Time_Span;
19 
21 {
22  friend class Time_Span;
23 
24 public:
25  // Constructors
26  static Time_Clock GetCurrentTime();
27  void ToCurrentTime();
28  Time_Clock( timeval &in_mytime)
29  {
30  _my_time = in_mytime;
31  };
32  Time_Clock();
33  Time_Clock(time_t time);
34  Time_Clock(long secs, long usecs);
35  Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
36  Time_Clock(const Time_Clock& timeSrc);
37 
38  inline const Time_Clock& operator=(const Time_Clock& timeSrc);
39  inline const Time_Clock& operator=(time_t t);
40 
41  // Attributes
42  struct tm* GetGmtTm(struct tm* ptm = NULL) const;
43  struct tm* GetLocalTm(struct tm* ptm = NULL) const;
44 
45  time_t GetTime() const;
46  int GetYear() const;
47  int GetMonth() const; // month of year (1 = Jan)
48  int GetDay() const; // day of month
49  int GetHour() const;
50  int GetMinute() const;
51  int GetSecond() const;
52  int GetDayOfWeek() const; // 1=Sun, 2=Mon, ..., 7=Sat
53 
54  void Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
55 
56 
57  // Operations
58  // time math
60  const Time_Clock& operator-=(const Time_Span &Time_Span);
61  bool operator==(const Time_Clock &time) const;
62  bool operator!=(const Time_Clock &time) const;
63  bool operator<(const Time_Clock &time) const;
64  bool operator>(const Time_Clock &time) const;
65  bool operator<=(const Time_Clock &time) const;
66  bool operator>=(const Time_Clock &time) const;
67 
68 
69  time_t GetTime_t()
70  {
71  return _my_time.tv_sec;
72  };
73  unsigned long GetUsecPart()
74  {
75  return _my_time.tv_usec;
76  };
77 
78  // formatting using "C" strftime
79  std::string Format(const char * pFormat) const;
80  std::string FormatGmt(const char * pFormat) const;
81  const timeval & GetTval()
82  {
83  return _my_time;
84  };
85  const timeval & GetTval() const
86  {
87  return _my_time;
88  } ;
89  private:
90  struct timeval _my_time;
91 };
92 /////////////////////////////////////////////////////////////////////////////
93 // Time_Clock - absolute time
94 
95 /////////////////////////////////////////////////////////////
96 // Function name : Time_Clock::Time_Clock
97 // Description : Construction from parts
98 // Argument : int nYear
99 // Argument : int nMonth
100 // Argument : int nDay
101 // Argument : int nHour
102 // Argument : int nMin
103 // Argument : int nSec
104 // Argument : int nDST
105 //////////////////////////////////////////////////////////
106 inline Time_Clock::Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
107 {
108  struct tm atm;
109  atm.tm_sec = nSec;
110  atm.tm_min = nMin;
111  atm.tm_hour = nHour;
112  assert(nDay >= 1 && nDay <= 31);
113  atm.tm_mday = nDay;
114  assert(nMonth >= 1 && nMonth <= 12);
115  atm.tm_mon = nMonth - 1; // tm_mon is 0 based
116  assert(nYear >= 1900);
117  atm.tm_year = nYear - 1900; // tm_year is 1900 based
118  atm.tm_isdst = nDST;
119  _my_time.tv_sec = (long)mktime(&atm);
120  assert(_my_time.tv_sec != -1); // indicates an illegal input time
121  _my_time.tv_usec = microseconds;
122  assert(_my_time.tv_usec < 1000000);
123 }
124 //////////////////////////////////////////////////////////////
125 // Function name : Time_Clock::Set
126 // Description :
127 // Return type : inline
128 // Argument : int nYear
129 // Argument : int nMonth
130 // Argument : int nDay
131 // Argument : int nHour
132 // Argument : int nMin
133 // Argument : int nSec
134 // Argument : unsigned long microseconds
135 // Argument : int nDST
136 //////////////////////////////////////////////////////////////
137 inline void Time_Clock::Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
138 {
139  struct tm atm;
140  atm.tm_sec = nSec;
141  atm.tm_min = nMin;
142  atm.tm_hour = nHour;
143  assert(nDay >= 1 && nDay <= 31);
144  atm.tm_mday = nDay;
145  assert(nMonth >= 1 && nMonth <= 12);
146  atm.tm_mon = nMonth - 1; // tm_mon is 0 based
147  assert(nYear >= 1900);
148  atm.tm_year = nYear - 1900; // tm_year is 1900 based
149  atm.tm_isdst = nDST;
150  _my_time.tv_sec = (long)mktime(&atm);
151  assert(_my_time.tv_sec != -1); // indicates an illegal input time
152  _my_time.tv_usec = microseconds;
153  assert(_my_time.tv_usec < 1000000);
154 }
155 /////////////////////////////////////////////////////////////
156 // Function name : Time_Clock::GetCurrentTime
157 // Description : The Default no param constructor.. Will set time to current system time
158 // Return type : Time_Clock
159 //////////////////////////////////////////////////////////
161 {
162  return Time_Clock();
163 }
164 //////////////////////////////////////////////////////////////
165 // Function name : Time_Clock::Time_Clock
166 // Description :
167 // Return type : inline
168 //////////////////////////////////////////////////////////////
170 {
171  gettimeofday(&_my_time, NULL);
172 }
173 //////////////////////////////////////////////////////////////
174 // Function name : Time_Clock::ToCurrentTime
175 // Description : Load this object with the current OS time
176 // Return type : inline void
177 // Argument : void
178 //////////////////////////////////////////////////////////////
180 {
181  gettimeofday(&_my_time, NULL);
182 }
183 /////////////////////////////////////////////////////////////
184 // Function name : Time_Clock::GetGmtTm
185 // Description : Access the stored time and convers to a struct tm format
186 // If storage location is specified then it will stor information in the
187 // provided buffer else it will use the library's internal buffer space
188 // Return type : struct tm*
189 // Argument : struct tm* ptm
190 //////////////////////////////////////////////////////////
191 inline struct tm* Time_Clock::GetGmtTm(struct tm* ptm) const
192 {
193  if (ptm != NULL)
194  {
195  *ptm = *gmtime((const time_t *)&_my_time.tv_sec);
196  return ptm;
197  } else
198  return gmtime((const time_t *)&_my_time.tv_sec);
199 }
200 
201 ////////////////////////////////////////////////////////////////////
202 // Function name : Time_Clock::GetLocalTm
203 // Description : Gets The local time in a tm structre from the internal time value
204 //
205 // Return type : struct tm*
206 // Argument : struct tm* ptm
207 ////////////////////////////////////////////////////////////////////
208 inline struct tm* Time_Clock::GetLocalTm(struct tm* ptm) const
209 {
210  if (ptm != NULL)
211  {
212  struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
213  if (ptmTemp == NULL)
214  return NULL; // indicates the _my_time.tv_sec was not initialized!
215  *ptm = *ptmTemp;
216  return ptm;
217  } else
218  return localtime((const time_t *)&_my_time.tv_sec);
219 }
220 /////////////////////////////////////////////////////////////////////////////
221 // String formatting
222 #define maxTimeBufferSize 4096
223 // Verifies will fail if the needed buffer size is too large
224 /////////////////////////////////////////////////////////////
225 // Function name : Time_Clock::Format
226 // Description : Used to allow access to the "C" library strftime functions..
227 //
228 // Return type : std::string
229 // Argument : char * pFormat
230 //////////////////////////////////////////////////////////
231 inline std::string Time_Clock::Format(const char * pFormat) const
232 {
233 
234  char szBuffer[maxTimeBufferSize];
235  char ch, ch1;
236  char * pch = szBuffer;
237 
238  while ((ch = *pFormat++) != '\0')
239  {
240  assert(pch < &szBuffer[maxTimeBufferSize]);
241  if (ch == '%')
242  {
243  switch (ch1 = *pFormat++)
244  {
245  default:
246  *pch++ = ch;
247  *pch++ = ch1;
248  break;
249  case 'N':
250  pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
251  break;
252  }
253  }
254  else
255  {
256  *pch++ = ch;
257  }
258  }
259 
260  *pch = '\0';
261 
262  char szBuffer1[maxTimeBufferSize];
263 
264  struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
265  if (ptmTemp == NULL ||
266  !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
267  szBuffer1[0] = '\0';
268  return std::string(szBuffer1);
269 }
270 //////////////////////////////////////////////////////////////
271 // Function name : Time_Clock::FormatGmt
272 // Description : A Wraper to
273 //
274 // size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
275 //
276 // Return type : inline std::string
277 // Argument : char * pFormat
278 //////////////////////////////////////////////////////////////
279 inline std::string Time_Clock::FormatGmt(const char * pFormat) const
280 {
281 
282  char szBuffer[maxTimeBufferSize];
283  char ch, ch1;
284  char * pch = szBuffer;
285 
286  while ((ch = *pFormat++) != '\0')
287  {
288  assert(pch < &szBuffer[maxTimeBufferSize]);
289  if (ch == '%')
290  {
291  switch (ch1 = *pFormat++)
292  {
293  default:
294  *pch++ = ch;
295  *pch++ = ch1;
296  break;
297  case 'N':
298  pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
299  break;
300  }
301  }
302  else
303  {
304  *pch++ = ch;
305  }
306  }
307  *pch = '\0';
308 
309  char szBuffer1[maxTimeBufferSize];
310 
311  struct tm* ptmTemp = gmtime((const time_t *)&_my_time.tv_sec);
312  if (ptmTemp == NULL ||
313  !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
314  szBuffer1[0] = '\0';
315  return std::string(szBuffer1);
316 }
317 //////////////////////////////////////////////////////////////
318 // Function name : Time_Clock::Time_Clock
319 // Description : The Constructor that take a time_t objext
320 // Return type : inline
321 // Argument : time_t time
322 //////////////////////////////////////////////////////////////
323 inline Time_Clock::Time_Clock(time_t time)
324 {
325  _my_time.tv_sec = (long)time;
326  _my_time.tv_usec = 0;
327 };
328 //////////////////////////////////////////////////////////////
329 // Function name : Time_Clock::Time_Clock
330 // Description : Constructor that takes in sec and usecs..
331 // Return type : inline
332 // Argument : long secs
333 // Argument : long usecs
334 //////////////////////////////////////////////////////////////
335 inline Time_Clock::Time_Clock(long secs, long usecs)
336 {
337  _my_time.tv_sec = secs;
338  _my_time.tv_usec = usecs;
339  NormalizeTime(_my_time);
340 };
341 //////////////////////////////////////////////////////////////
342 // Function name : Time_Clock::Time_Clock
343 // Description : yet another constructor
344 // Return type : inline
345 // Argument : const Time_Clock& timeSrc
346 //////////////////////////////////////////////////////////////
347 inline Time_Clock::Time_Clock(const Time_Clock& timeSrc)
348 {
349  _my_time.tv_sec = timeSrc._my_time.tv_sec;
350  _my_time.tv_usec = timeSrc._my_time.tv_usec;
351 }
352 //////////////////////////////////////////////////////////////
353 // Function name : Time_Clock::operator==
354 // Description : .. is time equal
355 // Return type : inline bool
356 // Argument : const Time_Clock &time
357 //////////////////////////////////////////////////////////////
358 inline bool Time_Clock::operator==(const Time_Clock &time) const
359 {
360  return ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec == time._my_time.tv_usec));
361 }
362 //////////////////////////////////////////////////////////////
363 // Function name : Time_Clock::operator!=
364 // Description : .is time !=
365 // Return type : inline bool
366 // Argument : const Time_Clock &time
367 //////////////////////////////////////////////////////////////
368 inline bool Time_Clock::operator!=(const Time_Clock &time) const
369 {
370  return ((_my_time.tv_sec != time._my_time.tv_sec) || (_my_time.tv_usec != time._my_time.tv_usec));
371 }
372 
373 //////////////////////////////////////////////////////////////
374 // Function name : Time_Clock::operator<
375 // Description :
376 // Return type : inline bool
377 // Argument : const Time_Clock &time
378 //////////////////////////////////////////////////////////////
379 inline bool Time_Clock::operator<(const Time_Clock &time) const
380 {
381  return ((_my_time.tv_sec < time._my_time.tv_sec) ||
382  ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec < time._my_time.tv_usec)));
383 }
384 //////////////////////////////////////////////////////////////
385 // Function name : Time_Clock::operator>
386 // Description :
387 // Return type : inline bool
388 // Argument : const Time_Clock &time
389 //////////////////////////////////////////////////////////////
390 inline bool Time_Clock::operator>(const Time_Clock &time) const
391 {
392  return ((_my_time.tv_sec > time._my_time.tv_sec) ||
393  ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec > time._my_time.tv_usec)));
394 }
395 //////////////////////////////////////////////////////////////
396 // Function name : Time_Clock::operator<=
397 // Description :
398 // Return type : inline bool
399 // Argument : const Time_Clock &time
400 //////////////////////////////////////////////////////////////
401 inline bool Time_Clock::operator<=(const Time_Clock &time) const
402 {
403  return ((_my_time.tv_sec < time._my_time.tv_sec) ||
404  ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec <= time._my_time.tv_usec)));
405 }
406 //////////////////////////////////////////////////////////////
407 // Function name : Time_Clock::operator>=
408 // Description :
409 // Return type : inline bool
410 // Argument : const Time_Clock &time
411 //////////////////////////////////////////////////////////////
412 inline bool Time_Clock::operator>=(const Time_Clock &time) const
413 {
414  return ((_my_time.tv_sec > time._my_time.tv_sec) ||
415  ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec >= time._my_time.tv_usec)));
416 }
417 //////////////////////////////////////////////////////////////
418 // Function name : Time_Clock& Time_Clock::operator=
419 // Description :
420 // Return type : inline const
421 // Argument : const Time_Clock& timeSrc
422 //////////////////////////////////////////////////////////////
423 inline const Time_Clock& Time_Clock::operator=(const Time_Clock& timeSrc)
424 {
425  if (&timeSrc == this)
426  return * this;
427 
428  _my_time = timeSrc._my_time;
429  return *this;
430 }
431 //////////////////////////////////////////////////////////////
432 // Function name : Time_Clock& Time_Clock::operator=
433 // Description :
434 // Return type : inline const
435 // Argument : time_t t
436 //////////////////////////////////////////////////////////////
437 inline const Time_Clock& Time_Clock::operator=(time_t t)
438 {
439  _my_time.tv_sec = (long)t;
440  _my_time.tv_usec = 0;
441  return *this;
442 }
443 //////////////////////////////////////////////////////////////
444 // Function name : Time_Clock::GetTime
445 // Description :
446 // Return type : inline time_t
447 //////////////////////////////////////////////////////////////
448 inline time_t Time_Clock::GetTime() const
449 {
450  return _my_time.tv_sec;
451 }
452 //////////////////////////////////////////////////////////////
453 // Function name : Time_Clock::GetYear
454 // Description :
455 // Return type : inline int
456 //////////////////////////////////////////////////////////////
457 inline int Time_Clock::GetYear() const
458 {
459  return (GetLocalTm(NULL)->tm_year) + 1900;
460 }
461 //////////////////////////////////////////////////////////////
462 // Function name : Time_Clock::GetMonth
463 // Description :
464 // Return type : inline int
465 //////////////////////////////////////////////////////////////
466 inline int Time_Clock::GetMonth() const
467 {
468  return GetLocalTm(NULL)->tm_mon + 1;
469 }
470 //////////////////////////////////////////////////////////////
471 // Function name : Time_Clock::GetDay
472 // Description :
473 // Return type : inline int
474 //////////////////////////////////////////////////////////////
475 inline int Time_Clock::GetDay() const
476 {
477  return GetLocalTm(NULL)->tm_mday;
478 }
479 //////////////////////////////////////////////////////////////
480 // Function name : Time_Clock::GetHour
481 // Description :
482 // Return type : inline int
483 //////////////////////////////////////////////////////////////
484 inline int Time_Clock::GetHour() const
485 {
486  return GetLocalTm(NULL)->tm_hour;
487 }
488 //////////////////////////////////////////////////////////////
489 // Function name : Time_Clock::GetMinute
490 // Description :
491 // Return type : inline int
492 //////////////////////////////////////////////////////////////
493 inline int Time_Clock::GetMinute() const
494 {
495  return GetLocalTm(NULL)->tm_min;
496 }
497 //////////////////////////////////////////////////////////////
498 // Function name : Time_Clock::GetSecond
499 // Description :
500 // Return type : inline int
501 //////////////////////////////////////////////////////////////
502 inline int Time_Clock::GetSecond() const
503 {
504  return GetLocalTm(NULL)->tm_sec;
505 }
506 //////////////////////////////////////////////////////////////
507 // Function name : Time_Clock::GetDayOfWeek
508 // Description :
509 // Return type : inline int
510 //////////////////////////////////////////////////////////////
511 inline int Time_Clock::GetDayOfWeek() const
512 {
513  return GetLocalTm(NULL)->tm_wday + 1;
514 }
515 
516 #endif //__Time_H__
std::string Format(const char *pFormat) const
Used to allow access to the "C" library strftime functions.
Definition: time_clock.h:231
int GetDay() const
Return type : inline int.
Definition: time_clock.h:475
const Time_Clock & operator+=(const Time_Span &Time_Span)
Return type : inline const Argument : Time_Span &Time_Span.
Definition: time_general.h:62
int GetMinute() const
Return type : inline int.
Definition: time_clock.h:493
int GetDayOfWeek() const
Return type : inline int.
Definition: time_clock.h:511
bool operator<=(const Time_Clock &time) const
Return type : inline bool Argument : const Time_Clock &time.
Definition: time_clock.h:401
bool operator==(const Time_Clock &time) const
Definition: time_clock.h:358
bool operator!=(const Time_Clock &time) const
.is time != Return type : inline bool Argument : const Time_Clock &time
Definition: time_clock.h:368
time_t GetTime() const
Return type : inline time_t.
Definition: time_clock.h:448
std::string FormatGmt(const char *pFormat) const
A Wraper to.
Definition: time_clock.h:279
struct tm * GetLocalTm(struct tm *ptm=NULL) const
Gets The local time in a tm structre from the internal time value.
Definition: time_clock.h:208
bool operator>=(const Time_Clock &time) const
Return type : inline bool Argument : const Time_Clock &time.
Definition: time_clock.h:412
int GetHour() const
Return type : inline int.
Definition: time_clock.h:484
int GetSecond() const
Return type : inline int.
Definition: time_clock.h:502
void ToCurrentTime()
Load this object with the current OS time Return type : inline void Argument : void.
Definition: time_clock.h:179
bool operator<(const Time_Clock &time) const
Return type : inline bool Argument : const Time_Clock &time.
Definition: time_clock.h:379
struct tm * GetGmtTm(struct tm *ptm=NULL) const
Access the stored time and convers to a struct tm format If storage location is specified then it wil...
Definition: time_clock.h:191
const Time_Clock & operator-=(const Time_Span &Time_Span)
Return type : inline const Argument : Time_Span &Time_Span.
Definition: time_general.h:99
bool operator>(const Time_Clock &time) const
Return type : inline bool Argument : const Time_Clock &time.
Definition: time_clock.h:390
int GetMonth() const
Return type : inline int.
Definition: time_clock.h:466
const Time_Clock & operator=(const Time_Clock &timeSrc)
Return type : inline const Argument : const Time_Clock& timeSrc.
Definition: time_clock.h:423
Time_Clock()
Return type : inline.
Definition: time_clock.h:169
int GetYear() const
Return type : inline int.
Definition: time_clock.h:457
void Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds=0, int nDST=-1)
Return type : inline Argument : int nYear Argument : int nMonth Argument : int nDay Argument : int nH...
Definition: time_clock.h:137
static Time_Clock GetCurrentTime()
The Default no param constructor.
Definition: time_clock.h:160