58 TDatime::TDatime(Int_t date, Int_t time)
67 TDatime::TDatime(Int_t year, Int_t month, Int_t day,
68 Int_t hour, Int_t min, Int_t sec)
70 Set(year, month, day, hour, min, sec);
77 TDatime::TDatime(
const char *sqlDateTime)
85 Int_t TDatime::GetDayOfWeek()
const
87 static TString weekDays[7] = {
"Mon",
"Tue",
"Wed",
"Thu",
"Fri",
"Sat",
"Sun" };
88 TString wd = AsString();
90 for (day = 0; day < 7; day++) {
91 if (wd(0, 3) == weekDays[day])
94 return (day < 7) ? day+1: -1;
101 const char *TDatime::AsString()
const
103 time_t t = Convert();
104 char *retStr = ctime(&t);
109 static const char *defaulttime =
"15/06/96";
110 Error(
"TDatime::AsString",
"could not get time string");
120 const char *TDatime::AsString(
char *out)
const
122 time_t t = Convert();
124 #if defined(R__SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L)
125 char *retStr = ctime_r(&t, out, 26);
127 char *retStr = ctime_r(&t, out);
130 char *retStr = ctime(&t);
139 static const char *defaulttime =
"15/06/96";
140 strcpy(out, defaulttime);
141 Error(
"TDatime::AsString",
"could not get time string");
151 const char *TDatime::AsSQLString()
const
153 static char sqldate[20];
155 UInt_t year = fDatime>>26;
156 UInt_t month = (fDatime<<6)>>28;
157 UInt_t day = (fDatime<<10)>>27;
158 UInt_t hour = (fDatime<<15)>>27;
159 UInt_t min = (fDatime<<20)>>26;
160 UInt_t sec = (fDatime<<26)>>26;
162 snprintf(sqldate,20,
"%04d-%02d-%02d %02d:%02d:%02d", (year+1995), month, day,
181 UInt_t TDatime::Convert(Bool_t toGMT)
const
183 UInt_t year = fDatime>>26;
184 UInt_t month = (fDatime<<6)>>28;
185 UInt_t day = (fDatime<<10)>>27;
186 UInt_t hour = (fDatime<<15)>>27;
187 UInt_t min = (fDatime<<20)>>26;
188 UInt_t sec = (fDatime<<26)>>26;
191 tp.tm_year = year+95;
199 time_t t = mktime(&tp);
201 Error(
"TDatime::Convert",
"error converting fDatime to time_t");
207 struct tm *tgp = gmtime_r(&t, &tg);
209 struct tm *tgp = gmtime(&t);
220 void TDatime::Copy(TDatime &datime)
const
222 datime.fDatime = fDatime;
228 void TDatime::FillBuffer(
char *&buffer)
230 tobuf(buffer, fDatime);
239 UInt_t TDatime::Get()
const
247 Int_t TDatime::GetDate()
const
249 UInt_t year = fDatime>>26;
250 UInt_t month = (fDatime<<6)>>28;
251 UInt_t day = (fDatime<<10)>>27;
252 return 10000*(year+1995) + 100*month + day;
258 Int_t TDatime::GetTime()
const
260 UInt_t hour = (fDatime<<15)>>27;
261 UInt_t min = (fDatime<<20)>>26;
262 UInt_t sec = (fDatime<<26)>>26;
263 return 10000*hour + 100*min + sec;
269 void TDatime::Print(Option_t *)
const
271 printf(
"Date/Time = %s\n", AsString());
277 void TDatime::ReadBuffer(
char *&buffer)
279 frombuf(buffer, &fDatime);
291 time_t tloc = time(0);
294 struct tm *tp = localtime_r(&tloc, &tpa);
296 struct tm *tp = localtime(&tloc);
298 UInt_t year = tp->tm_year;
299 UInt_t month = tp->tm_mon + 1;
300 UInt_t day = tp->tm_mday;
301 UInt_t hour = tp->tm_hour;
302 UInt_t min = tp->tm_min;
303 UInt_t sec = tp->tm_sec;
307 UInt_t year = tp.wYear-1900;
308 UInt_t month = tp.wMonth;
309 UInt_t day = tp.wDay;
310 UInt_t hour = tp.wHour;
311 UInt_t min = tp.wMinute;
312 UInt_t sec = tp.wSecond;
315 fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
324 void TDatime::Set(UInt_t tloc, Bool_t dosDate)
326 UInt_t year, month, day, hour, min, sec;
329 year = ((tloc >> 25) & 0x7f) + 80;
330 month = ((tloc >> 21) & 0xf);
331 day = (tloc >> 16) & 0x1f;
332 hour = (tloc >> 11) & 0x1f;
333 min = (tloc >> 5) & 0x3f;
334 sec = (tloc & 0x1f) * 2;
336 time_t t = (time_t) tloc;
339 struct tm *tp = localtime_r(&t, &tpa);
341 struct tm *tp = localtime(&t);
344 month = tp->tm_mon + 1;
351 fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
362 void TDatime::Set(Int_t date, Int_t time)
364 if (date > 19000000) date -= 19000000;
366 Error(
"TDatime::Set",
"year smaller than 1995");
370 Int_t year = date/10000;
371 Int_t month = (date-year*10000)/100;
372 Int_t day = date%100;
374 Int_t hour, min, sec;
377 min = (time-hour*10000)/100;
380 fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
387 void TDatime::Set(Int_t year, Int_t month, Int_t day,
388 Int_t hour, Int_t min, Int_t sec)
390 if (year < 159) year += 1900;
392 Error(
"TDatime::Set",
"year must be >= 1995");
396 fDatime = (year-1995)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
403 void TDatime::Set(
const char* sqlDateTime)
405 Int_t yy, mm, dd, hh, mi, ss;
407 if (sscanf(sqlDateTime,
"%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &hh, &mi, &ss) == 6)
408 Set(yy, mm, dd, hh, mi, ss);
410 Error(
"TDatime(sqlDatTime)",
"input string not in right format, set"
411 " to current date/time");
419 void TDatime::Streamer(TBuffer &b)
434 void TDatime::GetDateTime(UInt_t datetime, Int_t &date, Int_t &time)
436 UInt_t year = datetime>>26;
437 UInt_t month = (datetime<<6)>>28;
438 UInt_t day = (datetime<<10)>>27;
439 UInt_t hour = (datetime<<15)>>27;
440 UInt_t min = (datetime<<20)>>26;
441 UInt_t sec = (datetime<<26)>>26;
442 date = 10000*(year+1995) + 100*month + day;
443 time = 10000*hour + 100*min + sec;
452 Int_t TDatime::GetGlobalDayFromDate(Int_t date)
455 Int_t dy = date / 10000;
456 Int_t dm = (date - dy*10000)/100;
457 Int_t dd = (date - dy*10000 - dm*100);
459 Int_t m = (dm + 9)%12;
461 return y*365 + y/4 - y/100 + y/400 + (m*306 + 5)/10 + (dd - 1);
469 Int_t TDatime::GetDateFromGlobalDay(Int_t day)
472 Int_t y = int((10000*ld + 14780)/3652425);
473 Int_t ddd = day - (y*365 + y/4 - y/100 + y/400);
476 ddd = day - (y*365 + y/4 - y/100 + y/400);
478 Int_t mi = (52 + 100*ddd)/3060;
479 Int_t dy = y + (mi + 2)/12;
480 Int_t dm = (mi + 2)%12 + 1;
481 Int_t dd = ddd - (mi*306 + 5)/10 + 1;
483 return dy*10000 + dm*100 + dd;
495 Int_t TDatime::GetLegalGlobalDayFromDate(Int_t date)
497 static Int_t calstart = 0;
499 calstart = TDatime::GetGlobalDayFromDate(15821001);
500 Int_t d = TDatime::GetGlobalDayFromDate(date);
502 ::Warning(
"TDatime::GetLegalGlobalDayFromDate",
"dates before Oct. 1582 are inaccurate.");
503 Int_t dte = TDatime::GetDateFromGlobalDay(d);
505 ::Error(
"TDatime::GetLegalGlobalDayFromDate",
"illegal date %d", dte);
514 std::string cling::printValue(
const TDatime* val) {
516 return std::string(val->AsString(buf));