removed heap dependency in datetime() and simplified the function

This commit is contained in:
Ali H. Fardan 2016-08-28 16:39:04 +03:00
parent 25eb9ff35e
commit 1d257999ed

View file

@ -163,27 +163,14 @@ cpu_perc(void)
static char * static char *
datetime(const char *timeformat) datetime(const char *timeformat)
{ {
time_t tm; time_t t;
size_t bufsize = 64; char timestr[80];
char *buf = malloc(bufsize);
if (buf == NULL) {
fprintf(stderr, "Failed to get date/time.\n");
return smprintf(UNKNOWN_STR);
}
time(&tm); t = time(NULL);
setlocale(LC_TIME, ""); if (strftime(timestr, sizeof(timestr), timeformat, localtime(&t)) == 0)
if (!strftime(buf, bufsize, timeformat, localtime(&tm))) {
setlocale(LC_TIME, "C");
free(buf);
fprintf(stderr, "Strftime failed.\n");
return smprintf(UNKNOWN_STR); return smprintf(UNKNOWN_STR);
}
setlocale(LC_TIME, "C"); return smprintf("%s", timestr);
char *ret = smprintf("%s", buf);
free(buf);
return ret;
} }
static char * static char *