Panda3D
time_span.h
1 #ifndef __TIME_SPAN_H__
2 #define __TIME_SPAN_H__
3 //////////////////////////////////////////////////////
4 // Class : Time_Span
5 //
6 // Description:
7 //////////////////////////////////////////////////////
8 class Time_Span
9 {
10 public:
11  // Constructors
12  Time_Span()
13  {
14  }
15 
16  Time_Span(struct timeval time)
17  {
18  _my_time = time;NormalizeTime(_my_time);
19  }
20 
21  Time_Span(time_t time);
22  Time_Span(long lDays, int nHours, int nMins, int nSecs, int usecs);
23  Time_Span(long seconds, int usecs ) ;
24  Time_Span(const Time_Span& Time_SpanSrc);
25  Time_Span(const Time_Clock& Time_SpanSrc);
26  Time_Span(PN_stdfloat Seconds);
27 
28  ///////////////////
29 
30  const Time_Span& operator=(const Time_Span& Time_SpanSrc);
31 
32  // Attributes
33  // extract parts
34  long GetDays() const; // total # of days
35  long GetTotalHours() const;
36  int GetHours() const;
37  long GetTotalMinutes() const;
38  int GetMinutes() const;
39  long GetTotalSeconds() const;
40  int GetSeconds() const;
41  long GetTotalMSeconds() const;
42  long GetTotal100Seconds() const;
43  long GetMSeconds() const;
44 
45  // Operations
46  // time math
47  const Time_Span& operator+=(Time_Span &Time_Span);
48  const Time_Span& operator-=(Time_Span &Time_Span);
49  bool operator==(Time_Span &Time_Span) const;
50  bool operator!=(Time_Span &Time_Span) const;
51  bool operator<(Time_Span &Time_Span) const;
52  bool operator>(Time_Span &Time_Span) const;
53  bool operator<=(Time_Span &Time_Span) const;
54  bool operator>=(Time_Span &Time_Span) const;
55  const timeval & GetTval() const
56  {
57  return _my_time;
58  }
59 
60 
61  void Set(long lDays, int nHours, int nMins, int nSecs, int usecs);
62 
63  std::string Format(char *pFormat) const;
64 private:
65  struct timeval _my_time;
66  friend class Time_Clock;
67 };
68 //////////////////////////////////////////////////////////////
69 // Function name : Time_Span::Time_Span
70 // Description :
71 //////////////////////////////////////////////////////////////
72 inline Time_Span::Time_Span(long seconds, int usecs)
73 {
74  _my_time.tv_sec = seconds;
75  _my_time.tv_usec = usecs;
76  NormalizeTime(_my_time);
77 }
78 //////////////////////////////////////////////////////////////
79 // Function name : Time_Span::Time_Span
80 // Description :
81 //////////////////////////////////////////////////////////////
82 inline Time_Span::Time_Span(time_t time)
83 {
84  _my_time.tv_usec = 0;
85  _my_time.tv_sec = (long)time;
86 }
87 //////////////////////////////////////////////////////////////
88 // Function name : Time_Span::Time_Span
89 // Description :
90 //////////////////////////////////////////////////////////////
91 inline Time_Span::Time_Span(PN_stdfloat Seconds)
92 {
93  _my_time.tv_sec = (long)Seconds; // this truncats .. desired result..
94  _my_time.tv_usec = (long)((Seconds - (double)_my_time.tv_sec) * (double)USEC);
95 }
96 //////////////////////////////////////////////////////////////
97 // Function name : Time_Span::Time_Span
98 // Description :
99 //////////////////////////////////////////////////////////////
100 inline Time_Span::Time_Span(long lDays, int nHours, int nMins, int nSecs, int usecs)
101 {
102  _my_time.tv_sec = nSecs + 60 * (nMins + 60 * (nHours + 24 * lDays));
103  _my_time.tv_usec = usecs;
104 
105 }
106 
107 //////////////////////////////////////////////////////////////
108 // Function name : Time_Span::Set
109 // Description :
110 //////////////////////////////////////////////////////////////
111 inline void Time_Span::Set(long lDays, int nHours, int nMins, int nSecs, int usecs)
112 {
113  _my_time.tv_sec = nSecs + 60 * (nMins + 60 * (nHours + 24 * lDays));
114  _my_time.tv_usec = usecs;
115 
116 }
117 
118 //////////////////////////////////////////////////////////////
119 // Function name : Time_Span::Time_Span
120 // Description :
121 //////////////////////////////////////////////////////////////
122 inline Time_Span::Time_Span(const Time_Span& Time_SpanSrc)
123 {
124  _my_time = Time_SpanSrc._my_time;
125 }
126 
127 //////////////////////////////////////////////////////////////
128 // Function name : Time_Span::Time_Span
129 // Description :
130 //////////////////////////////////////////////////////////////
131 inline Time_Span::Time_Span(const Time_Clock& Time_SpanSrc)
132 {
133  _my_time = Time_SpanSrc._my_time;
134 }
135 
136 //////////////////////////////////////////////////////////////
137 // Function name : Time_Span& Time_Span::operator=
138 // Description :
139 //////////////////////////////////////////////////////////////
140 inline const Time_Span& Time_Span::operator=(const Time_Span& Time_SpanSrc)
141 {
142  if (&Time_SpanSrc == this)
143  return * this;
144  _my_time = Time_SpanSrc._my_time; return *this;
145 }
146 
147 //////////////////////////////////////////////////////////////
148 // Function name : Time_Span::GetDays
149 // Description :
150 //////////////////////////////////////////////////////////////
151 inline long Time_Span::GetDays() const
152 {
153  return _my_time.tv_sec / (24*3600L);
154 }
155 
156 //////////////////////////////////////////////////////////////
157 // Function name : Time_Span::GetTotalHours
158 // Description :
159 //////////////////////////////////////////////////////////////
160 inline long Time_Span::GetTotalHours() const
161 {
162  return _my_time.tv_sec / 3600;
163 }
164 
165 //////////////////////////////////////////////////////////////
166 // Function name : Time_Span::GetHours
167 // Description :
168 //////////////////////////////////////////////////////////////
169 inline int Time_Span::GetHours() const
170 {
171  return (int)(GetTotalHours() - GetDays()*24);
172 }
173 
174 //////////////////////////////////////////////////////////////
175 // Function name : Time_Span::GetTotalMinutes
176 // Description :
177 //////////////////////////////////////////////////////////////
178 inline long Time_Span::GetTotalMinutes() const
179 {
180  return _my_time.tv_sec / 60;
181 }
182 
183 //////////////////////////////////////////////////////////////
184 // Function name : Time_Span::GetMinutes
185 // Description :
186 //////////////////////////////////////////////////////////////
187 inline int Time_Span::GetMinutes() const
188 {
189  return (int)(GetTotalMinutes() - GetTotalHours()*60);
190 }
191 
192 //////////////////////////////////////////////////////////////
193 // Function name : Time_Span::GetTotalSeconds
194 // Description :
195 //////////////////////////////////////////////////////////////
196 inline long Time_Span::GetTotalSeconds() const
197 {
198  return _my_time.tv_sec;
199 }
200 
201 //////////////////////////////////////////////////////////////
202 // Function name : Time_Span::GetTotalMSeconds
203 // Description :
204 //////////////////////////////////////////////////////////////
205 inline long Time_Span::GetTotalMSeconds() const
206 {
207  return (_my_time.tv_sec * 1000) + (_my_time.tv_usec / 1000);
208 }
209 
210 
211 inline long Time_Span::GetTotal100Seconds() const
212 {
213  return (_my_time.tv_sec * 100) + (_my_time.tv_usec / 10000);
214 }
215 
216 
217 
218 //////////////////////////////////////////////////////////////
219 // Function name : Time_Span::GetTotalMSeconds
220 // Description :
221 //////////////////////////////////////////////////////////////
222 inline long Time_Span::GetMSeconds() const
223 {
224  return (_my_time.tv_usec / 1000);
225 }
226 
227 
228 //////////////////////////////////////////////////////////////
229 // Function name : Time_Span::GetSeconds
230 // Description :
231 //////////////////////////////////////////////////////////////
232 inline int Time_Span::GetSeconds() const
233 {
234  return (int)(GetTotalSeconds() - GetTotalMinutes()*60);
235 }
236 
237 //////////////////////////////////////////////////////////////
238 // Function name : TimeDifference
239 // Description :
240 //////////////////////////////////////////////////////////////
241 inline Time_Span TimeDifference(const Time_Span &Time_Span1, const Time_Span &Time_Span2)
242 {
243  timeval ans;
244  TimeDif(Time_Span2.GetTval(), Time_Span1.GetTval(), ans);
245  return Time_Span(ans);
246 }
247 
248 //////////////////////////////////////////////////////////////
249 // Function name : TimeAddition
250 // Description :
251 //////////////////////////////////////////////////////////////
252 inline Time_Span TimeAddition(const Time_Span &Time_Span1, const Time_Span &Time_Span2)
253 {
254  timeval ans;
255  TimeAdd(Time_Span2.GetTval(), Time_Span1.GetTval(), ans);
256  return Time_Span(ans);
257 }
258 
259 //////////////////////////////////////////////////////////////
260 // Function name : Time_Span& Time_Span::operator+=
261 // Description :
262 //////////////////////////////////////////////////////////////
263 inline const Time_Span& Time_Span::operator+=(Time_Span &Time_Span)
264 {
265  _my_time.tv_usec += Time_Span._my_time.tv_usec;
266  _my_time.tv_sec += Time_Span._my_time.tv_sec;
267  NormalizeTime(_my_time);
268  return *this;
269 }
270 
271 //////////////////////////////////////////////////////////////
272 // Function name : Time_Span& Time_Span::operator-=
273 // Description :
274 //////////////////////////////////////////////////////////////
275 inline const Time_Span& Time_Span::operator-=(Time_Span &Time_Span)
276 {
277  _my_time.tv_usec -= Time_Span._my_time.tv_usec;
278  _my_time.tv_sec -= Time_Span._my_time.tv_sec;
279  NormalizeTime(_my_time);
280  return *this;
281 }
282 
283 //////////////////////////////////////////////////////////////
284 // Function name : Time_Span::operator==
285 // Description :
286 //////////////////////////////////////////////////////////////
287 inline bool Time_Span::operator==(Time_Span &Time_Span) const
288 {
289  return ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec == Time_Span._my_time.tv_usec));
290 }
291 
292 //////////////////////////////////////////////////////////////
293 // Function name : Time_Span::operator!=
294 // Description :
295 //////////////////////////////////////////////////////////////
296 inline bool Time_Span::operator!=(Time_Span &Time_Span) const
297 {
298  return ((_my_time.tv_sec != Time_Span._my_time.tv_sec) || (_my_time.tv_usec != Time_Span._my_time.tv_usec));
299 }
300 
301 //////////////////////////////////////////////////////////////
302 // Function name : Time_Span::operator<
303 // Description :
304 //////////////////////////////////////////////////////////////
305 inline bool Time_Span::operator<(Time_Span &Time_Span) const
306 {
307  return ((_my_time.tv_sec < Time_Span._my_time.tv_sec) ||
308  ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec < Time_Span._my_time.tv_usec)));
309 }
310 
311 //////////////////////////////////////////////////////////////
312 // Function name : Time_Span::operator>
313 // Description :
314 //////////////////////////////////////////////////////////////
315 inline bool Time_Span::operator>(Time_Span &Time_Span) const
316 {
317  return ((_my_time.tv_sec > Time_Span._my_time.tv_sec) ||
318  ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec > Time_Span._my_time.tv_usec)));
319 }
320 
321 //////////////////////////////////////////////////////////////
322 // Function name : Time_Span::operator<=
323 // Description :
324 //////////////////////////////////////////////////////////////
325 inline bool Time_Span::operator<=(Time_Span &Time_Span) const
326 {
327  return ((_my_time.tv_sec < Time_Span._my_time.tv_sec) ||
328  ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec <= Time_Span._my_time.tv_usec)));
329 }
330 
331 //////////////////////////////////////////////////////////////
332 // Function name : Time_Span::operator>=
333 // Description :
334 //////////////////////////////////////////////////////////////
335 inline bool Time_Span::operator>=(Time_Span &Time_Span) const
336 {
337  return ((_my_time.tv_sec > Time_Span._my_time.tv_sec) ||
338  ((_my_time.tv_sec == Time_Span._my_time.tv_sec) && (_my_time.tv_usec >= Time_Span._my_time.tv_usec)));
339 }
340 
341 //////////////////////////////////////////////////////////////
342 // Function name : Time_Span::Format
343 // Description :
344 //////////////////////////////////////////////////////////////
345 inline std::string Time_Span::Format(char * pFormat) const
346 // formatting Time_Spans is a little trickier than formatting
347 // * we are only interested in relative time formats, ie. it is illegal
348 // to format anything dealing with absolute time (i.e. years, months,
349 // day of week, day of year, timezones, ...)
350 // * the only valid formats:
351 // %D - # of days -- NEW !!!
352 // %H - hour in 24 hour format
353 // %M - minute (0-59)
354 // %S - seconds (0-59)
355 // %% - percent sign
356 // %N - nanosecs
357 {
358  char szBuffer[maxTimeBufferSize];
359  char ch;
360  char * pch = szBuffer;
361 
362  while ((ch = *pFormat++) != '\0') {
363  assert(pch < &szBuffer[maxTimeBufferSize]);
364  if (ch == '%') {
365  switch (ch = *pFormat++) {
366  default:
367  assert(false); // probably a bad format character
368  case '%':
369  *pch++ = ch;
370  break;
371  case 'D':
372  pch += sprintf(pch, "%ld", GetDays());
373  break;
374  case 'H':
375  pch += sprintf(pch, "%02d", GetHours());
376  break;
377  case 'M':
378  pch += sprintf(pch, "%02d", GetMinutes());
379  break;
380  case 'S':
381  pch += sprintf(pch, "%02d", GetSeconds());
382  break;
383  case 'N':
384  pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
385  break;
386  }
387  } else {
388  *pch++ = ch;
389  }
390  }
391 
392  *pch = '\0';
393  return std::string(szBuffer);
394 }
395 
396 #endif //__TIME_SPAN_H__