libtime++: Date and time calculation

◆ timestring() [3/3]

std::string libtime::TAbsoluteTime::timestring ( const std::string &  format) const

return string representation of time.

The function takes a format definition and passes it to strftime(3) to receive a formatted time string.

Parameters
formatformat string, see strftime(3)
Returns
time string in requested format

Definition at line 49 of file formatted_timestring.cc.

References day(), doy(), libtime::TBaseClassTime::hour(), libtime_assert, libtime::TBaseClassTime::minute(), month(), libtime::TBaseClassTime::second(), and year().

Referenced by Ainit_from_value(), and main().

50  {
51  const int incr=20;
52  const int nstart=format.length()+incr;
53  int n=nstart;
54  bool success=false;
55  struct std::tm timetm;
56  timetm.tm_year=this->year()-1900;
57  timetm.tm_mon=this->month()-1;
58  timetm.tm_mday=this->day();
59  timetm.tm_yday=this->doy();
60  timetm.tm_hour=this->hour();
61  timetm.tm_min=this->minute();
62  timetm.tm_sec=this->second();
63  timetm.tm_isdst=0;
64  timetm.tm_wday=0;
65  std::string retval;
66  while (!success)
67  {
68  char* ts=new char[n];
69  size_t nret=std::strftime(ts, n, format.c_str(), &timetm);
70  retval=std::string(ts);
71  delete[] ts;
72  if ((nret<(n-1))&&(nret>0))
73  {
74  success=true;
75  }
76  else
77  {
78  n+=incr;
79  libtime_assert(n<1000,
80  "ERROR TAbsoluteTime::timestring(): "
81  "time string is unreasonably long");
82  }
83  }
84  return retval;
85  }
timeint year() const
Definition: libtime++.h:337
#define libtime_assert(C, M)
Check an assertion and report by throwing an exception.
Definition: libtime++.h:714
timeint month() const
Definition: libtime++.h:339
timeint doy() const
Definition: libtime++.h:338
timeint day() const
Definition: libtime++.h:341
timeint hour() const
Definition: libtime++.h:290
timeint second() const
Definition: libtime++.h:292
timeint minute() const
Definition: libtime++.h:291
Here is the call graph for this function:
Here is the caller graph for this function: