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