#include "RConfig.h"
#include <time.h>
#ifdef WIN32
#include "Windows4Root.h"
#include <string.h>
#endif
#include "TBuffer.h"
#include "Strlen.h"
#include "TDatime.h"
#include "TError.h"
#include "Bytes.h"
#include "TString.h"
ClassImp(TDatime)
TDatime::TDatime()
{
Set();
}
TDatime::TDatime(Int_t date, Int_t time)
{
Set(date, time);
}
TDatime::TDatime(Int_t year, Int_t month, Int_t day,
Int_t hour, Int_t min, Int_t sec)
{
Set(year, month, day, hour, min, sec);
}
TDatime::TDatime(const char *sqlDateTime)
{
Set(sqlDateTime);
}
Int_t TDatime::GetDayOfWeek() const
{
static TString weekDays[7] = { "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
TString wd = AsString();
int day;
for (day = 0; day < 7; day++) {
if (wd(0, 3) == weekDays[day])
break;
}
return (day < 7) ? day+1: -1;
}
const char *TDatime::AsString() const
{
time_t t = Convert();
char *retStr = ctime(&t);
if (retStr) {
*(retStr + 24) = 0;
return retStr;
} else {
static const char *defaulttime = "15/06/96";
Error("TDatime::AsString", "could not get time string");
return defaulttime;
}
}
const char *TDatime::AsString(char *out) const
{
time_t t = Convert();
#ifdef _REENTRANT
#if defined(R__SOLARIS) && (_POSIX_C_SOURCE - 0 < 199506L)
char *retStr = ctime_r(&t, out, 26);
#else
char *retStr = ctime_r(&t, out);
#endif
#else
char *retStr = ctime(&t);
#endif
if (retStr) {
*(retStr + 24) = 0;
#ifndef _REENTRANT
strcpy(out, retStr);
#endif
return retStr;
} else {
static const char *defaulttime = "15/06/96";
strcpy(out, defaulttime);
Error("TDatime::AsString", "could not get time string");
return defaulttime;
}
}
const char *TDatime::AsSQLString() const
{
static char sqldate[20];
UInt_t year = fDatime>>26;
UInt_t month = (fDatime<<6)>>28;
UInt_t day = (fDatime<<10)>>27;
UInt_t hour = (fDatime<<15)>>27;
UInt_t min = (fDatime<<20)>>26;
UInt_t sec = (fDatime<<26)>>26;
sprintf(sqldate, "%04d-%02d-%02d %02d:%02d:%02d", (year+1995), month, day,
hour, min, sec);
return sqldate;
}
UInt_t TDatime::Convert(Bool_t toGMT) const
{
UInt_t year = fDatime>>26;
UInt_t month = (fDatime<<6)>>28;
UInt_t day = (fDatime<<10)>>27;
UInt_t hour = (fDatime<<15)>>27;
UInt_t min = (fDatime<<20)>>26;
UInt_t sec = (fDatime<<26)>>26;
struct tm tp;
tp.tm_year = year+95;
tp.tm_mon = month-1;
tp.tm_mday = day;
tp.tm_hour = hour;
tp.tm_min = min;
tp.tm_sec = sec;
tp.tm_isdst = -1;
time_t t = mktime(&tp);
if ((int)t == -1) {
Error("TDatime::Convert", "error converting fDatime to time_t");
return 0;
}
if (toGMT) {
struct tm *tg;
tg = gmtime(&t);
tg->tm_isdst = -1;
t = mktime(tg);
}
return (UInt_t)t;
}
void TDatime::Copy(TDatime &datime) const
{
datime.fDatime = fDatime;
}
void TDatime::FillBuffer(char *&buffer)
{
tobuf(buffer, fDatime);
}
Int_t TDatime::GetDate() const
{
UInt_t year = fDatime>>26;
UInt_t month = (fDatime<<6)>>28;
UInt_t day = (fDatime<<10)>>27;
return 10000*(year+1995) + 100*month + day;
}
Int_t TDatime::GetTime() const
{
UInt_t hour = (fDatime<<15)>>27;
UInt_t min = (fDatime<<20)>>26;
UInt_t sec = (fDatime<<26)>>26;
return 10000*hour + 100*min + sec;
}
void TDatime::Print(Option_t *) const
{
printf("Date/Time = %s\n", AsString());
}
void TDatime::ReadBuffer(char *&buffer)
{
frombuf(buffer, &fDatime);
}
void TDatime::Set()
{
#ifndef WIN32
time_t tloc = time(0);
struct tm tp;
localtime_r(&tloc, &tp);
UInt_t year = tp.tm_year;
UInt_t month = tp.tm_mon + 1;
UInt_t day = tp.tm_mday;
UInt_t hour = tp.tm_hour;
UInt_t min = tp.tm_min;
UInt_t sec = tp.tm_sec;
#else
SYSTEMTIME tp;
GetLocalTime(&tp);
UInt_t year = tp.wYear-1900;
UInt_t month = tp.wMonth;
UInt_t day = tp.wDay;
UInt_t hour = tp.wHour;
UInt_t min = tp.wMinute;
UInt_t sec = tp.wSecond;
#endif
fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
}
void TDatime::Set(UInt_t tloc, Bool_t dosDate)
{
UInt_t year, month, day, hour, min, sec;
if (dosDate) {
year = ((tloc >> 25) & 0x7f) + 80;
month = ((tloc >> 21) & 0xf);
day = (tloc >> 16) & 0x1f;
hour = (tloc >> 11) & 0x1f;
min = (tloc >> 5) & 0x3f;
sec = (tloc & 0x1f) * 2;
} else {
time_t t = (time_t) tloc;
struct tm *tp = localtime(&t);
year = tp->tm_year;
month = tp->tm_mon + 1;
day = tp->tm_mday;
hour = tp->tm_hour;
min = tp->tm_min;
sec = tp->tm_sec;
}
fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
}
void TDatime::Set(Int_t date, Int_t time)
{
if (date > 19000000) date -= 19000000;
if (date < 950101) {
Error("TDatime::Set", "year smaller than 1995");
return;
}
Int_t year = date/10000;
Int_t month = (date-year*10000)/100;
Int_t day = date%100;
Int_t hour, min, sec;
hour = time/10000;
min = (time-hour*10000)/100;
sec = time%100;
fDatime = (year-95)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
}
void TDatime::Set(Int_t year, Int_t month, Int_t day,
Int_t hour, Int_t min, Int_t sec)
{
if (year < 200) year += 1900;
if (year < 1995) {
Error("TDatime::Set", "year must be >= 1995");
return;
}
fDatime = (year-1995)<<26 | month<<22 | day<<17 | hour<<12 | min<<6 | sec;
}
void TDatime::Set(const char* sqlDateTime)
{
Int_t yy, mm, dd, hh, mi, ss;
if (sscanf(sqlDateTime, "%d-%d-%d %d:%d:%d", &yy, &mm, &dd, &hh, &mi, &ss) == 6)
Set(yy, mm, dd, hh, mi, ss);
else {
Error("TDatime(sqlDatTime)", "input string not in right format, set"
" to current date/time");
Set();
}
}
void TDatime::Streamer(TBuffer &b)
{
if (b.IsReading()) {
b >> fDatime;
} else {
b << fDatime;
}
}
void TDatime::GetDateTime(UInt_t datetime, Int_t &date, Int_t &time)
{
UInt_t year = datetime>>26;
UInt_t month = (datetime<<6)>>28;
UInt_t day = (datetime<<10)>>27;
UInt_t hour = (datetime<<15)>>27;
UInt_t min = (datetime<<20)>>26;
UInt_t sec = (datetime<<26)>>26;
date = 10000*(year+1995) + 100*month + day;
time = 10000*hour + 100*min + sec;
}
Last change: Wed Jun 25 08:36:04 2008
Last generated: 2008-06-25 08:36
This page has been automatically generated. If you have any comments or suggestions about the page layout send a mail to ROOT support, or contact the developers with any questions or problems regarding ROOT.