Home | News | Documentation | Download

TDatime inverse conversion

I have another question, now on TDatetime (in timeonaxis2.C). I understand that T0 is converted to an int X0 by X0 = T0.Convert(). Is there an inverse method which converts back T0 from X0? In my application, I store successive points in time in an ntuple, and I store the successive X0’s instead of T0’s since to store integers is easy and I don’t need the Convert() method in reading many times the ntuple only when I write it (only once). However, after reading the X0’s I’d like to print the Date and Time values and I don’t see methods for this on X0 only on T0. How to solve this problem? Thanks,

PS I think that this problem must have a solution since the X0 values after having read them from the ntuple are correctly plotted in the time axis by TGraph. Thanks for pointing me how it is done.

Hello, I have digged a little and see that UInt_t x0 = T0.TDatime::Get(); can be converted back with TDatime::GetDateTime(x0,date,time); but I still don’t see how int X0 = T0.Convert(); can be converted back. When applying TDatime::GetDateTime(X0,date,time); date and time will be filled with wrong values, unfortunately. As far as I see from examples, it is X0 and not x0 which should be used e.g. on plotting in TGraph.

Elemer also sent this code demonstrating the issue:

int GetMyDate(UInt_t Xm) {
    UInt_t year  = Xm>>26;
    UInt_t month = (Xm<<6)>>28;
    UInt_t day   = (Xm<<10)>>27;
    return 10000*(year+1995) + 100*month + day;
int GetMyTime(UInt_t Xm) {
    UInt_t hour  = (Xm<<15)>>27;
    UInt_t min   = (Xm<<20)>>26;
    UInt_t sec   = (Xm<<26)>>26;
    return 10000*hour + 100*min + sec;
void TempFormat() {
  int date, time;
  TDatime T0(2020, 7, 24, 16, 4, 10);
  UInt_t X0 = T0.Convert();
  printf("  \n Converted DateTime: %2d  \n ", X0 );
  printf("  \n Year: %2d Month: %2d Day: %2d Hour: %2d Minute: %2d Second: %2d \n ",
         T0.GetYear(), T0.GetMonth(), T0.GetDay(), T0.GetHour(), T0.GetMinute(), T0.GetSecond() );
  printf(" \n From Convert date: %2d time: %2d \n ", date, time );
  UInt_t x0 = T0.TDatime::Get();
  printf(" \n From Get date: %2d time: %2d \n ", date, time );
  date = GetMyDate(X0);
  time = GetMyTime(X0);
  printf(" \n From Convert and shifted date: %2d time: %2d \n ", date, time );

The result obtained by: root -l TempFormat.C

root [0] 
Processing TempFormat.C...
 Converted DateTime: 1595599450  
 Year: 2020 Month:  7 Day: 24 Hour: 16 Minute:  4 Second: 10 
 From Convert date: 20181213 time: 144126 
 From Get date: 20200724 time: 160410 
 From Convert and shifted date: 20181213 time: 144126 
 root [1]

Sorry to have missed that. It was in a new topic in a solved one.

When you do:

  TDatime T0(2020, 7, 24, 16, 4, 10);
  UInt_t X0 = T0.Convert();  

X0 contains the encoded date and time. To go back to the initial date you should use GetYear() GetMonth(), etc … all the getters are available. That would be the way to proceed… does it answer your question ?

No, it doesn’t answer my question. GetYear() etc. has to be applied on T0 and I would like to get back date and time from X0 and not from T0 by reasons I have mentioned in my post. As you can see from my code example, methods on X0 gives back wrong date and time.

Just create a new TDatime from X0;

root [0]   TDatime T0(2020, 7, 24, 16, 4, 10);
root [1]   UInt_t X0 = T0.Convert();
root [2]   TDatime T1(X0)
(TDatime &) Fri Jul 24 16:04:10 2020

Sorry, if I write the line TDatime T1(X0); in my code I see nothing. Also, my question was how can I fetch the date and time in integer values from X0 to use them in my offline code e.g. to print them out. Thanks.

Yes, I see now. I just apply GetYear() etc. on T1. Indeed, it answers my question. Thanks, Elemer

Exactly. I mark this as solved.
PS: if you have a new unrelated question, please create a new post :slight_smile: