From c7c02521ade09fbec64f86776bf0fde52b1c86e2 Mon Sep 17 00:00:00 2001 From: ac-stef Date: Mon, 21 Mar 2016 20:48:34 +0100 Subject: [PATCH] add thread-safe variant of timestring() --- source/src/tools.cpp | 12 ++++++------ source/src/tools.h | 4 +++- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/source/src/tools.cpp b/source/src/tools.cpp index b6c69ba0..3f64fdae 100644 --- a/source/src/tools.cpp +++ b/source/src/tools.cpp @@ -15,19 +15,19 @@ char *strtok_r(char *s, const char *delim, char **b) } #endif -const char *timestring(bool local, const char *fmt) +string _timestringbuffer; + +const char *timestring(time_t t, bool local, const char *fmt, char *buf) { - static string asciitime; - time_t t = time(NULL); #ifdef NO_POSIX_R - struct tm * timeinfo; + struct tm *timeinfo; timeinfo = local ? localtime(&t) : gmtime (&t); #else struct tm *timeinfo, b; timeinfo = local ? localtime_r(&t, &b) : gmtime_r(&t, &b); #endif - strftime(asciitime, sizeof(string) - 1, fmt ? fmt : "%Y%m%d_%H.%M.%S", timeinfo); // sortable time for filenames - return asciitime; + strftime(buf, sizeof(string) - 1, fmt ? fmt : "%Y%m%d_%H.%M.%S", timeinfo); // sortable time for filenames + return buf; } const char *asctime() diff --git a/source/src/tools.h b/source/src/tools.h index 66bccd92..7fabb537 100644 --- a/source/src/tools.h +++ b/source/src/tools.h @@ -934,7 +934,9 @@ struct stream #endif }; -extern const char *timestring(bool local = false, const char *fmt = NULL); +extern string _timestringbuffer; +extern const char *timestring(time_t t, bool local = false, const char *fmt = NULL, char *buf = _timestringbuffer); +inline const char *timestring(bool local = false, const char *fmt = NULL, char *buf = _timestringbuffer) { return timestring(time(NULL), local, fmt, buf); } extern const char *asctime(); extern const char *numtime(); extern void transformoldentitytypes(int mapversion, uchar &enttype);