64 if (os.tie()) os.tie()->flush();
68 if (os.flags() & std::ios::unitbuf) os.flush();
122 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
142 Set(date, time, nsec, isUTC, secOffset);
153 Set(tloc, isUTC, secOffset, dosDate);
172 Double_t D_r = D - fmod(2.0*D+1.0, 2.0)/2.0;
174 Double_t sidereal = (24110.54841 + 8640184.812866*T + 0.093142*T*T
175 - 0.0000062*T*T*T + (D - D_r)*86400.0*1.002737909350795)/3600.0;
176 Double_t rval = fmod(sidereal, 24.0);
177 return rval < 0 ? rval + 24.0 : rval;
201 return rval < 0 ? rval + 24.0 : rval;
217 return rval < 0 ? rval + 24.0 : rval;
233 return rval < 0 ? rval + 24.0 : rval;
273 const Int_t nbuffers = 8;
275 static Char_t formatted[nbuffers][64];
276 static Char_t formatted2[nbuffers][64];
278 static Int_t ibuffer = nbuffers;
282 ibuffer = (ibuffer+1)%nbuffers;
290 return formatted[ibuffer];
295 const Char_t *kRFC822 =
"%a, %d %b %Y %H:%M:%S %z (%Z) +#9ld nsec";
296 const Char_t *kISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%z";
297 const Char_t *kISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
300 const Char_t *kRFC822 =
"%a, %d %b %Y %H:%M:%S %Z +#9ld nsec";
301 const Char_t *kISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%Z";
302 const Char_t *kISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
304 const Char_t *kSQL =
"%Y-%m-%d %H:%M:%S";
308 if (asSQL) asLocal =
kFALSE;
313 if (!asLocal) format = kISO8601Z;
315 if (asSQL) format = kSQL;
318 time_t seconds = (time_t)
fSec;
321 struct tm *ptm = (asLocal) ? localtime_r(&seconds, &buf) : gmtime_r(&seconds, &buf);
323 struct tm *ptm = (asLocal) ? localtime(&seconds) : gmtime(&seconds);
328 strftime(formatted[ibuffer],
sizeof(formatted[ibuffer]), format, ptm);
330 if (asSQL)
return formatted[ibuffer];
333 Char_t *ptr = strrchr(formatted[ibuffer],
'#');
335 sprintf(formatted2[ibuffer], formatted[ibuffer],
fNanoSec);
337 return formatted2[ibuffer];
356 time_t atime =
fSec + secOffset;
359 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
361 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
364 if (day) *day = ptm->tm_mday;
365 if (month) *month = ptm->tm_mon + 1;
366 if (year) *year = ptm->tm_year + 1900;
368 return (1900+ptm->tm_year)*10000 + (1+ptm->tm_mon)*100 + ptm->tm_mday;
378 time_t atime =
fSec + secOffset;
381 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
383 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
386 if (hour) *hour = ptm->tm_hour;
387 if (min) *min = ptm->tm_min;
388 if (sec) *sec = ptm->tm_sec;
390 return ptm->tm_hour*10000 + ptm->tm_min*100 + ptm->tm_sec;
399 time_t atime =
fSec + secOffset;
402 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
404 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
407 Int_t day = ptm->tm_mday;
408 Int_t month = ptm->tm_mon + 1;
409 Int_t year = ptm->tm_year + 1900;
420 time_t atime =
fSec + secOffset;
423 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
425 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
428 Int_t day = ptm->tm_mday;
429 Int_t month = ptm->tm_mon + 1;
430 Int_t year = ptm->tm_year + 1900;
440 time_t atime =
fSec + secOffset;
443 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
445 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
448 return ptm->tm_mon + 1;
458 time_t atime =
fSec + secOffset;
461 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
463 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
466 Int_t day = ptm->tm_mday;
467 Int_t month = ptm->tm_mon + 1;
468 Int_t year = ptm->tm_year + 1900;
470 return GetWeek(day, month, year);
486 time_t atime =
fSec + secOffset;
489 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
491 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
494 Int_t year = ptm->tm_year + 1900;
509 #if defined(R__WINGCC) 512 #if !defined(R__FBSD) && !defined(R__OBSD) 519 return -localtime_r(&tp, &buf)->tm_gmtoff;
521 return -localtime(&tp)->tm_gmtoff;
546 printf(
"Date/Time = %s\n",
AsString(option));
560 GetSystemTimeAsFileTime((FILETIME *)&time);
565 (
unsigned __int64) 1000000000);
567 (
unsigned __int64) (1000*1000*10)
568 * (
unsigned __int64) (60 * 60 * 24)
569 * (
unsigned __int64) (134774);
571 fSec =
Int_t(time.QuadPart/(
unsigned __int64) (1000*1000*10));
574 gettimeofday(&tp, 0);
579 static Int_t sec = 0, nsec = 0, fake_ns = 0;
612 if (year <= 37) year += 2000;
613 if (year >= 70 && year <= 137) year += 1900;
615 if (year >= 1900) year -= 1900;
618 tmstruct.tm_year = year;
619 tmstruct.tm_mon = month-1;
620 tmstruct.tm_mday = day;
621 tmstruct.tm_hour = hour;
622 tmstruct.tm_min = min;
623 tmstruct.tm_sec = sec + secOffset;
624 tmstruct.tm_isdst = -1;
626 const time_t bad_time_t = (time_t) -1;
631 time_t utc_sec = (isUTC) ?
MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
633 if (utc_sec == bad_time_t)
634 Error(
"TTimeStamp::Set",
"mktime returned -1");
663 Int_t year = date/10000;
664 Int_t month = (date-year*10000)/100;
665 Int_t day = date%100;
668 const Int_t oneday = 240000;
673 while (time > oneday) {
677 Int_t hour = time/10000;
678 Int_t min = (time-hour*10000)/100;
679 Int_t sec = time%100;
681 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
693 memset (&localtm, 0,
sizeof (localtm));
696 localtm.tm_year = ((tloc >> 25) & 0x7f) + 80;
697 localtm.tm_mon = ((tloc >> 21) & 0xf);
698 localtm.tm_mday = (tloc >> 16) & 0x1f;
699 localtm.tm_hour = (tloc >> 11) & 0x1f;
700 localtm.tm_min = (tloc >> 5) & 0x3f;
701 localtm.tm_sec = (tloc & 0x1f) * 2 + secOffset;
702 localtm.tm_isdst = -1;
704 time_t t = (time_t) tloc;
707 struct tm *tp = localtime_r(&t, &tpa);
709 struct tm *tp = localtime(&t);
711 localtm.tm_year = tp->tm_year;
712 localtm.tm_mon = tp->tm_mon;
713 localtm.tm_mday = tp->tm_mday;
714 localtm.tm_hour = tp->tm_hour;
715 localtm.tm_min = tp->tm_min;
716 localtm.tm_sec = tp->tm_sec + secOffset;
717 localtm.tm_isdst = -1;
720 const time_t bad_time_t = (time_t) -1;
725 time_t utc_sec = (isUTC && dosDate) ?
MktimeFromUTC(&localtm) : mktime(&localtm);
727 if (utc_sec == bad_time_t)
728 Error(
"TTimeStamp::Set",
"mktime returned -1");
741 const Int_t kNsPerSec = 1000000000;
769 Int_t daysInMonth[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
770 Int_t year = tmstruct->tm_year + 1900;
775 Int_t &ref_tm_mon = tmstruct->tm_mon;
776 Int_t &ref_tm_mday = tmstruct->tm_mday;
778 tmstruct->tm_yday = 0;
779 for (
Int_t imonth = 0; imonth < ref_tm_mon; imonth++) {
780 tmstruct->tm_yday += daysInMonth[imonth];
782 tmstruct->tm_yday += ref_tm_mday - 1;
785 while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
786 ref_tm_mday -= daysInMonth[ref_tm_mon];
793 tmstruct->tm_isdst = 0;
798 Int_t utc_sec = tmstruct->tm_sec +
799 tmstruct->tm_min*60 +
800 tmstruct->tm_hour*3600 +
801 tmstruct->tm_yday*86400 +
802 (tmstruct->tm_year-70)*31536000 +
803 ((tmstruct->tm_year-69)/4)*86400;
815 Int_t daysInMonth[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
818 for (
Int_t i = 0; i < (month - 1); i++)
819 dayOfYear += daysInMonth[i];
838 dayno = 1 + day + 2*month + 3*(month + 1)/5 + year + year/4 - year/100 + year/400;
842 return ((dayno == 0) ? 7 : dayno);
854 Int_t week = (dayOfYear + dayJan1st - 2) / 7 + 1;
861 if (dayNextJan1st > 1 && dayNextJan1st < 5) {
865 }
else if (week == 0) {
867 week = (dayPrevJan1st < 5 && dayJan1st > 4) ? 53 : 52;
870 return year * 100 + week;
886 return (year % 4 == 0) && !((year % 100 == 0) && (year % 400 > 0));
905 printf(
" tm { year %4d, mon %2d, day %2d,\n",
909 printf(
" hour %2d, min %2d, sec %2d,\n",
913 printf(
" wday %2d, yday %3d, isdst %2d",
917 #if (defined(linux) && !defined(R__WINGCC)) || defined(R__MACOSX) 918 printf(
",\n tm_gmtoff %6ld, tm_zone \"%s\"",
919 #
if defined(__USE_BSD) || defined(R__MACOSX) || defined(__USE_MISC)
920 tmstruct.tm_gmtoff, tmstruct.tm_zone);
922 tmstruct.__tm_gmtoff, tmstruct.__tm_zone);
static time_t MktimeFromUTC(tm_t *tmstruct)
Equivalent of standard routine "mktime" but using the assumption that tm struct is filled with UTC...
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
Double_t AsLAST(Double_t Longitude, Double_t UT1Offset=0) const
Return local apparent sidereal time (LAST) in hour-angle, given a longitude in degrees.
RooArgList L(const RooAbsArg &v1)
Int_t GetMonth(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the month of the year. Valid return values are between 1 and 12.
Buffer base class used for serializing objects.
This class implements a mutex interface.
void ToLower()
Change string to lower-case.
Double_t AsJulianDate() const
Double_t AsLMST(Double_t Longitude, Double_t UT1Offset=0) const
Return local mean sidereal time (LMST) in hour-angle, given a longitude in degrees.
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
static std::string format(double x, double y, int digits, int width)
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller's formula for calculating the day number.
Int_t GetDayOfYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the day of the year represented by this time stamp value.
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
void Print(const Option_t *option="") const
Print date and time.
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
void Set()
Set Date/Time to current time as reported by the system.
TVirtualMutex * gTimeMutex
void Error(const char *location, const char *msgfmt,...)
TTimeStamp()
Default ctor.
static void DumpTMStruct(const tm_t &tmstruct)
Print out the "tm" structure: tmstruct.tm_year = year; // years since 1900 tmstruct.tm_mon = month-1; // months since Jan [0,11] tmstruct.tm_mday = day; // day of the month [1,31] tmstruct.tm_hour = hour; // hours since midnight [0,23] tmstruct.tm_min = min; // minutes after the hour [0,59] tmstruct.tm_sec = sec; // seconds after the minute [0,59] tmstruct.tm_wday // day of week [0,6] tmstruct.tm_yday // days in year [0,365] tmstruct.tm_isdst // DST [-1/0/1] (unknown,false,true).
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
#define R__LOCKGUARD2(mutex)
The TTimeStamp encapsulates seconds and ns since EPOCH.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
UInt_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *year=0, UInt_t *month=0, UInt_t *day=0) const
Return date in form of 19971224 (i.e.
void Copy(TTimeStamp &ts) const
Copy this to ts.
Double_t AsGAST(Double_t UT1Offset=0) const
Return Greenwich apparent sidereal time (GAST) in hour-angle.
static Int_t GetZoneOffset()
Static method returning local (current) time zone offset from UTC.
UInt_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *hour=0, UInt_t *min=0, UInt_t *sec=0) const
Return time in form of 123623 (i.e.
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.