00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "TVldTimeStamp.hxx"
00031 #include <TSK_DBI_Log.hxx>
00032 #include <MsgFormat.h>
00033 using std::endl;
00034 #include <climits>
00035 #include <math.h>
00036
00037
00038 #ifdef R__WIN32
00039 #include "Windows4Root.h"
00040 #else
00041 #include <unistd.h>
00042
00043 #include <sys/time.h>
00044 #endif
00045
00046 #include "TString.h"
00047
00048 ClassImp(TVldTimeStamp)
00049
00050 const Int_t kNsPerSec = 1000000000;
00051
00052
00053 std::ostream& operator<<(std::ostream& os, const TVldTimeStamp& ts)
00054 {
00055 if (os.good()) {
00056 if (os.tie()) os.tie()->flush();
00057 os << ts.AsString("c");
00058 }
00059
00060 if (os.flags() & std::ios::unitbuf) os.flush();
00061 return os;
00062 }
00063
00064 TVldTimeStamp TVldTimeStamp::GetBOT()
00065 {
00066 return TVldTimeStamp((time_t)0,0);
00067 }
00068
00069 TVldTimeStamp TVldTimeStamp::GetEOT()
00070 {
00071 return TVldTimeStamp((time_t)INT_MAX,0);
00072 }
00073
00074 TVldTimeStamp TVldTimeStamp::GetNBOT()
00075 {
00076 return TVldTimeStamp((time_t)INT_MIN,0);
00077 }
00078
00079
00080
00081
00082
00083
00084
00085 TVldTimeStamp::TVldTimeStamp() : fSec(0), fNanoSec(0) { Set(); }
00086 TVldTimeStamp::~TVldTimeStamp() { ; }
00087
00088
00089 TVldTimeStamp::TVldTimeStamp(UInt_t year, UInt_t month,
00090 UInt_t day, UInt_t hour,
00091 UInt_t min, UInt_t sec,
00092 UInt_t nsec,
00093 Bool_t isUTC, Int_t secOffset)
00094 : fSec(0), fNanoSec(0)
00095 {
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
00107 }
00108
00109
00110 TVldTimeStamp::TVldTimeStamp(UInt_t date, UInt_t time, UInt_t nsec,
00111 Bool_t isUTC, Int_t secOffset)
00112 : fSec(0), fNanoSec(0)
00113 {
00114
00115
00116
00117 Set(date, time, nsec, isUTC, secOffset);
00118 }
00119
00120
00121 const char *TVldTimeStamp::AsString(Option_t *option) const
00122 {
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155 const int nbuffers = 8;
00156
00157 static char formatted[nbuffers][64];
00158 static char formatted2[nbuffers][64];
00159 static int ibuffer = nbuffers;
00160 ibuffer = (ibuffer+1)%nbuffers;
00161
00162 TString opt = option;
00163 opt.ToLower();
00164
00165 if (opt.Contains("2")) {
00166
00167 sprintf(formatted[ibuffer], "{%d,%d}", fSec, fNanoSec);
00168 return formatted[ibuffer];
00169 }
00170
00171 #ifdef linux
00172
00173 const char *RFC822 = "%a, %d %b %Y %H:%M:%S %z (%Z) +#9ld nsec";
00174 const char *ISO8601 = "%Y-%m-%d %H:%M:%S.#9.9ld%z";
00175 const char *ISO8601Z = "%Y-%m-%d %H:%M:%S.#9.9ldZ";
00176 #else
00177
00178 const char *RFC822 = "%a, %d %b %Y %H:%M:%S %Z +#9ld nsec";
00179 const char *ISO8601 = "%Y-%m-%d %H:%M:%S.#9.9ld%Z";
00180 const char *ISO8601Z = "%Y-%m-%d %H:%M:%S.#9.9ldZ";
00181 #endif
00182 const char *SQL = "%Y-%m-%d %H:%M:%S";
00183
00184 Bool_t asLocal = opt.Contains("l");
00185 Bool_t asSQL = opt.Contains("s");
00186 if (asSQL) asLocal = kFALSE;
00187
00188 const char *format = RFC822;
00189 if (opt.Contains("c")) {
00190 format = ISO8601;
00191 if (!asLocal) format = ISO8601Z;
00192 }
00193 if (asSQL) format = SQL;
00194
00195 struct tm *ptm;
00196 time_t seconds = (time_t) fSec;
00197
00198
00199
00200
00201 ptm = (asLocal) ? localtime(&seconds) : gmtime(&seconds);
00202
00203
00204
00205 strftime(formatted[ibuffer], sizeof(formatted[ibuffer]), format, ptm);
00206
00207 if (asSQL) return formatted[ibuffer];
00208
00209
00210 char *ptr = strrchr(formatted[ibuffer], '#');
00211 if (ptr) *ptr = '%';
00212 sprintf(formatted2[ibuffer], formatted[ibuffer], fNanoSec);
00213
00214 return formatted2[ibuffer];
00215 }
00216
00217
00218 void TVldTimeStamp::Copy(TVldTimeStamp &ts) const
00219 {
00220
00221
00222 ts.fSec = fSec;
00223 ts.fNanoSec = fNanoSec;
00224
00225 }
00226
00227
00228 Int_t TVldTimeStamp::GetDate(Bool_t inUTC, Int_t secOffset,
00229 UInt_t* year, UInt_t* month, UInt_t* day) const
00230 {
00231
00232
00233
00234 time_t atime = fSec + secOffset;
00235 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
00236
00237 if (year) *year = ptm->tm_year + 1900;
00238 if (month) *month = ptm->tm_mon + 1;
00239 if (day) *day = ptm->tm_mday;
00240
00241 return (1900+ptm->tm_year)*10000 + (1+ptm->tm_mon)*100 + ptm->tm_mday;
00242
00243 }
00244
00245
00246 Int_t TVldTimeStamp::GetTime(Bool_t inUTC, Int_t secOffset,
00247 UInt_t* hour, UInt_t* min, UInt_t* sec) const
00248 {
00249
00250
00251
00252 time_t atime = fSec + secOffset;
00253 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
00254
00255 if (hour) *hour = ptm->tm_hour;
00256 if (min) *min = ptm->tm_min;
00257 if (sec) *sec = ptm->tm_sec;
00258
00259 return ptm->tm_hour*10000 + ptm->tm_min*100 + ptm->tm_sec;
00260
00261 }
00262
00263
00264 Int_t TVldTimeStamp::GetZoneOffset()
00265 {
00266
00267
00268
00269
00270 #ifndef R__WIN32
00271 tzset();
00272 #if !defined(R__MACOSX) && !defined(R__FBSD)
00273 return timezone;
00274 #else
00275 time_t *tp = 0;
00276 time(tp);
00277 return localtime(tp)->tm_gmtoff;
00278 #endif
00279 #else
00280 _tzset();
00281 return _timezone;
00282 #endif
00283 }
00284
00285
00286 void TVldTimeStamp::Add(const TVldTimeStamp &offset)
00287 {
00288
00289
00290 fSec += offset.fSec;
00291 fNanoSec += offset.fNanoSec;
00292 NormalizeNanoSec();
00293
00294 }
00295
00296 void TVldTimeStamp::Add(Double_t seconds)
00297 {
00298
00299
00300 fSec += (Int_t) seconds;
00301 fNanoSec += (Int_t) (fmod(seconds,1.0) * 1e9);
00302 NormalizeNanoSec();
00303 if(seconds > 1e6)
00304 SK_DBI_Warn( "VldTimeStamp moved by offset " << seconds <<" which is too large to maintain ns accuracy." << " ");
00305 }
00306
00307
00308 void TVldTimeStamp::Print(Option_t *option) const
00309 {
00310
00311
00312 printf("Date/Time = %s\n", AsString(option));
00313
00314 }
00315
00316
00317 void TVldTimeStamp::Set()
00318 {
00319
00320
00321
00322
00323
00324
00325 #ifdef R__WIN32
00326 ULARGE_INTEGER time;
00327 GetSystemTimeAsFileTime((FILETIME *)&time);
00328
00329
00330
00331 fNanoSec = Int_t((time.QuadPart * (unsigned __int64) 100) %
00332 (unsigned __int64) 1000000000);
00333 time.QuadPart -=
00334 (unsigned __int64) (1000*1000*10)
00335 * (unsigned __int64) (60 * 60 * 24)
00336 * (unsigned __int64) (134774);
00337
00338 fSec = Int_t(time.QuadPart/(unsigned __int64) (1000*1000*10));
00339 #else
00340
00341
00342 struct timeval now;
00343 if (!gettimeofday(&now,0)) {
00344 fSec = now.tv_sec;
00345 fNanoSec = now.tv_usec * 1000;
00346 }
00347 else {
00348 time_t nowtime;
00349 time(&nowtime);
00350 fSec = nowtime;
00351 fNanoSec = 0;
00352 }
00353 #endif
00354 static Int_t sec = 0, nsec = 0, fake_ns = 0;
00355
00356 if (fSec == sec && fNanoSec == nsec)
00357 fNanoSec += ++fake_ns;
00358 else {
00359 fake_ns = 0;
00360 sec = fSec;
00361 nsec = fNanoSec;
00362 }
00363
00364 }
00365
00366
00367 void TVldTimeStamp::Set(Int_t year, Int_t month, Int_t day,
00368 Int_t hour, Int_t min, Int_t sec,
00369 Int_t nsec, Bool_t isUTC, Int_t secOffset)
00370 {
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386 if (year <= 37) year += 2000;
00387 if (year >= 70 && year <= 137) year += 1900;
00388
00389 if (year >= 1900) year -= 1900;
00390
00391 struct tm tmstruct;
00392 tmstruct.tm_year = year;
00393 tmstruct.tm_mon = month-1;
00394 tmstruct.tm_mday = day;
00395 tmstruct.tm_hour = hour;
00396 tmstruct.tm_min = min;
00397 tmstruct.tm_sec = sec + secOffset;
00398 tmstruct.tm_isdst = -1;
00399
00400 const time_t bad_time_t = (time_t) -1;
00401
00402
00403
00404
00405 time_t utc_sec = (isUTC) ? MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
00406
00407
00408
00409 if (utc_sec == bad_time_t)
00410 SK_DBI_Info( "VldTimeStamp::Set mktime returned -1" << " ");
00411
00412 fSec = utc_sec;
00413 fNanoSec = nsec;
00414
00415 NormalizeNanoSec();
00416 }
00417
00418
00419 void TVldTimeStamp::Set(Int_t date, Int_t time, Int_t nsec,
00420 Bool_t isUTC, Int_t secOffset)
00421 {
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437 Int_t year = date/10000;
00438 Int_t month = (date-year*10000)/100;
00439 Int_t day = date%100;
00440
00441
00442 const Int_t oneday = 240000;
00443 while (time < 0) {
00444 time += oneday;
00445 day -= 1;
00446 }
00447 while (time > oneday) {
00448 time -= oneday;
00449 day += 1;
00450 }
00451 Int_t hour = time/10000;
00452 Int_t min = (time-hour*10000)/100;
00453 Int_t sec = time%100;
00454
00455 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
00456
00457 }
00458
00459
00460 void TVldTimeStamp::NormalizeNanoSec()
00461 {
00462
00463
00464
00465 while (fNanoSec < 0) {
00466 fNanoSec += kNsPerSec;
00467 fSec -= 1;
00468 }
00469
00470 while (fNanoSec >= kNsPerSec) {
00471 fNanoSec -= kNsPerSec;
00472 fSec += 1;
00473 }
00474 }
00475
00476 time_t TVldTimeStamp::MktimeFromUTC(tm_t *tmstruct)
00477 {
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489 const Int_t days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00490 const Int_t daysLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00491
00492 Int_t year = tmstruct->tm_year + 1900;
00493 Bool_t isleap = TVldTimeStamp::IsLeapYear(year);
00494
00495 const Int_t *daysInMonth = days;
00496 if (isleap) daysInMonth = daysLeap;
00497
00498
00499
00500 int &ref_tm_mon = tmstruct->tm_mon;
00501 int &ref_tm_mday = tmstruct->tm_mday;
00502
00503 tmstruct->tm_yday = 0;
00504 for (Int_t imonth = 0; imonth < ref_tm_mon; imonth++) {
00505 tmstruct->tm_yday += daysInMonth[imonth];
00506 }
00507 tmstruct->tm_yday += ref_tm_mday - 1;
00508
00509
00510 while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
00511 ref_tm_mday -= daysInMonth[ref_tm_mon];
00512 ref_tm_mon++;
00513 }
00514
00515
00516
00517
00518 tmstruct->tm_isdst = 0;
00519
00520
00521
00522
00523 Int_t utc_sec = tmstruct->tm_sec +
00524 tmstruct->tm_min*60 +
00525 tmstruct->tm_hour*3600 +
00526 tmstruct->tm_yday*86400 +
00527 (tmstruct->tm_year-70)*31536000 +
00528 ((tmstruct->tm_year-69)/4)*86400;
00529
00530 return utc_sec;
00531 }
00532
00533
00534 Bool_t TVldTimeStamp::IsLeapYear(Int_t year)
00535 {
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548 if (year%4 != 0) {
00549 return false;
00550 }
00551 else {
00552 if (year%400 == 0) {
00553 return true;
00554 }
00555 else {
00556 if (year%100 == 0) {
00557 return false;
00558 }
00559 else {
00560 return true;
00561 }
00562 }
00563 }
00564
00565 }
00566
00567
00568 void TVldTimeStamp::DumpTMStruct(const tm_t &tmstruct)
00569 {
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581 printf(" tm { year %4d, mon %2d, day %2d,\n",
00582 tmstruct.tm_year,
00583 tmstruct.tm_mon,
00584 tmstruct.tm_mday);
00585 printf(" hour %2d, min %2d, sec %2d,\n",
00586 tmstruct.tm_hour,
00587 tmstruct.tm_min,
00588 tmstruct.tm_sec);
00589 printf(" wday %2d, yday %3d, isdst %2d",
00590 tmstruct.tm_wday,
00591 tmstruct.tm_yday,
00592 tmstruct.tm_isdst);
00593 #ifdef linux
00594
00595
00596 printf(",\n tm_gmtoff %7ld, tm_zone \"%s\"",
00597 #ifdef __USE_BSD
00598 tmstruct.tm_gmtoff,tmstruct.tm_zone);
00599 #else
00600 tmstruct.__tm_gmtoff,tmstruct.__tm_zone);
00601 #endif
00602 #endif
00603 printf("}\n");
00604 }
00605
00606