Panda3D
|
00001 #ifndef __TIME_BASE_H__ 00002 #define __TIME_BASE_H__ 00003 ///////////////////////////////////////////////////////////////////// 00004 // Functions To support General Time Managment. And to allow for cross platform use. 00005 // 00006 // 00007 // Today Really Two Base classes and one convience class 00008 // 00009 // Time_Clock = The clock time down to micro seconds.. 00010 // 00011 // Time_Span = Delta Time to the Mico Second.. 00012 // 00013 // Time_Out = Help timer ............count down a duration. 00014 // 00015 // I realize TimeClock is really an implied delta to EPOCH. I have chosen to implement it this way. 00016 // it may be apropriate to convert it all to delta times with an EPOCk constant and 00017 // functions that can handle the EPOCK to current time. 00018 // All though this is probably the "right" implementation most coders do not 00019 // think of clock time in this fashon. 00020 // 00021 // 00022 // General Observation.. 00023 // 00024 // Windows 2k and Linux are really slow (~250k a sec) at returning the current system time ?? 00025 // So use time functions that grab the current system time sparingly ?? 00026 // 00027 //////////////////////////////////////////////////////////////////////////////// 00028 #ifdef WIN32 00029 #include <winsock2.h> 00030 #include <wtypes.h> 00031 #include <sys/types.h> 00032 #include <sys/timeb.h> 00033 #else 00034 #include <sys/time.h> 00035 #endif 00036 #include <time.h> 00037 #include <string> 00038 #include <assert.h> 00039 00040 enum { USEC = 1000000 }; 00041 ////////////////////////////////////////////////////////////// 00042 // Function name : NormalizeTime 00043 // Description : 00044 // Return type : inline void 00045 // Argument : timeval &in 00046 ////////////////////////////////////////////////////////////// 00047 inline void NormalizeTime(timeval &in) 00048 { 00049 while (in.tv_usec >= USEC) 00050 { 00051 in.tv_usec -= USEC; 00052 in.tv_sec++; 00053 } 00054 00055 while (in.tv_usec < 0) 00056 { 00057 in.tv_usec += USEC; 00058 in.tv_sec--; 00059 } 00060 } 00061 ////////////////////////////////////////////////////////////// 00062 // Function name : TimeDif 00063 // Description : 00064 // Return type : inline void 00065 // Argument : const struct timeval &start 00066 // Argument : const struct timeval &fin 00067 // Argument : struct timeval &answer 00068 ////////////////////////////////////////////////////////////// 00069 inline void TimeDif(const struct timeval &start, const struct timeval &fin, struct timeval &answer) 00070 { 00071 answer.tv_usec = fin.tv_usec - start.tv_usec; 00072 answer.tv_sec = fin.tv_sec - start.tv_sec; 00073 NormalizeTime(answer); 00074 } 00075 ////////////////////////////////////////////////////////////// 00076 // Function name : TimeAdd 00077 // Description : 00078 // Return type : inline void 00079 // Argument : const struct timeval &start 00080 // Argument : const struct timeval &delta 00081 // Argument : struct timeval &answer 00082 ////////////////////////////////////////////////////////////// 00083 inline void TimeAdd(const struct timeval &start, const struct timeval &delta, struct timeval &answer) 00084 { 00085 answer.tv_usec = start.tv_usec + delta.tv_usec; 00086 answer.tv_sec = start.tv_sec + delta.tv_sec; 00087 NormalizeTime(answer); 00088 } 00089 00090 #ifdef WIN32 00091 //////////////////////////////////////////////////////////////// 00092 // 00093 // Lets make Windows think it is a unix machine :) 00094 // 00095 ////////////////////////////////////////////////////////////// 00096 // Function name : gettimeofday 00097 // Description : 00098 // Return type : inline int 00099 // Argument : struct timeval *tv 00100 // Argument : void * trash 00101 ////////////////////////////////////////////////////////////// 00102 inline int gettimeofday(struct timeval *tv, void * trash) 00103 { 00104 struct timeb timeb; 00105 ftime( &timeb); 00106 tv->tv_sec = (long)timeb.time; 00107 tv->tv_usec = (unsigned int)timeb.millitm * 1000; 00108 return 0; 00109 } 00110 #endif 00111 00112 #include "time_clock.h" 00113 #include "time_span.h" 00114 #include "time_general.h" 00115 #include "time_out.h" 00116 00117 #endif //__TIME_BASE_H__