Skip to content

Commit

Permalink
Change leftover localtime call to _r/_s
Browse files Browse the repository at this point in the history
  • Loading branch information
zuckschwerdt committed Oct 30, 2019
1 parent 3c2e6b3 commit 0f5e21c
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/r_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,24 @@ char *format_time_str(char *buf, char const *format, time_t time_secs)
char *usecs_time_str(char *buf, char const *format, struct timeval *tv)
{
struct timeval now;
struct tm *tm_info;
struct tm tm_info;

if (!tv) {
tv = &now;
get_time_now(tv);
}

time_t t_secs = tv->tv_sec;
tm_info = localtime(&t_secs); // note: win32 doesn't have localtime_r()
#ifdef _WIN32 /* MinGW might have localtime_r but apparently not MinGW64 */
localtime_s(&tm_info, &t_secs); // win32 doesn't have localtime_r()
#else
localtime_r(&t_secs, &tm_info); // thread-safe
#endif

if (!format || !*format)
format = "%Y-%m-%d %H:%M:%S";

size_t l = strftime(buf, LOCAL_TIME_BUFLEN, format, tm_info);
size_t l = strftime(buf, LOCAL_TIME_BUFLEN, format, &tm_info);
snprintf(buf + l, LOCAL_TIME_BUFLEN - l, ".%06ld", (long)tv->tv_usec);
return buf;
}
Expand Down

0 comments on commit 0f5e21c

Please sign in to comment.