12 const Int_t kNsPerSec = 1000000000;
18 if (os.tie()) { os.tie()->flush(); }
19 os << ts.AsString(
"c");
22 if (os.flags() & std::ios::unitbuf) { os.flush(); }
52 UInt_t day, UInt_t hour,
53 UInt_t
min, UInt_t sec,
55 Bool_t isUTC, Int_t secOffset)
56 : fSec(0), fNanoSec(0)
68 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
73 Bool_t isUTC, Int_t secOffset)
74 : fSec(0), fNanoSec(0)
79 Set(date, time, nsec, isUTC, secOffset);
117 const int nbuffers = 8;
119 static char formatted[nbuffers][64];
120 static char formatted2[nbuffers][64];
121 static int ibuffer = nbuffers;
122 ibuffer = (ibuffer+1)%nbuffers;
124 TString opt = option;
127 if (opt.Contains(
"2")) {
130 return formatted[ibuffer];
135 const char* RFC822 =
"%a, %d %b %Y %H:%M:%S %z (%Z) +#9ld nsec";
136 const char* ISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%z";
137 const char* ISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
140 const char* RFC822 =
"%a, %d %b %Y %H:%M:%S %Z +#9ld nsec";
141 const char* ISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%Z";
142 const char* ISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
144 const char* SQL =
"%Y-%m-%d %H:%M:%S";
146 Bool_t asLocal = opt.Contains(
"l");
147 Bool_t asSQL = opt.Contains(
"s");
148 if (asSQL) { asLocal = kFALSE; }
150 const char* format = RFC822;
151 if (opt.Contains(
"c")) {
153 if (!asLocal) { format = ISO8601Z; }
155 if (asSQL) { format = SQL; }
158 time_t seconds = (time_t)
fSec;
163 ptm = (asLocal) ? localtime(&seconds) : gmtime(&seconds);
167 strftime(formatted[ibuffer],
sizeof(formatted[ibuffer]), format, ptm);
169 if (asSQL) {
return formatted[ibuffer]; }
172 char* ptr = strrchr(formatted[ibuffer],
'#');
173 if (ptr) { *ptr =
'%'; }
174 sprintf(formatted2[ibuffer], formatted[ibuffer],
fNanoSec);
176 return formatted2[ibuffer];
191 UInt_t* year, UInt_t* month, UInt_t* day)
const
196 time_t atime =
fSec + secOffset;
197 struct tm* ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
199 if (year) { *year = ptm->tm_year + 1900; }
200 if (month) { *month = ptm->tm_mon + 1; }
201 if (day) { *day = ptm->tm_mday; }
203 return (1900+ptm->tm_year)*10000 + (1+ptm->tm_mon)*100 + ptm->tm_mday;
209 UInt_t* hour, UInt_t*
min, UInt_t* sec)
const
214 time_t atime =
fSec + secOffset;
215 struct tm* ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
217 if (hour) { *hour = ptm->tm_hour; }
218 if (min) { *min = ptm->tm_min; }
219 if (sec) { *sec = ptm->tm_sec; }
221 return ptm->tm_hour*10000 + ptm->tm_min*100 + ptm->tm_sec;
234 #if !defined(R__MACOSX) && !defined(R__FBSD)
239 return localtime(tp)->tm_gmtoff;
262 fSec += (Int_t) seconds;
263 fNanoSec += (Int_t) (fmod(seconds,1.0) * 1e9);
266 cout <<
"-I- ValTimeStamp::Add(): "
267 <<
" ValTimeStamp moved by offset "
268 << seconds <<
" which is too large to maintain ns accuracy."
292 GetSystemTimeAsFileTime((FILETIME*)&time);
296 fNanoSec = Int_t((time.QuadPart * (
unsigned __int64) 100) %
297 (
unsigned __int64) 1000000000);
299 (
unsigned __int64) (1000*1000*10)
300 * (
unsigned __int64) (60 * 60 * 24)
301 * (
unsigned __int64) (134774);
303 fSec = Int_t(time.QuadPart/(
unsigned __int64) (1000*1000*10));
308 if (!gettimeofday(&now,0)) {
318 static Int_t sec = 0, nsec = 0, fake_ns = 0;
332 Int_t hour, Int_t
min, Int_t sec,
333 Int_t nsec, Bool_t isUTC, Int_t secOffset)
350 if (year <= 37) { year += 2000; }
351 if (year >= 70 && year <= 137) { year += 1900; }
353 if (year >= 1900) { year -= 1900; }
356 tmstruct.tm_year = year;
357 tmstruct.tm_mon = month-1;
358 tmstruct.tm_mday = day;
359 tmstruct.tm_hour = hour;
360 tmstruct.tm_min =
min;
361 tmstruct.tm_sec = sec + secOffset;
362 tmstruct.tm_isdst = -1;
364 const time_t bad_time_t = (time_t) -1;
369 time_t utc_sec = (isUTC) ?
MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
373 if (utc_sec == bad_time_t)
376 cout <<
"-I- ValTimeStamp::Set mktime returned -1" << endl;
387 Bool_t isUTC, Int_t secOffset)
404 Int_t year = date/10000;
405 Int_t month = (date-year*10000)/100;
406 Int_t day = date%100;
409 const Int_t oneday = 240000;
414 while (time > oneday) {
418 Int_t hour = time/10000;
419 Int_t
min = (time-hour*10000)/100;
420 Int_t sec = time%100;
422 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
456 const Int_t days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
457 const Int_t daysLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
459 Int_t year = tmstruct->tm_year + 1900;
462 const Int_t* daysInMonth = days;
463 if (isleap) { daysInMonth = daysLeap; }
467 int& ref_tm_mon = tmstruct->tm_mon;
468 int& ref_tm_mday = tmstruct->tm_mday;
470 tmstruct->tm_yday = 0;
471 for (Int_t imonth = 0; imonth < ref_tm_mon; imonth++) {
472 tmstruct->tm_yday += daysInMonth[imonth];
474 tmstruct->tm_yday += ref_tm_mday - 1;
477 while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
478 ref_tm_mday -= daysInMonth[ref_tm_mon];
485 tmstruct->tm_isdst = 0;
490 Int_t utc_sec = tmstruct->tm_sec +
491 tmstruct->tm_min*60 +
492 tmstruct->tm_hour*3600 +
493 tmstruct->tm_yday*86400 +
494 (tmstruct->tm_year-70)*31536000 +
495 ((tmstruct->tm_year-69)/4)*86400;
545 printf(
" tm { year %4d, mon %2d, day %2d,\n",
549 printf(
" hour %2d, min %2d, sec %2d,\n",
553 printf(
" wday %2d, yday %3d, isdst %2d",
560 printf(
",\n tm_gmtoff %7ld, tm_zone \"%s\"",
562 tmstruct.tm_gmtoff,tmstruct.tm_zone);
564 tmstruct.__tm_gmtoff,tmstruct.__tm_zone);