Panda3D
time_base.h
1 #ifndef __TIME_BASE_H__
2 #define __TIME_BASE_H__
3 /////////////////////////////////////////////////////////////////////
4 // Functions To support General Time Managment. And to allow for cross platform use.
5 //
6 //
7 // Today Really Two Base classes and one convience class
8 //
9 // Time_Clock = The clock time down to micro seconds..
10 //
11 // Time_Span = Delta Time to the Mico Second..
12 //
13 // Time_Out = Help timer ............count down a duration.
14 //
15 // I realize TimeClock is really an implied delta to EPOCH. I have chosen to implement it this way.
16 // it may be apropriate to convert it all to delta times with an EPOCk constant and
17 // functions that can handle the EPOCK to current time.
18 // All though this is probably the "right" implementation most coders do not
19 // think of clock time in this fashon.
20 //
21 //
22 // General Observation..
23 //
24 // Windows 2k and Linux are really slow (~250k a sec) at returning the current system time ??
25 // So use time functions that grab the current system time sparingly ??
26 //
27 ////////////////////////////////////////////////////////////////////////////////
28 #ifdef WIN32
29 #include <winsock2.h>
30 #include <wtypes.h>
31 #include <sys/types.h>
32 #include <sys/timeb.h>
33 #else
34 #include <sys/time.h>
35 #endif
36 #include <time.h>
37 #include <string>
38 #include <assert.h>
39 
40 enum { USEC = 1000000 };
41 //////////////////////////////////////////////////////////////
42 // Function name : NormalizeTime
43 // Description :
44 // Return type : inline void
45 // Argument : timeval &in
46 //////////////////////////////////////////////////////////////
47 inline void NormalizeTime(timeval &in)
48 {
49  while (in.tv_usec >= USEC)
50  {
51  in.tv_usec -= USEC;
52  in.tv_sec++;
53  }
54 
55  while (in.tv_usec < 0)
56  {
57  in.tv_usec += USEC;
58  in.tv_sec--;
59  }
60 }
61 //////////////////////////////////////////////////////////////
62 // Function name : TimeDif
63 // Description :
64 // Return type : inline void
65 // Argument : const struct timeval &start
66 // Argument : const struct timeval &fin
67 // Argument : struct timeval &answer
68 //////////////////////////////////////////////////////////////
69 inline void TimeDif(const struct timeval &start, const struct timeval &fin, struct timeval &answer)
70 {
71  answer.tv_usec = fin.tv_usec - start.tv_usec;
72  answer.tv_sec = fin.tv_sec - start.tv_sec;
73  NormalizeTime(answer);
74 }
75 //////////////////////////////////////////////////////////////
76 // Function name : TimeAdd
77 // Description :
78 // Return type : inline void
79 // Argument : const struct timeval &start
80 // Argument : const struct timeval &delta
81 // Argument : struct timeval &answer
82 //////////////////////////////////////////////////////////////
83 inline void TimeAdd(const struct timeval &start, const struct timeval &delta, struct timeval &answer)
84 {
85  answer.tv_usec = start.tv_usec + delta.tv_usec;
86  answer.tv_sec = start.tv_sec + delta.tv_sec;
87  NormalizeTime(answer);
88 }
89 
90 #ifdef WIN32
91 ////////////////////////////////////////////////////////////////
92 //
93 // Lets make Windows think it is a unix machine :)
94 //
95 //////////////////////////////////////////////////////////////
96 // Function name : gettimeofday
97 // Description :
98 // Return type : inline int
99 // Argument : struct timeval *tv
100 // Argument : void * trash
101 //////////////////////////////////////////////////////////////
102 inline int gettimeofday(struct timeval *tv, void * trash)
103 {
104  struct timeb timeb;
105  ftime( &timeb);
106  tv->tv_sec = (long)timeb.time;
107  tv->tv_usec = (unsigned int)timeb.millitm * 1000;
108  return 0;
109 }
110 #endif
111 
112 #include "time_clock.h"
113 #include "time_span.h"
114 #include "time_general.h"
115 #include "time_out.h"
116 
117 #endif //__TIME_BASE_H__