Dear Rooters,
I recently tried to use the TDatime class to perform plotting and numerical operations with dates but I found out that the functions
TDatime::GetDateFromGlobalDay()
and
TDatime::GetGlobalDayFromDate()
are not implemented in root version 6 and the implementation present in older headers seems to not be correct.
Attached an implementation that I wrote within my framework to go around the issue that seems to be working with all performed tests. You will notice that the conversion tot he Gregorian calendar is performed with a cumulative array for the number of days per month of year. The input and output character string format is compatible with the TDatetime class.
Hope to help get this implemented in the next version.
unsigned int RadBase::GetGlobalDayFromDate(std::string date)
{
// date is in form yyyymmdd
unsigned int ui_date = atoi(date.c_str());
unsigned int dy = ui_date / 10000; // Extract year
unsigned int dm = (ui_date - dy * 10000) / 100; // Extract month
unsigned int dd = (ui_date - dy * 10000 - dm * 100); // Extract day of the month
dm = (dm + 9) % 12; // Shift month to start with 0 for March and add up to
// 11 for February to correctly acount for leap years
unsigned int df = dy; // Calculate the corrections year
if (dm / 10 > 0) df = df - dm/10;
// else dm--;
unsigned int cumulday[12] = {59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 0, 31};
return (dy-1)*365 + df/4 - df/100 + df/400 + cumulday[dm] + dd;
}
Int_t RadBase::GetDateFromGlobalDay(Int_t day)
{
unsigned int cumulday[12] = {31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
unsigned int y = (unsigned int)(day/365) + 1;
int d1 = day - ((y-1)*365 + (y-1)/4 - (y-1)/100 + (y-1)/400);
while (d1 < 0)
{
y--;
d1 = day - ((y-1)*365 + (y-1)/4 - (y-1)/100 + (y-1)/400);
}
unsigned int m = 0;
if (d1 != 0)
{
unsigned int y1 = y;
for (unsigned int k = 0; k < 12; k++)
{
if (d1 <= cumulday[k])
{
m = k + 1;
if (m < 3) y1 = y - 1;
if (k > 0) d1 = day - ((y-1)*365 + y1/4 - y1/100 + y1/400) - cumulday[k-1];
else d1 = day - ((y-1)*365 + y1/4 - y1/100 + y1/400);
if (d1 == 0) // we are at the last day of the month
{
unsigned int mday[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
d1 = mday[m - 1];
if (m == 2) // correction for february
{
if (y1 % 4) d1++;
if (y1 % 100) d1--;
if (y1 % 400) d1++;
}
}
break;
}
}
}
else { m = 12; d1 = 31; y--; } // we are at the last day of the year
if (GetDebug()) std::cout << __FUNCTION__ << " INFO: " << day << "\t" << d1
<< "\t" << y << "\t" << m << "\t" << d1 << std::endl;
return y * 10000 + m * 100 + d1;
}
// --------------------------------------------------------------------------------------------------------------
Functions are part of the radiation damage estimation framework available here:
Radiation Damage Calculation
Please read tips for efficient and successful posting and posting code
_ROOT Version: 6.24
_Platform: Windows 10
_Compiler: MSVC12