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;
194 Double_t Pi = 3.14159265358979323846;
196 Double_t epsilon = (23.4393 - 0.0000004 * D) * Pi / 180.0;
197 Double_t L = (280.47 + 0.98565 * D) * Pi / 180.0;
198 Double_t Omega = (125.04 - 0.052954 * D) * Pi / 180.0;
199 Double_t Deltapsi = -0.000319 * std::sin(Omega) - 0.000024 * std::sin(2.0 * L);
200 Double_t eqeq = Deltapsi * std::cos(epsilon);
202 return rval < 0 ? rval + 24.0 : rval;
218 return rval < 0 ? rval + 24.0 : rval;
234 return rval < 0 ? rval + 24.0 : rval;
274 const Int_t nbuffers = 8;
275 constexpr std::size_t bufferSize = 64;
277 static Char_t formatted[nbuffers][bufferSize];
278 static Char_t formatted2[nbuffers][bufferSize];
280 static Int_t ibuffer = nbuffers;
284 ibuffer = (ibuffer+1)%nbuffers;
292 return formatted[ibuffer];
297 const Char_t *kRFC822 =
"%a, %d %b %Y %H:%M:%S %z (%Z) +#9ld nsec";
298 const Char_t *kISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%z";
299 const Char_t *kISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
302 const Char_t *kRFC822 =
"%a, %d %b %Y %H:%M:%S %Z +#9ld nsec";
303 const Char_t *kISO8601 =
"%Y-%m-%d %H:%M:%S.#9.9ld%Z";
304 const Char_t *kISO8601Z =
"%Y-%m-%d %H:%M:%S.#9.9ldZ";
306 const Char_t *kSQL =
"%Y-%m-%d %H:%M:%S";
310 if (asSQL) asLocal =
kFALSE;
312 const Char_t *format = kRFC822;
315 if (!asLocal) format = kISO8601Z;
317 if (asSQL) format = kSQL;
320 time_t seconds = (time_t)
fSec;
323 struct tm *ptm = (asLocal) ? localtime_r(&seconds, &buf) : gmtime_r(&seconds, &buf);
325 struct tm *ptm = (asLocal) ? localtime(&seconds) : gmtime(&seconds);
330 strftime(formatted[ibuffer],
sizeof(formatted[ibuffer]), format, ptm);
332 if (asSQL)
return formatted[ibuffer];
335 Char_t *ptr = strrchr(formatted[ibuffer],
'#');
339 return formatted2[ibuffer];
358 time_t atime =
fSec + secOffset;
361 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
363 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
366 if (day) *day = ptm->tm_mday;
367 if (month) *month = ptm->tm_mon + 1;
368 if (year) *year = ptm->tm_year + 1900;
370 return (1900+ptm->tm_year)*10000 + (1+ptm->tm_mon)*100 + ptm->tm_mday;
380 time_t atime =
fSec + secOffset;
383 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
385 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
388 if (hour) *hour = ptm->tm_hour;
389 if (min) *min = ptm->tm_min;
390 if (sec) *sec = ptm->tm_sec;
392 return ptm->tm_hour*10000 + ptm->tm_min*100 + ptm->tm_sec;
401 time_t atime =
fSec + secOffset;
404 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
406 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
409 Int_t day = ptm->tm_mday;
410 Int_t month = ptm->tm_mon + 1;
411 Int_t year = ptm->tm_year + 1900;
422 time_t atime =
fSec + secOffset;
425 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
427 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
430 Int_t day = ptm->tm_mday;
431 Int_t month = ptm->tm_mon + 1;
432 Int_t year = ptm->tm_year + 1900;
442 time_t atime =
fSec + secOffset;
445 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
447 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
450 return ptm->tm_mon + 1;
460 time_t atime =
fSec + secOffset;
463 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
465 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
468 Int_t day = ptm->tm_mday;
469 Int_t month = ptm->tm_mon + 1;
470 Int_t year = ptm->tm_year + 1900;
472 return GetWeek(day, month, year);
488 time_t atime =
fSec + secOffset;
491 struct tm *ptm = (inUTC) ? gmtime_r(&atime, &buf) : localtime_r(&atime, &buf);
493 struct tm *ptm = (inUTC) ? gmtime(&atime) : localtime(&atime);
496 Int_t year = ptm->tm_year + 1900;
511#if defined(R__WINGCC)
514#if !defined(R__FBSD) && !defined(R__OBSD)
521 return -localtime_r(&tp, &buf)->tm_gmtoff;
523 return -localtime(&tp)->tm_gmtoff;
548 printf(
"Date/Time = %s\n",
AsString(option));
562 GetSystemTimeAsFileTime((FILETIME *)&time);
567 (
unsigned __int64) 1000000000);
569 (
unsigned __int64) (1000*1000*10)
570 * (
unsigned __int64) (60 * 60 * 24)
571 * (
unsigned __int64) (134774);
573 fSec =
Int_t(time.QuadPart/(
unsigned __int64) (1000*1000*10));
576 gettimeofday(&tp,
nullptr);
581 static Int_t sec = 0, nsec = 0, fake_ns = 0;
614 if (year <= 37) year += 2000;
615 if (year >= 70 && year <= 137) year += 1900;
617 if (year >= 1900) year -= 1900;
620 tmstruct.tm_year = year;
621 tmstruct.tm_mon = month-1;
622 tmstruct.tm_mday = day;
623 tmstruct.tm_hour = hour;
624 tmstruct.tm_min = min;
625 tmstruct.tm_sec = sec + secOffset;
626 tmstruct.tm_isdst = -1;
628 const time_t bad_time_t = (time_t) -1;
633 time_t utc_sec = (isUTC) ?
MktimeFromUTC(&tmstruct) : mktime(&tmstruct);
635 if (utc_sec == bad_time_t)
636 Error(
"TTimeStamp::Set",
"mktime returned -1");
665 Int_t year = date/10000;
666 Int_t month = (date-year*10000)/100;
667 Int_t day = date%100;
670 const Int_t oneday = 240000;
675 while (time > oneday) {
679 Int_t hour = time/10000;
680 Int_t min = (time-hour*10000)/100;
681 Int_t sec = time%100;
683 Set(year, month, day, hour, min, sec, nsec, isUTC, secOffset);
695 memset (&localtm, 0,
sizeof (localtm));
698 localtm.tm_year = ((tloc >> 25) & 0x7f) + 80;
699 localtm.tm_mon = ((tloc >> 21) & 0xf);
700 localtm.tm_mday = (tloc >> 16) & 0x1f;
701 localtm.tm_hour = (tloc >> 11) & 0x1f;
702 localtm.tm_min = (tloc >> 5) & 0x3f;
703 localtm.tm_sec = (tloc & 0x1f) * 2 + secOffset;
704 localtm.tm_isdst = -1;
706 time_t t = (time_t) tloc;
709 struct tm *tp = localtime_r(&t, &tpa);
711 struct tm *tp = localtime(&t);
713 localtm.tm_year = tp->tm_year;
714 localtm.tm_mon = tp->tm_mon;
715 localtm.tm_mday = tp->tm_mday;
716 localtm.tm_hour = tp->tm_hour;
717 localtm.tm_min = tp->tm_min;
718 localtm.tm_sec = tp->tm_sec + secOffset;
719 localtm.tm_isdst = -1;
722 const time_t bad_time_t = (time_t) -1;
727 time_t utc_sec = (isUTC && dosDate) ?
MktimeFromUTC(&localtm) : mktime(&localtm);
729 if (utc_sec == bad_time_t)
730 Error(
"TTimeStamp::Set",
"mktime returned -1");
743 const Int_t kNsPerSec = 1000000000;
771 Int_t daysInMonth[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
772 Int_t year = tmstruct->tm_year + 1900;
777 Int_t &ref_tm_mon = tmstruct->tm_mon;
778 Int_t &ref_tm_mday = tmstruct->tm_mday;
780 tmstruct->tm_yday = 0;
781 for (
Int_t imonth = 0; imonth < ref_tm_mon; imonth++) {
782 tmstruct->tm_yday += daysInMonth[imonth];
784 tmstruct->tm_yday += ref_tm_mday - 1;
787 while (ref_tm_mday > daysInMonth[ref_tm_mon]) {
788 ref_tm_mday -= daysInMonth[ref_tm_mon];
795 tmstruct->tm_isdst = 0;
800 Int_t utc_sec = tmstruct->tm_sec +
801 tmstruct->tm_min*60 +
802 tmstruct->tm_hour*3600 +
803 tmstruct->tm_yday*86400 +
804 (tmstruct->tm_year-70)*31536000 +
805 ((tmstruct->tm_year-69)/4)*86400;
817 Int_t daysInMonth[] = { 31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
820 for (
Int_t i = 0; i < (month - 1); i++)
821 dayOfYear += daysInMonth[i];
840 dayno = 1 + day + 2*month + 3*(month + 1)/5 + year + year/4 - year/100 + year/400;
844 return ((dayno == 0) ? 7 : dayno);
856 Int_t week = (dayOfYear + dayJan1st - 2) / 7 + 1;
863 if (dayNextJan1st > 1 && dayNextJan1st < 5) {
867 }
else if (week == 0) {
869 week = (dayPrevJan1st < 5 && dayJan1st > 4) ? 53 : 52;
872 return year * 100 + week;
888 return (year % 4 == 0) && !((year % 100 == 0) && (year % 400 > 0));
907 printf(
" tm { year %4d, mon %2d, day %2d,\n",
911 printf(
" hour %2d, min %2d, sec %2d,\n",
915 printf(
" wday %2d, yday %3d, isdst %2d",
919#if (defined(linux) && !defined(R__WINGCC)) || defined(R__MACOSX)
920 printf(
",\n tm_gmtoff %6ld, tm_zone \"%s\"",
921#
if defined(__USE_BSD) || defined(R__MACOSX) || defined(__USE_MISC)
922 tmstruct.tm_gmtoff, tmstruct.tm_zone);
924 tmstruct.__tm_gmtoff, tmstruct.__tm_zone);
void Streamer(TBuffer &) override
int Int_t
Signed integer 4 bytes (int).
char Char_t
Character 1 byte (char).
unsigned int UInt_t
Unsigned integer 4 bytes (unsigned int).
bool Bool_t
Boolean (0=false, 1=true) (bool).
double Double_t
Double 8 bytes.
const char Option_t
Option string (const char).
Error("WriteTObject","The current directory (%s) is not associated with a file. The object (%s) has not been written.", GetName(), objname)
TBuffer & operator>>(TBuffer &buf, TTimeStamp &ts)
Read time stamp from TBuffer.
std::ostream & operator<<(std::ostream &os, const TTimeStamp &ts)
Write time stamp to std::ostream.
externTVirtualMutex * gTimeMutex
#define R__LOCKGUARD2(mutex)
Buffer base class used for serializing objects.
void ToLower()
Change string to lower-case.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
The TTimeStamp encapsulates seconds and ns since EPOCH.
Bool_t IsLeapYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Is the year a leap year.
UInt_t GetTime(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *hour=nullptr, UInt_t *min=nullptr, UInt_t *sec=nullptr) const
Return time in form of 123623 (i.e.
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.
Int_t GetDayOfYear(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the day of the year represented by this time stamp value.
static void DumpTMStruct(const tm_t &tmstruct)
Print out the "tm" structure:
Int_t GetWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Get the week of the year.
TTimeStamp()
Default ctor.
Double_t AsJulianDate() const
void Add(const TTimeStamp &offset)
Add "offset" as a delta time.
void Copy(TTimeStamp &ts) const
Copy this to ts.
virtual void Streamer(TBuffer &)
Int_t GetDayOfWeek(Bool_t inUTC=kTRUE, Int_t secOffset=0) const
Method is using Zeller's formula for calculating the day number.
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.
Double_t AsGMST(Double_t UT1Offset=0) const
Return Greenwich mean sidereal time (GMST) in hour-angle.
void Print(const Option_t *option="") const
Print date and time.
void Set()
Set Date/Time to current time as reported by the system.
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.
static time_t MktimeFromUTC(tm_t *tmstruct)
Equivalent of standard routine "mktime" but using the assumption that tm struct is filled with UTC,...
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.
const char * AsString(const Option_t *option="") const
Return the date & time as a string.
UInt_t GetDate(Bool_t inUTC=kTRUE, Int_t secOffset=0, UInt_t *year=nullptr, UInt_t *month=nullptr, UInt_t *day=nullptr) const
Return date in form of 19971224 (i.e.
void NormalizeNanoSec()
Ensure that the fNanoSec field is in range [0,999999999].
This class implements a mutex interface.