libtime++: Date and time calculation
formatted_timestring.cc
Go to the documentation of this file.
1 
34 #define TF_FORMATTED_TIMESTRING_CC_VERSION \
35  "TF_FORMATTED_TIMESTRING_CC V1.0 "
36 
37 #include <libtime++.h>
38 #include <ctime>
39 
40 namespace libtime {
41 
49  std::string TAbsoluteTime::timestring(const std::string& format) const
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  }
86 
87 } // namespace libtime
88 
89 /* ----- END OF formatted_timestring.cc ----- */
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
std::string timestring() const
Definition: libtime++.h:267
timeint hour() const
Definition: libtime++.h:290
timeint second() const
Definition: libtime++.h:292
timeint minute() const
Definition: libtime++.h:291