How to customize seconds with SetTimeFormat?

Hello,

I am playing with SetTimeFormat and I am facing same issue as Configure sub-second display for a time axis.

I noticed that seconds are formatted as XsYYY. I want to customize, how to fix that ?
I would expect that %S should just return a number. The following format “%Y-%m-%d %H:%M:%S %Z” should return “2024-09-19 22:16:22 JST” not “2024-09-19 22:16:22s JST”

Alternatively is it possible to pass a function either to SetTimeFormat of SetLabelFormat ?
I am considering to use my how implementation SetLabelFormat("MyMethod(%s)")

Thank you
M.

Hello Marco,

I am sure @couet and @linev have ideas about how to proceed.

Best,
D

There is no SetLabelFormat for the TAxis.
And SetTimeFormat supports only arguments listed here:

https://root.cern/doc/master/classTGaxis.html#GA14

It is not possible to invoke any function when formatting time axis labels.

Thank you linev for your confirmation.

From documentation there is an issue the time format. After looking at the code, it seems that TGAxis::SetTimeOffset is doing some fancy modifications of the time format. I believe this method should actually just store the offset information for computing the final result in the final method TGaxis::PaintAxis

%S is maybe not made to handle microtime in the first place, but one nice input would be to introduce %.2S such that we can control decimals (that is not the case at the moment, I believe)

void TGaxis::SetTimeOffset(Double_t toffset, Option_t *option)
{
   TString opt = option;
   opt.ToLower();

   char tmp[20];
   time_t timeoff;
   struct tm* utctis;
   
   Int_t idF = fTimeFormat.Index("%F");
   if (idF>=0) fTimeFormat.Remove(idF);
   fTimeFormat.Append("%F");

   timeoff = (time_t)((Long_t)(toffset));

   // offset is always saved in GMT to allow file transport
   // to different time zones
   utctis = gmtime(&timeoff);

   if (utctis != nullptr) {
      strftime(tmp, 20,"%Y-%m-%d %H:%M:%S",utctis);
      fTimeFormat.Append(tmp);
   } else {
      fTimeFormat.Append("1970-01-01 00:00:00");
   }

   // append the decimal part of the time offset
   Double_t ds = toffset-(Int_t)toffset;
   snprintf(tmp,20,"s%g",ds);
   fTimeFormat.Append(tmp);

   // add GMT/local option
   if (opt.Contains("gmt")) fTimeFormat.Append(" GMT");
}

Additionally, I don’t understand the purpose of the option parameter.
As an international standard, this should be written as UTC I believe.

Reading source code - I see usage strftime function which does not support fractional part of the seconds. ROOT manually adds fraction if %S appears in format string - but it always 5 digits after the “.”. See:

Maybe one can add some static variable at this place.
I guess @couet can have better idea

Yes microtime are not handle. The resolution is the second.

Ok, then I think I will not use SetTime* options. Thank you for the support.
I will most like set the first bin to 0 and based on the number of bins recompute the time axis on my own.

1 Like

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