add thread-safe variant of timestring()

This commit is contained in:
ac-stef
2016-09-04 13:45:14 +02:00
parent 9852b98bf3
commit c7c02521ad
2 changed files with 9 additions and 7 deletions
+6 -6
View File
@@ -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()
+3 -1
View File
@@ -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);