Form(), simple way to pass today's date!

Dear All,
I am facing this kind of problem.

I have to pass a directory to look for a .root file, like:

TFile* file1D = new TFile("pngResults/2019-09-18/Results.root");

As you can see, I am passing the date inside the string.
At the moment I am using a handful of files, so I can change them each time, but it is a bit unhealthy.

I was thinking of doing something like:

TFile* file1D = new TFile(Form("pngResults/%s/Results.root", DateOfToday);

that is because I am running a bash script to redo everything each time with a new folder for each day I am running it in.

I understand that there is a ROOT class to provide the date (TDatime?).
Could you please help me a bit, and tell me if this way is feasible…?


root [0] TDatime d
root [1] cout << Form("pngResults/%d-%d-%d/Results.root",d.GetYear(),d.GetMonth(),d.GetDay()) << endl;

Many thanks!

This is exactly what I was looking for!!

And if I simply wanted to add in a zero somewhere (I am telling mainly for others who could possibly read later this post), I could use:

root [0] TDatime d
root [1] cout << Form("pngResults/%d-0%d-%d/Results.root",d.GetYear(),d.GetMonth(),d.GetDay()) << endl;

Many thanks again for your fast reply,

You should add the 0 like that because you will need a special case for months and days > 9.

the way to do it is:

cout << Form("pngResults/%d-%2.2d-%2.2d/Results.root",d.GetYear(),d.GetMonth(),d.GetDay()) << endl;


Thanks, I was not aware of this.
This is extremely useful.

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