Panda3D
|
00001 #ifndef __TIME_SPAN_H__ 00002 #define __TIME_SPAN_H__ 00003 ////////////////////////////////////////////////////// 00004 // Class : Time_Span 00005 // 00006 // Description: 00007 ////////////////////////////////////////////////////// 00008 class Time_Span 00009 { 00010 public: 00011 // Constructors 00012 Time_Span() 00013 { 00014 } 00015 00016 Time_Span(struct timeval time) 00017 { 00018 _my_time = time;NormalizeTime(_my_time); 00019 } 00020 00021 Time_Span(time_t time); 00022 Time_Span(long lDays, int nHours, int nMins, int nSecs, int usecs); 00023 Time_Span(long seconds, int usecs ) ; 00024 Time_Span(const Time_Span& Time_SpanSrc); 00025 Time_Span(const Time_Clock& Time_SpanSrc); 00026 Time_Span(PN_stdfloat Seconds); 00027 00028 /////////////////// 00029 00030 const Time_Span& operator=(const Time_Span& Time_SpanSrc); 00031 00032 // Attributes 00033 // extract parts 00034 long GetDays() const; // total # of days 00035 long GetTotalHours() const; 00036 int GetHours() const; 00037 long GetTotalMinutes() const; 00038 int GetMinutes() const; 00039 long GetTotalSeconds() const; 00040 int GetSeconds() const; 00041 long GetTotalMSeconds() const; 00042 long GetTotal100Seconds() const; 00043 long GetMSeconds() const; 00044 00045 // Operations 00046 // time math 00047 const Time_Span& operator+=(Time_Span &Time_Span); 00048 const Time_Span& operator-=(Time_Span &Time_Span); 00049 bool operator==(Time_Span &Time_Span) const; 00050 bool operator!=(Time_Span &Time_Span) const; 00051 bool operator<(Time_Span &Time_Span) const; 00052 bool operator>(Time_Span &Time_Span) const; 00053 bool operator<=(Time_Span &Time_Span) const; 00054 bool operator>=(Time_Span &Time_Span) const; 00055 const timeval & GetTval() const 00056 { 00057 return _my_time; 00058 } 00059 00060 00061 void Set(long lDays, int nHours, int nMins, int nSecs, int usecs); 00062 00063 std::string Format(char *pFormat) const; 00064 private: 00065 struct timeval _my_time; 00066 friend class Time_Clock; 00067 }; 00068 ////////////////////////////////////////////////////////////// 00069 // Function name : Time_Span::Time_Span 00070 // Description : 00071 ////////////////////////////////////////////////////////////// 00072 inline Time_Span::Time_Span(long seconds, int usecs) 00073 { 00074 _my_time.tv_sec = seconds; 00075 _my_time.tv_usec = usecs; 00076 NormalizeTime(_my_time); 00077 } 00078 ////////////////////////////////////////////////////////////// 00079 // Function name : Time_Span::Time_Span 00080 // Description : 00081 ////////////////////////////////////////////////////////////// 00082 inline Time_Span::Time_Span(time_t time) 00083 { 00084 _my_time.tv_usec = 0; 00085 _my_time.tv_sec = (long)time; 00086 } 00087 ////////////////////////////////////////////////////////////// 00088 // Function name : Time_Span::Time_Span 00089 // Description : 00090 ////////////////////////////////////////////////////////////// 00091 inline Time_Span::Time_Span(PN_stdfloat Seconds) 00092 { 00093 _my_time.tv_sec = (long)Seconds; // this truncats .. desired result.. 00094 _my_time.tv_usec = (long)((Seconds - (double)_my_time.tv_sec) * (double)USEC); 00095 } 00096 ////////////////////////////////////////////////////////////// 00097 // Function name : Time_Span::Time_Span 00098 // Description : 00099 ////////////////////////////////////////////////////////////// 00100 inline Time_Span::Time_Span(long lDays, int nHours, int nMins, int nSecs, int usecs) 00101 { 00102 _my_time.tv_sec = nSecs + 60 * (nMins + 60 * (nHours + 24 * lDays)); 00103 _my_time.tv_usec = usecs; 00104 00105 } 00106 00107 ////////////////////////////////////////////////////////////// 00108 // Function name : Time_Span::Set 00109 // Description : 00110 ////////////////////////////////////////////////////////////// 00111 inline void Time_Span::Set(long lDays, int nHours, int nMins, int nSecs, int usecs) 00112 { 00113 _my_time.tv_sec = nSecs + 60 * (nMins + 60 * (nHours + 24 * lDays)); 00114 _my_time.tv_usec = usecs; 00115 00116 } 00117 00118 ////////////////////////////////////////////////////////////// 00119 // Function name : Time_Span::Time_Span 00120 // Description : 00121 ////////////////////////////////////////////////////////////// 00122 inline Time_Span::Time_Span(const Time_Span& Time_SpanSrc) 00123 { 00124 _my_time = Time_SpanSrc._my_time; 00125 } 00126 00127 ////////////////////////////////////////////////////////////// 00128 // Function name : Time_Span::Time_Span 00129 // Description : 00130 ////////////////////////////////////////////////////////////// 00131 inline Time_Span::Time_Span(const Time_Clock& Time_SpanSrc) 00132 { 00133 _my_time = Time_SpanSrc._my_time; 00134 } 00135 00136 ////////////////////////////////////////////////////////////// 00137 // Function name : Time_Span& Time_Span::operator= 00138 // Description : 00139 ////////////////////////////////////////////////////////////// 00140 inline const Time_Span& Time_Span::operator=(const Time_Span& Time_SpanSrc) 00141 { 00142 if (&Time_SpanSrc == this) 00143 return * this; 00144 _my_time = Time_SpanSrc._my_time; return *this; 00145 } 00146 00147 ////////////////////////////////////////////////////////////// 00148 // Function name : Time_Span::GetDays 00149 // Description : 00150 ////////////////////////////////////////////////////////////// 00151 inline long Time_Span::GetDays() const 00152 { 00153 return _my_time.tv_sec / (24*3600L); 00154 } 00155 00156 ////////////////////////////////////////////////////////////// 00157 // Function name : Time_Span::GetTotalHours 00158 // Description : 00159 ////////////////////////////////////////////////////////////// 00160 inline long Time_Span::GetTotalHours() const 00161 { 00162 return _my_time.tv_sec / 3600; 00163 } 00164 00165 ////////////////////////////////////////////////////////////// 00166 // Function name : Time_Span::GetHours 00167 // Description : 00168 ////////////////////////////////////////////////////////////// 00169 inline int Time_Span::GetHours() const 00170 { 00171 return (int)(GetTotalHours() - GetDays()*24); 00172 } 00173 00174 ////////////////////////////////////////////////////////////// 00175 // Function name : Time_Span::GetTotalMinutes 00176 // Description : 00177 ////////////////////////////////////////////////////////////// 00178 inline long Time_Span::GetTotalMinutes() const 00179 { 00180 return _my_time.tv_sec / 60; 00181 } 00182 00183 ////////////////////////////////////////////////////////////// 00184 // Function name : Time_Span::GetMinutes 00185 // Description : 00186 ////////////////////////////////////////////////////////////// 00187 inline int Time_Span::GetMinutes() const 00188 { 00189 return (int)(GetTotalMinutes() - GetTotalHours()*60); 00190 } 00191 00192 ////////////////////////////////////////////////////////////// 00193 // Function name : Time_Span::GetTotalSeconds 00194 // Description : 00195 ////////////////////////////////////////////////////////////// 00196 inline long Time_Span::GetTotalSeconds() const 00197 { 00198 return _my_time.tv_sec; 00199 } 00200 00201 ////////////////////////////////////////////////////////////// 00202 // Function name : Time_Span::GetTotalMSeconds 00203 // Description : 00204 ////////////////////////////////////////////////////////////// 00205 inline long Time_Span::GetTotalMSeconds() const 00206 { 00207 return (_my_time.tv_sec * 1000) + (_my_time.tv_usec / 1000); 00208 } 00209 00210 00211 inline long Time_Span::GetTotal100Seconds() const 00212 { 00213 return (_my_time.tv_sec * 100) + (_my_time.tv_usec / 10000); 00214 } 00215 00216 00217 00218 ////////////////////////////////////////////////////////////// 00219 // Function name : Time_Span::GetTotalMSeconds 00220 // Description : 00221 ////////////////////////////////////////////////////////////// 00222 inline long Time_Span::GetMSeconds() const 00223 { 00224 return (_my_time.tv_usec / 1000); 00225 } 00226 00227 00228 ////////////////////////////////////////////////////////////// 00229 // Function name : Time_Span::GetSeconds 00230 // Description : 00231 ////////////////////////////////////////////////////////////// 00232 inline int Time_Span::GetSeconds() const 00233 { 00234 return (int)(GetTotalSeconds() - GetTotalMinutes()*60); 00235 } 00236 00237 ////////////////////////////////////////////////////////////// 00238 // Function name : TimeDifference 00239 // Description : 00240 ////////////////////////////////////////////////////////////// 00241 inline Time_Span TimeDifference(const Time_Span &Time_Span1, const Time_Span &Time_Span2) 00242 { 00243 timeval ans; 00244 TimeDif(Time_Span2.GetTval(), Time_Span1.GetTval(), ans); 00245 return Time_Span(ans); 00246 } 00247 00248 ////////////////////////////////////////////////////////////// 00249 // Function name : TimeAddition 00250 // Description : 00251 ////////////////////////////////////////////////////////////// 00252 inline Time_Span TimeAddition(const Time_Span &Time_Span1, const Time_Span &Time_Span2) 00253 { 00254 timeval ans; 00255 TimeAdd(Time_Span2.GetTval(), Time_Span1.GetTval(), ans); 00256 return Time_Span(ans); 00257 } 00258 00259 ////////////////////////////////////////////////////////////// 00260 // Function name : Time_Span& Time_Span::operator+= 00261 // Description : 00262 ////////////////////////////////////////////////////////////// 00263 inline const Time_Span& Time_Span::operator+=(Time_Span &Time_Span) 00264 { 00265 _my_time.tv_usec += Time_Span._my_time.tv_usec; 00266 _my_time.tv_sec += Time_Span._my_time.tv_sec; 00267 NormalizeTime(_my_time); 00268 return *this; 00269 } 00270 00271 ////////////////////////////////////////////////////////////// 00272 // Function name : Time_Span& Time_Span::operator-= 00273 // Description : 00274 ////////////////////////////////////////////////////////////// 00275 inline const Time_Span& Time_Span::operator-=(Time_Span &Time_Span) 00276 { 00277 _my_time.tv_usec -= Time_Span._my_time.tv_usec; 00278 _my_time.tv_sec -= Time_Span._my_time.tv_sec; 00279 NormalizeTime(_my_time); 00280 return *this; 00281 } 00282 00283 ////////////////////////////////////////////////////////////// 00284 // Function name : Time_Span::operator== 00285 // Description : 00286 ////////////////////////////////////////////////////////////// 00287 inline bool Time_Span::operator==(Time_Span &Time_Span) const 00288 { 00289 return ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec == Time_Span._my_time.tv_usec)); 00290 } 00291 00292 ////////////////////////////////////////////////////////////// 00293 // Function name : Time_Span::operator!= 00294 // Description : 00295 ////////////////////////////////////////////////////////////// 00296 inline bool Time_Span::operator!=(Time_Span &Time_Span) const 00297 { 00298 return ((_my_time.tv_sec != Time_Span._my_time.tv_sec) || (_my_time.tv_usec != Time_Span._my_time.tv_usec)); 00299 } 00300 00301 ////////////////////////////////////////////////////////////// 00302 // Function name : Time_Span::operator< 00303 // Description : 00304 ////////////////////////////////////////////////////////////// 00305 inline bool Time_Span::operator<(Time_Span &Time_Span) const 00306 { 00307 return ((_my_time.tv_sec < Time_Span._my_time.tv_sec) || 00308 ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec < Time_Span._my_time.tv_usec))); 00309 } 00310 00311 ////////////////////////////////////////////////////////////// 00312 // Function name : Time_Span::operator> 00313 // Description : 00314 ////////////////////////////////////////////////////////////// 00315 inline bool Time_Span::operator>(Time_Span &Time_Span) const 00316 { 00317 return ((_my_time.tv_sec > Time_Span._my_time.tv_sec) || 00318 ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec > Time_Span._my_time.tv_usec))); 00319 } 00320 00321 ////////////////////////////////////////////////////////////// 00322 // Function name : Time_Span::operator<= 00323 // Description : 00324 ////////////////////////////////////////////////////////////// 00325 inline bool Time_Span::operator<=(Time_Span &Time_Span) const 00326 { 00327 return ((_my_time.tv_sec < Time_Span._my_time.tv_sec) || 00328 ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec <= Time_Span._my_time.tv_usec))); 00329 } 00330 00331 ////////////////////////////////////////////////////////////// 00332 // Function name : Time_Span::operator>= 00333 // Description : 00334 ////////////////////////////////////////////////////////////// 00335 inline bool Time_Span::operator>=(Time_Span &Time_Span) const 00336 { 00337 return ((_my_time.tv_sec > Time_Span._my_time.tv_sec) || 00338 ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec >= Time_Span._my_time.tv_usec))); 00339 } 00340 00341 ////////////////////////////////////////////////////////////// 00342 // Function name : Time_Span::Format 00343 // Description : 00344 ////////////////////////////////////////////////////////////// 00345 inline std::string Time_Span::Format(char * pFormat) const 00346 // formatting Time_Spans is a little trickier than formatting 00347 // * we are only interested in relative time formats, ie. it is illegal 00348 // to format anything dealing with absolute time (i.e. years, months, 00349 // day of week, day of year, timezones, ...) 00350 // * the only valid formats: 00351 // %D - # of days -- NEW !!! 00352 // %H - hour in 24 hour format 00353 // %M - minute (0-59) 00354 // %S - seconds (0-59) 00355 // %% - percent sign 00356 // %N - nanosecs 00357 { 00358 char szBuffer[maxTimeBufferSize]; 00359 char ch; 00360 char * pch = szBuffer; 00361 00362 while ((ch = *pFormat++) != '\0') { 00363 assert(pch < &szBuffer[maxTimeBufferSize]); 00364 if (ch == '%') { 00365 switch (ch = *pFormat++) { 00366 default: 00367 assert(false); // probably a bad format character 00368 case '%': 00369 *pch++ = ch; 00370 break; 00371 case 'D': 00372 pch += sprintf(pch, "%ld", GetDays()); 00373 break; 00374 case 'H': 00375 pch += sprintf(pch, "%02d", GetHours()); 00376 break; 00377 case 'M': 00378 pch += sprintf(pch, "%02d", GetMinutes()); 00379 break; 00380 case 'S': 00381 pch += sprintf(pch, "%02d", GetSeconds()); 00382 break; 00383 case 'N': 00384 pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000)); 00385 break; 00386 } 00387 } else { 00388 *pch++ = ch; 00389 } 00390 } 00391 00392 *pch = '\0'; 00393 return std::string(szBuffer); 00394 } 00395 00396 #endif //__TIME_SPAN_H__