27 Time_Clock(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
long microseconds = 0,
int nDST = -1);
34 struct tm *
GetGmtTm(
struct tm *ptm)
const;
37 time_t GetTime()
const;
42 int GetMinute()
const;
43 int GetSecond()
const;
44 int GetDayOfWeek()
const;
46 void Set(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
long microseconds = 0,
int nDST = -1);
58 inline time_t GetTime_t() {
59 return _my_time.tv_sec;
61 inline long GetUsecPart() {
62 return _my_time.tv_usec;
66 std::string
Format(
const char *pFormat)
const;
67 std::string
FormatGmt(
const char *pFormat)
const;
69 const timeval &GetTval() {
72 const timeval &GetTval()
const {
77 struct timeval _my_time;
84 Time_Clock(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
long microseconds,
int nDST) {
89 assert(nDay >= 1 && nDay <= 31);
91 assert(nMonth >= 1 && nMonth <= 12);
92 atm.tm_mon = nMonth - 1;
93 assert(nYear >= 1900);
94 atm.tm_year = nYear - 1900;
96 _my_time.tv_sec = (long)mktime(&atm);
97 assert(_my_time.tv_sec != -1);
98 _my_time.tv_usec = microseconds;
99 assert(_my_time.tv_usec < 1000000);
105 inline void Time_Clock::
106 Set(
int nYear,
int nMonth,
int nDay,
int nHour,
int nMin,
int nSec,
long microseconds ,
int nDST) {
111 assert(nDay >= 1 && nDay <= 31);
113 assert(nMonth >= 1 && nMonth <= 12);
114 atm.tm_mon = nMonth - 1;
115 assert(nYear >= 1900);
116 atm.tm_year = nYear - 1900;
118 _my_time.tv_sec = (long)mktime(&atm);
119 assert(_my_time.tv_sec != -1);
120 _my_time.tv_usec = microseconds;
121 assert(_my_time.tv_usec < 1000000);
137 gettimeofday(&_my_time,
nullptr);
145 gettimeofday(&_my_time,
nullptr);
155 nassertr(ptm !=
nullptr,
nullptr);
157 return (gmtime_s(ptm, (
const time_t *)&_my_time.tv_sec) == 0) ? ptm :
nullptr;
159 return gmtime_r((
const time_t *)&_my_time.tv_sec, ptm);
168 nassertr(ptm !=
nullptr,
nullptr);
170 return (localtime_s(ptm, (
const time_t *)&_my_time.tv_sec) == 0) ? ptm :
nullptr;
172 return localtime_r((
const time_t *)&_my_time.tv_sec, ptm);
176 #define maxTimeBufferSize 4096 183 char szBuffer[maxTimeBufferSize];
185 char *pch = szBuffer;
187 while ((ch = *pFormat++) !=
'\0') {
188 assert(pch < &szBuffer[maxTimeBufferSize]);
190 switch (ch1 = *pFormat++) {
197 pch += sprintf_s(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
199 pch += snprintf(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
210 char szBuffer1[maxTimeBufferSize];
214 !strftime(szBuffer1,
sizeof(szBuffer1), szBuffer, &tmTemp)) {
217 return std::string(szBuffer1);
227 char szBuffer[maxTimeBufferSize];
229 char *pch = szBuffer;
231 while ((ch = *pFormat++) !=
'\0') {
232 assert(pch < &szBuffer[maxTimeBufferSize]);
234 switch (ch1 = *pFormat++) {
241 pch += sprintf_s(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
243 pch += snprintf(pch, maxTimeBufferSize,
"%03ld", (
long)(_my_time.tv_usec / 1000));
253 char szBuffer1[maxTimeBufferSize];
257 !strftime(szBuffer1,
sizeof(szBuffer1), szBuffer, &tmTemp)) {
260 return std::string(szBuffer1);
267 Time_Clock(time_t time) {
268 _my_time.tv_sec = (long)time;
269 _my_time.tv_usec = 0;
276 Time_Clock(
long secs,
long usecs) {
277 _my_time.tv_sec = secs;
278 _my_time.tv_usec = usecs;
279 NormalizeTime(_my_time);
287 _my_time.tv_sec = timeSrc._my_time.tv_sec;
288 _my_time.tv_usec = timeSrc._my_time.tv_usec;
296 return ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec == time._my_time.tv_usec));
304 return ((_my_time.tv_sec != time._my_time.tv_sec) || (_my_time.tv_usec != time._my_time.tv_usec));
310 inline bool Time_Clock::
312 return ((_my_time.tv_sec < time._my_time.tv_sec) ||
313 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec < time._my_time.tv_usec)));
319 inline bool Time_Clock::
321 return ((_my_time.tv_sec > time._my_time.tv_sec) ||
322 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec > time._my_time.tv_usec)));
328 inline bool Time_Clock::
330 return ((_my_time.tv_sec < time._my_time.tv_sec) ||
331 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec <= time._my_time.tv_usec)));
337 inline bool Time_Clock::
339 return ((_my_time.tv_sec > time._my_time.tv_sec) ||
340 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec >= time._my_time.tv_usec)));
348 if (&timeSrc ==
this) {
352 _my_time = timeSrc._my_time;
360 operator=(time_t t) {
361 _my_time.tv_sec = (long)t;
362 _my_time.tv_usec = 0;
369 inline time_t Time_Clock::
371 return _my_time.tv_sec;
377 inline int Time_Clock::
386 inline int Time_Clock::
395 inline int Time_Clock::
404 inline int Time_Clock::
413 inline int Time_Clock::
422 inline int Time_Clock::
431 inline int Time_Clock::
432 GetDayOfWeek()
const {
std::string Format(const char *pFormat) const
Used to allow access to the "C" library strftime functions.
This class is to provide a consistant interface and storage to clock time
struct tm * GetGmtTm(struct tm *ptm) const
Access the stored time and converts to a struct tm format If storage location is specified then it wi...
bool operator==(const Time_Clock &time) const
bool operator!=(const Time_Clock &time) const
.is time !=
std::string FormatGmt(const char *pFormat) const
A Wraper to size_t strftime( char *strDest, size_t maxsize, const char *format, const struct tm *time...
void ToCurrentTime()
Load this object with the current OS time.
struct tm * GetLocalTm(struct tm *ptm) const
Gets The local time in a tm structre from the internal time value.
static Time_Clock GetCurrentTime()
The Default no param constructor.