Calculate sizes of strftime buffers based on format strings

- Leverage std::size to determine buffer size at compile time.
- Simplified 'TimeMon::evaluate' implementation as it was using strftime
  to get the month, convert the string to int, and then decrement it by
  one to make it zero based. This same value is already available in
  the 'struct tm' previously generated with the call to localtime_r (and
  where the month is already zero-based)
This commit is contained in:
Eduardo Arias
2024-08-12 08:47:04 -07:00
parent 5e6fcbc60b
commit 23a341eb6a
11 changed files with 50 additions and 66 deletions

View File

@@ -51,28 +51,27 @@ Parallel::~Parallel() {
inline std::string Parallel::logFilePath(time_t *t,
int part) {
struct tm timeinfo;
char tstr[300];
std::string name("");
std::string name;
struct tm timeinfo;
localtime_r(t, &timeinfo);
if (part & YearMonthDayDirectory) {
memset(tstr, '\0', 300);
strftime(tstr, 299, "/%Y%m%d", &timeinfo);
name = tstr;
char tstr[std::size("/yyyymmdd")];
strftime(tstr, std::size(tstr), "/%Y%m%d", &timeinfo);
name.append(tstr);
}
if (part & YearMonthDayAndTimeDirectory) {
memset(tstr, '\0', 300);
strftime(tstr, 299, "/%Y%m%d-%H%M", &timeinfo);
name = name + tstr;
char tstr[std::size("/yyyymmdd-hhmm")];
strftime(tstr, std::size(tstr), "/%Y%m%d-%H%M", &timeinfo);
name.append(tstr);
}
if (part & YearMonthDayAndTimeFileName) {
memset(tstr, '\0', 300);
strftime(tstr, 299, "/%Y%m%d-%H%M%S", &timeinfo);
name = name + tstr;
char tstr[std::size("/yyyymmdd-hhmmss")];
strftime(tstr, std::size(tstr), "/%Y%m%d-%H%M%S", &timeinfo);
name.append(tstr);
}
return name;