Panda3D
 All Classes Functions Variables Enumerations
time_clock.h
00001 #ifndef __Time_H__
00002 #define __Time_H__ 
00003 //////////////////////////////////////////////////////
00004 // Class : Time_Clock
00005 //
00006 // Description:
00007 //  This class is to provide a consistant interface and storage to
00008 //  clock time .. Epoch based time to the second
00009 //
00010 // jan-2000 .. rhh changinging all time to use sub second timing...
00011 //
00012 //
00013 //////////////////////////////////////////////////////
00014 
00015 
00016 #include <stdio.h>
00017 
00018 class Time_Span;
00019 
00020 class Time_Clock
00021 {
00022     friend class Time_Span;
00023     
00024 public:
00025     // Constructors
00026     static Time_Clock GetCurrentTime();
00027     void ToCurrentTime();
00028     Time_Clock( timeval &in_mytime)
00029     {
00030         _my_time = in_mytime;
00031     };
00032     Time_Clock();
00033     Time_Clock(time_t time);
00034     Time_Clock(long secs, long usecs);
00035     Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
00036     Time_Clock(const Time_Clock& timeSrc);
00037     
00038     inline const Time_Clock& operator=(const Time_Clock& timeSrc);
00039     inline const Time_Clock& operator=(time_t t);
00040     
00041     // Attributes
00042     struct tm* GetGmtTm(struct tm* ptm = NULL) const;
00043     struct tm* GetLocalTm(struct tm* ptm = NULL) const;
00044     
00045     time_t GetTime() const;
00046     int GetYear() const;
00047     int GetMonth() const;       // month of year (1 = Jan)
00048     int GetDay() const;         // day of month
00049     int GetHour() const;
00050     int GetMinute() const;
00051     int GetSecond() const;
00052     int GetDayOfWeek() const;   // 1=Sun, 2=Mon, ..., 7=Sat
00053     
00054     void Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
00055     
00056     
00057     // Operations
00058     // time math
00059     const Time_Clock& operator+=(const Time_Span &Time_Span);
00060     const Time_Clock& operator-=(const Time_Span &Time_Span);
00061     bool operator==(const Time_Clock &time) const;
00062     bool operator!=(const Time_Clock &time) const;
00063     bool operator<(const Time_Clock &time) const;
00064     bool operator>(const Time_Clock &time) const;
00065     bool operator<=(const Time_Clock &time) const;
00066     bool operator>=(const Time_Clock &time) const;
00067     
00068     
00069     time_t GetTime_t()
00070     {
00071         return _my_time.tv_sec;
00072     };
00073     unsigned long GetUsecPart()
00074     {
00075         return _my_time.tv_usec;
00076     };
00077     
00078     // formatting using "C" strftime
00079     std::string Format(const char * pFormat) const;
00080     std::string FormatGmt(const char * pFormat) const;
00081     const timeval & GetTval()
00082     {
00083         return _my_time;
00084     };
00085     const timeval & GetTval() const
00086     {
00087         return _my_time;
00088     } ;
00089     private:
00090         struct timeval _my_time;
00091 };
00092 /////////////////////////////////////////////////////////////////////////////
00093 // Time_Clock - absolute time
00094 
00095 /////////////////////////////////////////////////////////////
00096 // Function name : Time_Clock::Time_Clock
00097 // Description     : Construction from parts
00098 // Argument         : int nYear
00099 // Argument         : int nMonth
00100 // Argument         : int nDay
00101 // Argument         : int nHour
00102 // Argument         : int nMin
00103 // Argument         : int nSec
00104 // Argument         : int nDST
00105 //////////////////////////////////////////////////////////
00106 inline Time_Clock::Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
00107 {
00108     struct tm atm;
00109     atm.tm_sec = nSec;
00110     atm.tm_min = nMin;
00111     atm.tm_hour = nHour;
00112     assert(nDay >= 1 && nDay <= 31);
00113     atm.tm_mday = nDay;
00114     assert(nMonth >= 1 && nMonth <= 12);
00115     atm.tm_mon = nMonth - 1;        // tm_mon is 0 based
00116     assert(nYear >= 1900);
00117     atm.tm_year = nYear - 1900;     // tm_year is 1900 based
00118     atm.tm_isdst = nDST;
00119     _my_time.tv_sec = (long)mktime(&atm);
00120     assert(_my_time.tv_sec != -1);       // indicates an illegal input time
00121     _my_time.tv_usec = microseconds;
00122     assert(_my_time.tv_usec < 1000000);
00123 }
00124 //////////////////////////////////////////////////////////////
00125 // Function name : Time_Clock::Set
00126 // Description     :
00127 // Return type  : inline
00128 // Argument         : int nYear
00129 // Argument         : int nMonth
00130 // Argument         : int nDay
00131 // Argument         : int nHour
00132 // Argument         : int nMin
00133 // Argument         : int nSec
00134 // Argument         : unsigned long microseconds
00135 // Argument         : int nDST
00136 //////////////////////////////////////////////////////////////
00137 inline void Time_Clock::Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
00138 {
00139     struct tm atm;
00140     atm.tm_sec = nSec;
00141     atm.tm_min = nMin;
00142     atm.tm_hour = nHour;
00143     assert(nDay >= 1 && nDay <= 31);
00144     atm.tm_mday = nDay;
00145     assert(nMonth >= 1 && nMonth <= 12);
00146     atm.tm_mon = nMonth - 1;        // tm_mon is 0 based
00147     assert(nYear >= 1900);
00148     atm.tm_year = nYear - 1900;     // tm_year is 1900 based
00149     atm.tm_isdst = nDST;
00150     _my_time.tv_sec = (long)mktime(&atm);
00151     assert(_my_time.tv_sec != -1);       // indicates an illegal input time
00152     _my_time.tv_usec = microseconds;
00153     assert(_my_time.tv_usec < 1000000);
00154 }
00155 /////////////////////////////////////////////////////////////
00156 // Function name : Time_Clock::GetCurrentTime
00157 // Description     : The Default no param constructor.. Will set time to current system time
00158 // Return type  : Time_Clock
00159 //////////////////////////////////////////////////////////
00160 inline Time_Clock Time_Clock::GetCurrentTime()
00161 {
00162     return Time_Clock();
00163 }
00164 //////////////////////////////////////////////////////////////
00165 // Function name : Time_Clock::Time_Clock
00166 // Description     :
00167 // Return type  : inline
00168 //////////////////////////////////////////////////////////////
00169 inline Time_Clock::Time_Clock()
00170 {
00171     gettimeofday(&_my_time, NULL);
00172 }
00173 //////////////////////////////////////////////////////////////
00174 // Function name : Time_Clock::ToCurrentTime
00175 // Description     : Load this object with the current OS time
00176 // Return type  : inline void
00177 // Argument         : void
00178 //////////////////////////////////////////////////////////////
00179 inline void Time_Clock::ToCurrentTime()
00180 {
00181     gettimeofday(&_my_time, NULL);
00182 }
00183 /////////////////////////////////////////////////////////////
00184 // Function name : Time_Clock::GetGmtTm
00185 // Description     :  Access the stored time and convers to a struct tm format
00186 //      If storage location is specified then it will stor information in the
00187 //      provided buffer else it will use the library's internal buffer space
00188 // Return type  : struct tm*
00189 // Argument         : struct tm* ptm
00190 //////////////////////////////////////////////////////////
00191 inline struct tm* Time_Clock::GetGmtTm(struct tm* ptm) const
00192 {
00193     if (ptm != NULL)
00194     {
00195         *ptm = *gmtime((const time_t *)&_my_time.tv_sec);
00196         return ptm;
00197     } else
00198         return gmtime((const time_t *)&_my_time.tv_sec);
00199 }
00200 
00201 ////////////////////////////////////////////////////////////////////
00202 // Function name : Time_Clock::GetLocalTm
00203 // Description     :  Gets The local time in a tm structre from the internal time value
00204 //
00205 // Return type  : struct tm*
00206 // Argument         : struct tm* ptm
00207 ////////////////////////////////////////////////////////////////////
00208 inline struct tm* Time_Clock::GetLocalTm(struct tm* ptm) const
00209 {
00210     if (ptm != NULL)
00211     {
00212         struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
00213         if (ptmTemp == NULL)
00214             return NULL;    // indicates the _my_time.tv_sec was not initialized!
00215         *ptm = *ptmTemp;
00216         return ptm;
00217     } else
00218         return localtime((const time_t *)&_my_time.tv_sec);
00219 }
00220 /////////////////////////////////////////////////////////////////////////////
00221 // String formatting
00222 #define maxTimeBufferSize       4096
00223 // Verifies will fail if the needed buffer size is too large
00224 /////////////////////////////////////////////////////////////
00225 // Function name :  Time_Clock::Format
00226 // Description     :  Used to allow access to the "C" library strftime functions..
00227 //
00228 // Return type  : std::string
00229 // Argument         : char * pFormat
00230 //////////////////////////////////////////////////////////
00231 inline std::string Time_Clock::Format(const char * pFormat) const
00232 {
00233     
00234     char szBuffer[maxTimeBufferSize];
00235     char ch, ch1;
00236     char * pch = szBuffer;
00237     
00238     while ((ch = *pFormat++) != '\0') 
00239     {
00240         assert(pch < &szBuffer[maxTimeBufferSize]);
00241         if (ch == '%') 
00242         {
00243             switch (ch1 = *pFormat++) 
00244             {
00245             default:
00246                 *pch++ = ch;
00247                 *pch++ = ch1;
00248                 break;
00249             case 'N':
00250                 pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
00251                 break;
00252             }
00253         }
00254         else 
00255         {
00256             *pch++ = ch;
00257         }
00258     }
00259     
00260     *pch = '\0';
00261     
00262     char szBuffer1[maxTimeBufferSize];
00263     
00264     struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
00265     if (ptmTemp == NULL ||
00266         !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
00267         szBuffer1[0] = '\0';
00268     return std::string(szBuffer1);
00269 }
00270 //////////////////////////////////////////////////////////////
00271 // Function name :   Time_Clock::FormatGmt
00272 // Description     : A Wraper to
00273 //
00274 //   size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *timeptr );
00275 //
00276 // Return type  : inline std::string
00277 // Argument         : char * pFormat
00278 //////////////////////////////////////////////////////////////
00279 inline std::string Time_Clock::FormatGmt(const char * pFormat) const
00280 {
00281     
00282     char szBuffer[maxTimeBufferSize];
00283     char ch, ch1;
00284     char * pch = szBuffer;
00285     
00286     while ((ch = *pFormat++) != '\0') 
00287     {
00288         assert(pch < &szBuffer[maxTimeBufferSize]);
00289         if (ch == '%') 
00290         {
00291             switch (ch1 = *pFormat++) 
00292             {
00293             default:
00294                 *pch++ = ch;
00295                 *pch++ = ch1;
00296                 break;
00297             case 'N':
00298                 pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
00299                 break;
00300             }
00301         }
00302         else 
00303         {
00304             *pch++ = ch;
00305         }
00306     }
00307     *pch = '\0';
00308     
00309     char szBuffer1[maxTimeBufferSize];
00310     
00311     struct tm* ptmTemp = gmtime((const time_t *)&_my_time.tv_sec);
00312     if (ptmTemp == NULL ||
00313         !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
00314         szBuffer1[0] = '\0';
00315     return std::string(szBuffer1);
00316 }
00317 //////////////////////////////////////////////////////////////
00318 // Function name : Time_Clock::Time_Clock
00319 // Description     : The Constructor that take a time_t objext
00320 // Return type  : inline
00321 // Argument         : time_t time
00322 //////////////////////////////////////////////////////////////
00323 inline Time_Clock::Time_Clock(time_t time)
00324 {
00325     _my_time.tv_sec = (long)time;
00326     _my_time.tv_usec = 0;
00327 };
00328 //////////////////////////////////////////////////////////////
00329 // Function name : Time_Clock::Time_Clock
00330 // Description     : Constructor that takes in sec and usecs..
00331 // Return type  : inline
00332 // Argument         : long secs
00333 // Argument         : long usecs
00334 //////////////////////////////////////////////////////////////
00335 inline Time_Clock::Time_Clock(long secs, long usecs)
00336 {
00337     _my_time.tv_sec = secs;
00338     _my_time.tv_usec = usecs;
00339     NormalizeTime(_my_time);
00340 };
00341 //////////////////////////////////////////////////////////////
00342 // Function name : Time_Clock::Time_Clock
00343 // Description     :  yet another constructor
00344 // Return type  : inline
00345 // Argument         : const Time_Clock& timeSrc
00346 //////////////////////////////////////////////////////////////
00347 inline Time_Clock::Time_Clock(const Time_Clock& timeSrc)
00348 {
00349     _my_time.tv_sec = timeSrc._my_time.tv_sec;
00350     _my_time.tv_usec = timeSrc._my_time.tv_usec;
00351 }
00352 //////////////////////////////////////////////////////////////
00353 // Function name : Time_Clock::operator==
00354 // Description     : .. is time  equal
00355 // Return type  : inline bool
00356 // Argument         : const Time_Clock &time
00357 //////////////////////////////////////////////////////////////
00358 inline bool Time_Clock::operator==(const Time_Clock &time) const
00359 {
00360     return ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec == time._my_time.tv_usec));
00361 }
00362 //////////////////////////////////////////////////////////////
00363 // Function name : Time_Clock::operator!=
00364 // Description     :  .is time !=
00365 // Return type  : inline bool
00366 // Argument         : const Time_Clock &time
00367 //////////////////////////////////////////////////////////////
00368 inline bool Time_Clock::operator!=(const Time_Clock &time) const
00369 {
00370     return ((_my_time.tv_sec != time._my_time.tv_sec) || (_my_time.tv_usec != time._my_time.tv_usec));
00371 }
00372 
00373 //////////////////////////////////////////////////////////////
00374 // Function name : Time_Clock::operator<
00375 // Description     :
00376 // Return type  : inline bool
00377 // Argument         : const Time_Clock &time
00378 //////////////////////////////////////////////////////////////
00379 inline bool Time_Clock::operator<(const Time_Clock &time) const
00380 {
00381     return ((_my_time.tv_sec < time._my_time.tv_sec) ||
00382         ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec < time._my_time.tv_usec)));
00383 }
00384 //////////////////////////////////////////////////////////////
00385 // Function name : Time_Clock::operator>
00386 // Description     :
00387 // Return type  : inline bool
00388 // Argument         : const Time_Clock &time
00389 //////////////////////////////////////////////////////////////
00390 inline bool Time_Clock::operator>(const Time_Clock &time) const
00391 {
00392     return ((_my_time.tv_sec > time._my_time.tv_sec) ||
00393         ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec > time._my_time.tv_usec)));
00394 }
00395 //////////////////////////////////////////////////////////////
00396 // Function name : Time_Clock::operator<=
00397 // Description     :
00398 // Return type  : inline bool
00399 // Argument         : const Time_Clock &time
00400 //////////////////////////////////////////////////////////////
00401 inline bool Time_Clock::operator<=(const Time_Clock &time) const
00402 {
00403     return ((_my_time.tv_sec < time._my_time.tv_sec) ||
00404         ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec <= time._my_time.tv_usec)));
00405 }
00406 //////////////////////////////////////////////////////////////
00407 // Function name : Time_Clock::operator>=
00408 // Description     :
00409 // Return type  : inline bool
00410 // Argument         : const Time_Clock &time
00411 //////////////////////////////////////////////////////////////
00412 inline bool Time_Clock::operator>=(const Time_Clock &time) const
00413 {
00414     return ((_my_time.tv_sec > time._my_time.tv_sec) ||
00415         ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec >= time._my_time.tv_usec)));
00416 }
00417 //////////////////////////////////////////////////////////////
00418 // Function name : Time_Clock& Time_Clock::operator=
00419 // Description     :
00420 // Return type  : inline const
00421 // Argument         : const Time_Clock& timeSrc
00422 //////////////////////////////////////////////////////////////
00423 inline const Time_Clock& Time_Clock::operator=(const Time_Clock& timeSrc)
00424 {
00425     if (&timeSrc == this)
00426         return * this;
00427     
00428     _my_time = timeSrc._my_time;
00429     return *this;
00430 }
00431 //////////////////////////////////////////////////////////////
00432 // Function name : Time_Clock& Time_Clock::operator=
00433 // Description     :
00434 // Return type  : inline const
00435 // Argument         : time_t t
00436 //////////////////////////////////////////////////////////////
00437 inline const Time_Clock& Time_Clock::operator=(time_t t)
00438 {
00439     _my_time.tv_sec = (long)t;
00440     _my_time.tv_usec = 0;
00441     return *this;
00442 }
00443 //////////////////////////////////////////////////////////////
00444 // Function name : Time_Clock::GetTime
00445 // Description     :
00446 // Return type  : inline time_t
00447 //////////////////////////////////////////////////////////////
00448 inline time_t Time_Clock::GetTime() const
00449 {
00450     return _my_time.tv_sec;
00451 }
00452 //////////////////////////////////////////////////////////////
00453 // Function name : Time_Clock::GetYear
00454 // Description     :
00455 // Return type  : inline int
00456 //////////////////////////////////////////////////////////////
00457 inline int Time_Clock::GetYear() const
00458 {
00459     return (GetLocalTm(NULL)->tm_year) + 1900;
00460 }
00461 //////////////////////////////////////////////////////////////
00462 // Function name : Time_Clock::GetMonth
00463 // Description     :
00464 // Return type  : inline int
00465 //////////////////////////////////////////////////////////////
00466 inline int Time_Clock::GetMonth() const
00467 {
00468     return GetLocalTm(NULL)->tm_mon + 1;
00469 }
00470 //////////////////////////////////////////////////////////////
00471 // Function name : Time_Clock::GetDay
00472 // Description     :
00473 // Return type  : inline int
00474 //////////////////////////////////////////////////////////////
00475 inline int Time_Clock::GetDay() const
00476 {
00477     return GetLocalTm(NULL)->tm_mday;
00478 }
00479 //////////////////////////////////////////////////////////////
00480 // Function name : Time_Clock::GetHour
00481 // Description     :
00482 // Return type  : inline int
00483 //////////////////////////////////////////////////////////////
00484 inline int Time_Clock::GetHour() const
00485 {
00486     return GetLocalTm(NULL)->tm_hour;
00487 }
00488 //////////////////////////////////////////////////////////////
00489 // Function name : Time_Clock::GetMinute
00490 // Description     :
00491 // Return type  : inline int
00492 //////////////////////////////////////////////////////////////
00493 inline int Time_Clock::GetMinute() const
00494 {
00495     return GetLocalTm(NULL)->tm_min;
00496 }
00497 //////////////////////////////////////////////////////////////
00498 // Function name : Time_Clock::GetSecond
00499 // Description     :
00500 // Return type  : inline int
00501 //////////////////////////////////////////////////////////////
00502 inline int Time_Clock::GetSecond() const
00503 {
00504     return GetLocalTm(NULL)->tm_sec;
00505 }
00506 //////////////////////////////////////////////////////////////
00507 // Function name : Time_Clock::GetDayOfWeek
00508 // Description     :
00509 // Return type  : inline int
00510 //////////////////////////////////////////////////////////////
00511 inline int Time_Clock::GetDayOfWeek() const
00512 {
00513     return GetLocalTm(NULL)->tm_wday + 1;
00514 }
00515 
00516 #endif //__Time_H__
 All Classes Functions Variables Enumerations