Read date and time


I’ve a question about reading time and date from an .txt file. The lines I want to read in looks like:

2006.05.11 16:00:07.888 13.184082951038

Is there any possibility to read in this date and time with TDatime ?



One way to do it is by parsing the string through a regular
expression . For a detailed example and explanation of the
TPRegexp class, have a look at tutorials/regexp.C .

The code below does what you requested . I ignored the 13.18…
part because I do not know what it is .

  TString dateStr("2006.05.11 16:00:07.888 13.184082951038");

  const TPRegexp r("(\\d{4}).(\\d{2}).(\\d{2})\\s+(\\d{2}):(\\d{2}):([0-9.]+)");
  const TObjArray *subStrL = r.MatchS(dateStr);

  if (subStrL->GetLast()+1 >= 7)
    const Int_t year  = ((TObjString *)subStrL->At(1))->GetString().Atoi();
    const Int_t month = ((TObjString *)subStrL->At(2))->GetString().Atoi();
    const Int_t day   = ((TObjString *)subStrL->At(3))->GetString().Atoi();
    const Int_t hour  = ((TObjString *)subStrL->At(4))->GetString().Atoi();
    const Int_t min   = ((TObjString *)subStrL->At(5))->GetString().Atoi();
    const Int_t sec   = ((TObjString *)subStrL->At(6))->GetString().Atof();

    TDatime t(year,month,day,hour,min,sec);

  delete subStrL;