1 #ifndef __TIME_SPAN_H__ 2 #define __TIME_SPAN_H__ 14 NormalizeTime(_my_time);
18 Time_Span(
long lDays,
int nHours,
int nMins,
int nSecs,
int usecs);
28 long GetTotalHours()
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;
47 const timeval &GetTval()
const {
51 void Set(
long lDays,
int nHours,
int nMins,
int nSecs,
int usecs);
53 std::string Format(
char *pFormat)
const;
56 struct timeval _my_time;
64 Time_Span(
long seconds,
int usecs) {
65 _my_time.tv_sec = seconds;
66 _my_time.tv_usec = usecs;
67 NormalizeTime(_my_time);
74 Time_Span(time_t time) {
76 _my_time.tv_sec = (long)time;
83 Time_Span(PN_stdfloat Seconds) {
84 _my_time.tv_sec = (long)Seconds;
85 _my_time.tv_usec = (long)((Seconds - (
double)_my_time.tv_sec) * (
double)USEC);
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;
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;
112 Time_Span(
const Time_Span &Time_SpanSrc) {
113 _my_time = Time_SpanSrc._my_time;
121 _my_time = Time_SpanSrc._my_time;
128 operator=(
const Time_Span &Time_SpanSrc) {
129 if (&Time_SpanSrc ==
this) {
132 _my_time = Time_SpanSrc._my_time;
139 inline long Time_Span::
141 return _my_time.tv_sec / (24*3600L);
147 inline long Time_Span::
148 GetTotalHours()
const {
149 return _my_time.tv_sec / 3600;
155 inline int Time_Span::
157 return (
int)(GetTotalHours() - GetDays()*24);
163 inline long Time_Span::
164 GetTotalMinutes()
const {
165 return _my_time.tv_sec / 60;
171 inline int Time_Span::
173 return (
int)(GetTotalMinutes() - GetTotalHours()*60);
179 inline long Time_Span::
180 GetTotalSeconds()
const {
181 return _my_time.tv_sec;
187 inline long Time_Span::
188 GetTotalMSeconds()
const {
189 return (_my_time.tv_sec * 1000) + (_my_time.tv_usec / 1000);
192 inline long Time_Span::
193 GetTotal100Seconds()
const {
194 return (_my_time.tv_sec * 100) + (_my_time.tv_usec / 10000);
200 inline long Time_Span::
201 GetMSeconds()
const {
202 return (_my_time.tv_usec / 1000);
208 inline int Time_Span::
210 return (
int)(GetTotalSeconds() - GetTotalMinutes()*60);
218 TimeDif(Time_Span2.GetTval(), Time_Span1.GetTval(), ans);
227 TimeAdd(Time_Span2.GetTval(), Time_Span1.GetTval(), ans);
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);
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);
256 inline bool Time_Span::
258 return ((_my_time.tv_sec ==
Time_Span._my_time.tv_sec) && (_my_time.tv_usec ==
Time_Span._my_time.tv_usec));
264 inline bool Time_Span::
266 return ((_my_time.tv_sec !=
Time_Span._my_time.tv_sec) || (_my_time.tv_usec !=
Time_Span._my_time.tv_usec));
272 inline bool Time_Span::
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)));
281 inline bool Time_Span::
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)));
290 inline bool Time_Span::
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)));
299 inline bool Time_Span::
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)));
308 inline std::string Time_Span::
309 Format(
char *pFormat)
const {
318 char szBuffer[maxTimeBufferSize];
320 char *pch = szBuffer;
322 while ((ch = *pFormat++) !=
'\0') {
323 assert(pch < &szBuffer[maxTimeBufferSize]);
325 switch (ch = *pFormat++) {
333 pch += sprintf_s(pch, maxTimeBufferSize,
"%ld", GetDays());
335 pch += snprintf(pch, maxTimeBufferSize,
"%ld", GetDays());
340 pch += sprintf_s(pch, maxTimeBufferSize,
"%02d", GetHours());
342 pch += snprintf(pch, maxTimeBufferSize,
"%02d", GetHours());
347 pch += sprintf_s(pch, maxTimeBufferSize,
"%02d", GetMinutes());
349 pch += snprintf(pch, maxTimeBufferSize,
"%02d", GetMinutes());
354 pch += sprintf_s(pch, maxTimeBufferSize,
"%02d", GetSeconds());
356 pch += snprintf(pch, maxTimeBufferSize,
"%02d", GetSeconds());
361 pch += sprintf_s(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
363 pch += snprintf(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
373 return std::string(szBuffer);
376 #endif //__TIME_SPAN_H__ This class is to provide a consistant interface and storage to clock time