00001 #ifndef __Time_H__
00002 #define __Time_H__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <stdio.h>
00017
00018 class Time_Span;
00019
00020 class Time_Clock
00021 {
00022 friend class Time_Span;
00023
00024 public:
00025
00026 static Time_Clock GetCurrentTime();
00027 void ToCurrentTime();
00028 Time_Clock( timeval &in_mytime)
00029 {
00030 _my_time = in_mytime;
00031 };
00032 Time_Clock();
00033 Time_Clock(time_t time);
00034 Time_Clock(long secs, long usecs);
00035 Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
00036 Time_Clock(const Time_Clock& timeSrc);
00037
00038 inline const Time_Clock& operator=(const Time_Clock& timeSrc);
00039 inline const Time_Clock& operator=(time_t t);
00040
00041
00042 struct tm* GetGmtTm(struct tm* ptm = NULL) const;
00043 struct tm* GetLocalTm(struct tm* ptm = NULL) const;
00044
00045 time_t GetTime() const;
00046 int GetYear() const;
00047 int GetMonth() const;
00048 int GetDay() const;
00049 int GetHour() const;
00050 int GetMinute() const;
00051 int GetSecond() const;
00052 int GetDayOfWeek() const;
00053
00054 void Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds = 0, int nDST = -1);
00055
00056
00057
00058
00059 const Time_Clock& operator+=(const Time_Span &Time_Span);
00060 const Time_Clock& operator-=(const Time_Span &Time_Span);
00061 bool operator==(const Time_Clock &time) const;
00062 bool operator!=(const Time_Clock &time) const;
00063 bool operator<(const Time_Clock &time) const;
00064 bool operator>(const Time_Clock &time) const;
00065 bool operator<=(const Time_Clock &time) const;
00066 bool operator>=(const Time_Clock &time) const;
00067
00068
00069 time_t GetTime_t()
00070 {
00071 return _my_time.tv_sec;
00072 };
00073 unsigned long GetUsecPart()
00074 {
00075 return _my_time.tv_usec;
00076 };
00077
00078
00079 std::string Format(const char * pFormat) const;
00080 std::string FormatGmt(const char * pFormat) const;
00081 const timeval & GetTval()
00082 {
00083 return _my_time;
00084 };
00085 const timeval & GetTval() const
00086 {
00087 return _my_time;
00088 } ;
00089 private:
00090 struct timeval _my_time;
00091 };
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 inline Time_Clock::Time_Clock(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
00107 {
00108 struct tm atm;
00109 atm.tm_sec = nSec;
00110 atm.tm_min = nMin;
00111 atm.tm_hour = nHour;
00112 assert(nDay >= 1 && nDay <= 31);
00113 atm.tm_mday = nDay;
00114 assert(nMonth >= 1 && nMonth <= 12);
00115 atm.tm_mon = nMonth - 1;
00116 assert(nYear >= 1900);
00117 atm.tm_year = nYear - 1900;
00118 atm.tm_isdst = nDST;
00119 _my_time.tv_sec = (long)mktime(&atm);
00120 assert(_my_time.tv_sec != -1);
00121 _my_time.tv_usec = microseconds;
00122 assert(_my_time.tv_usec < 1000000);
00123 }
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137 inline void Time_Clock::Set(int nYear, int nMonth, int nDay, int nHour, int nMin, int nSec, unsigned long microseconds , int nDST)
00138 {
00139 struct tm atm;
00140 atm.tm_sec = nSec;
00141 atm.tm_min = nMin;
00142 atm.tm_hour = nHour;
00143 assert(nDay >= 1 && nDay <= 31);
00144 atm.tm_mday = nDay;
00145 assert(nMonth >= 1 && nMonth <= 12);
00146 atm.tm_mon = nMonth - 1;
00147 assert(nYear >= 1900);
00148 atm.tm_year = nYear - 1900;
00149 atm.tm_isdst = nDST;
00150 _my_time.tv_sec = (long)mktime(&atm);
00151 assert(_my_time.tv_sec != -1);
00152 _my_time.tv_usec = microseconds;
00153 assert(_my_time.tv_usec < 1000000);
00154 }
00155
00156
00157
00158
00159
00160 inline Time_Clock Time_Clock::GetCurrentTime()
00161 {
00162 return Time_Clock();
00163 }
00164
00165
00166
00167
00168
00169 inline Time_Clock::Time_Clock()
00170 {
00171 gettimeofday(&_my_time, NULL);
00172 }
00173
00174
00175
00176
00177
00178
00179 inline void Time_Clock::ToCurrentTime()
00180 {
00181 gettimeofday(&_my_time, NULL);
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191 inline struct tm* Time_Clock::GetGmtTm(struct tm* ptm) const
00192 {
00193 if (ptm != NULL)
00194 {
00195 *ptm = *gmtime((const time_t *)&_my_time.tv_sec);
00196 return ptm;
00197 } else
00198 return gmtime((const time_t *)&_my_time.tv_sec);
00199 }
00200
00201
00202
00203
00204
00205
00206
00207
00208 inline struct tm* Time_Clock::GetLocalTm(struct tm* ptm) const
00209 {
00210 if (ptm != NULL)
00211 {
00212 struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
00213 if (ptmTemp == NULL)
00214 return NULL;
00215 *ptm = *ptmTemp;
00216 return ptm;
00217 } else
00218 return localtime((const time_t *)&_my_time.tv_sec);
00219 }
00220
00221
00222 #define maxTimeBufferSize 4096
00223
00224
00225
00226
00227
00228
00229
00230
00231 inline std::string Time_Clock::Format(const char * pFormat) const
00232 {
00233
00234 char szBuffer[maxTimeBufferSize];
00235 char ch, ch1;
00236 char * pch = szBuffer;
00237
00238 while ((ch = *pFormat++) != '\0')
00239 {
00240 assert(pch < &szBuffer[maxTimeBufferSize]);
00241 if (ch == '%')
00242 {
00243 switch (ch1 = *pFormat++)
00244 {
00245 default:
00246 *pch++ = ch;
00247 *pch++ = ch1;
00248 break;
00249 case 'N':
00250 pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
00251 break;
00252 }
00253 }
00254 else
00255 {
00256 *pch++ = ch;
00257 }
00258 }
00259
00260 *pch = '\0';
00261
00262 char szBuffer1[maxTimeBufferSize];
00263
00264 struct tm* ptmTemp = localtime((const time_t *)&_my_time.tv_sec);
00265 if (ptmTemp == NULL ||
00266 !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
00267 szBuffer1[0] = '\0';
00268 return std::string(szBuffer1);
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 inline std::string Time_Clock::FormatGmt(const char * pFormat) const
00280 {
00281
00282 char szBuffer[maxTimeBufferSize];
00283 char ch, ch1;
00284 char * pch = szBuffer;
00285
00286 while ((ch = *pFormat++) != '\0')
00287 {
00288 assert(pch < &szBuffer[maxTimeBufferSize]);
00289 if (ch == '%')
00290 {
00291 switch (ch1 = *pFormat++)
00292 {
00293 default:
00294 *pch++ = ch;
00295 *pch++ = ch1;
00296 break;
00297 case 'N':
00298 pch += sprintf(pch, "%03ld", (long)(_my_time.tv_usec / 1000));
00299 break;
00300 }
00301 }
00302 else
00303 {
00304 *pch++ = ch;
00305 }
00306 }
00307 *pch = '\0';
00308
00309 char szBuffer1[maxTimeBufferSize];
00310
00311 struct tm* ptmTemp = gmtime((const time_t *)&_my_time.tv_sec);
00312 if (ptmTemp == NULL ||
00313 !strftime(szBuffer1, sizeof(szBuffer1), szBuffer, ptmTemp))
00314 szBuffer1[0] = '\0';
00315 return std::string(szBuffer1);
00316 }
00317
00318
00319
00320
00321
00322
00323 inline Time_Clock::Time_Clock(time_t time)
00324 {
00325 _my_time.tv_sec = (long)time;
00326 _my_time.tv_usec = 0;
00327 };
00328
00329
00330
00331
00332
00333
00334
00335 inline Time_Clock::Time_Clock(long secs, long usecs)
00336 {
00337 _my_time.tv_sec = secs;
00338 _my_time.tv_usec = usecs;
00339 NormalizeTime(_my_time);
00340 };
00341
00342
00343
00344
00345
00346
00347 inline Time_Clock::Time_Clock(const Time_Clock& timeSrc)
00348 {
00349 _my_time.tv_sec = timeSrc._my_time.tv_sec;
00350 _my_time.tv_usec = timeSrc._my_time.tv_usec;
00351 }
00352
00353
00354
00355
00356
00357
00358 inline bool Time_Clock::operator==(const Time_Clock &time) const
00359 {
00360 return ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec == time._my_time.tv_usec));
00361 }
00362
00363
00364
00365
00366
00367
00368 inline bool Time_Clock::operator!=(const Time_Clock &time) const
00369 {
00370 return ((_my_time.tv_sec != time._my_time.tv_sec) || (_my_time.tv_usec != time._my_time.tv_usec));
00371 }
00372
00373
00374
00375
00376
00377
00378
00379 inline bool Time_Clock::operator<(const Time_Clock &time) const
00380 {
00381 return ((_my_time.tv_sec < time._my_time.tv_sec) ||
00382 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec < time._my_time.tv_usec)));
00383 }
00384
00385
00386
00387
00388
00389
00390 inline bool Time_Clock::operator>(const Time_Clock &time) const
00391 {
00392 return ((_my_time.tv_sec > time._my_time.tv_sec) ||
00393 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec > time._my_time.tv_usec)));
00394 }
00395
00396
00397
00398
00399
00400
00401 inline bool Time_Clock::operator<=(const Time_Clock &time) const
00402 {
00403 return ((_my_time.tv_sec < time._my_time.tv_sec) ||
00404 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec <= time._my_time.tv_usec)));
00405 }
00406
00407
00408
00409
00410
00411
00412 inline bool Time_Clock::operator>=(const Time_Clock &time) const
00413 {
00414 return ((_my_time.tv_sec > time._my_time.tv_sec) ||
00415 ((_my_time.tv_sec == time._my_time.tv_sec) && (_my_time.tv_usec >= time._my_time.tv_usec)));
00416 }
00417
00418
00419
00420
00421
00422
00423 inline const Time_Clock& Time_Clock::operator=(const Time_Clock& timeSrc)
00424 {
00425 if (&timeSrc == this)
00426 return * this;
00427
00428 _my_time = timeSrc._my_time;
00429 return *this;
00430 }
00431
00432
00433
00434
00435
00436
00437 inline const Time_Clock& Time_Clock::operator=(time_t t)
00438 {
00439 _my_time.tv_sec = (long)t;
00440 _my_time.tv_usec = 0;
00441 return *this;
00442 }
00443
00444
00445
00446
00447
00448 inline time_t Time_Clock::GetTime() const
00449 {
00450 return _my_time.tv_sec;
00451 }
00452
00453
00454
00455
00456
00457 inline int Time_Clock::GetYear() const
00458 {
00459 return (GetLocalTm(NULL)->tm_year) + 1900;
00460 }
00461
00462
00463
00464
00465
00466 inline int Time_Clock::GetMonth() const
00467 {
00468 return GetLocalTm(NULL)->tm_mon + 1;
00469 }
00470
00471
00472
00473
00474
00475 inline int Time_Clock::GetDay() const
00476 {
00477 return GetLocalTm(NULL)->tm_mday;
00478 }
00479
00480
00481
00482
00483
00484 inline int Time_Clock::GetHour() const
00485 {
00486 return GetLocalTm(NULL)->tm_hour;
00487 }
00488
00489
00490
00491
00492
00493 inline int Time_Clock::GetMinute() const
00494 {
00495 return GetLocalTm(NULL)->tm_min;
00496 }
00497
00498
00499
00500
00501
00502 inline int Time_Clock::GetSecond() const
00503 {
00504 return GetLocalTm(NULL)->tm_sec;
00505 }
00506
00507
00508
00509
00510
00511 inline int Time_Clock::GetDayOfWeek() const
00512 {
00513 return GetLocalTm(NULL)->tm_wday + 1;
00514 }
00515
00516 #endif //__Time_H__