Subtracting days TDatime

I am trying to subtract days from a date using TDatetime and it gives the wrong answer when I try to find the month or year.

root [0] TDatime Date;
root [1] Date.Set(2018,7,1,0, 0, 0);
root [2] cout<<Date.AsString()<<endl;
Sun Jul 1 00:00:00 2018
root [3] TDatime Date2(Date.GetYear(),Date.GetMonth(),Date.GetDay()-1,Date.GetHour(), Date.GetMinute(), Date.GetSecond());
root [4] cout<<Date2.AsString()<<endl;
Sat Jun 30 00:00:00 2018
root [5] cout<<Date2.GetMonth()<<endl;
7

So as you can see here it gives the correct date if I access it via AsString() but not as GetMonth()


ROOT Version: v6-14-04
Platform: macOS
Compiler: Not Provided


What you are doing with Date2 is equivalent of setting a date Date3 like:

 TDatime Date3(2018,7,0,0,0,0);

ie: setting a day number as 0 … which is not really correct. I get the same effect with Date3 as your have with Date2

root [6] TDatime Date3(2018,7,0,0,0,0);
root [1] cout<<Date3.AsString()<<endl;
Sat Jun 30 00:00:00 2018
root [2] cout<<Date3.GetMonth()<<endl;
7

What is funny is that as a string Date3 is able to go back in June.

So if I have a date and want to go back to the day before (or many days before) and read out the year month and day as separate numbers how would I do it?

As the date returned as string is correctly computed may be doing a Set back with this setter will fix the correct date / … just an idea.

With some work, it could work for taking one day away, it’s not just Date3.Set(Date2.AsString()); as have to rearrange the string and convert month, but if I want to take 2 days away then the string output gives.

root [6] Date2.Set(Date.GetYear(),Date.GetMonth(),Date.GetDay()-2,Date.GetHour(),Date.GetMinute(),Date.GetSecond());
root [7] cout<<Date2.AsString()<<endl;
Mon Mar 31 00:00:00 2059

It seems that I can do what I want using TTimeStamp
root [0] TTimeStamp date(2018, 7, 1-60, 0,0,0, 0, kFALSE, 0)
(TTimeStamp &) @0x101ac77c8
root [1] date.GetDate()
(unsigned int) 20180502
root [2] //To get year
root [3] date.GetDate()/10000
(unsigned int) 2018
root [4] //To get month
root [5] (date.GetDate()-(date.GetDate()/1000010000))/100
(unsigned int) 5
root [6] //To get day
root [7] (date.GetDate()-(date.GetDate()/10000
10000))-(date.GetDate()-(date.GetDate()/1000010000))/100100
(unsigned int) 2

Thanks for your help

Yes that’s a better solution.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.