Calculate timestamp lengths only once

The lengths will never change.
This commit is contained in:
Markus Mäkelä
2018-08-15 23:39:56 +03:00
parent 6aff79e634
commit f82c965bfb

View File

@ -360,7 +360,7 @@ std::string get_timestamp(void)
struct tm tm; struct tm tm;
localtime_r(&t, &tm); localtime_r(&t, &tm);
const char* timestamp_formatstr = "%04d-%02d-%02d %02d:%02d:%02d "; const char* timestamp_formatstr = "%04d-%02d-%02d %02d:%02d:%02d ";
int required = snprintf(NULL, 0, timestamp_formatstr, static int required = snprintf(NULL, 0, timestamp_formatstr,
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour,
tm.tm_min, tm.tm_sec); tm.tm_min, tm.tm_sec);
char buf[required + 1]; char buf[required + 1];
@ -381,7 +381,7 @@ std::string get_timestamp_hp(void)
int usec = tv.tv_usec / 1000; int usec = tv.tv_usec / 1000;
const char* timestamp_formatstr_hp = "%04d-%02d-%02d %02d:%02d:%02d.%03d "; const char* timestamp_formatstr_hp = "%04d-%02d-%02d %02d:%02d:%02d.%03d ";
int required = snprintf(NULL, 0, timestamp_formatstr_hp, static int required = snprintf(NULL, 0, timestamp_formatstr_hp,
tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec, usec); tm.tm_hour, tm.tm_min, tm.tm_sec, usec);