From bbdc11842d324005567bac5eafdea5fd0fc62688 Mon Sep 17 00:00:00 2001 From: Victor Julien Date: Sat, 23 May 2020 20:57:21 +0200 Subject: [PATCH] windows: fix timestring timezone display Bug: #3690 --- src/util-time.c | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/src/util-time.c b/src/util-time.c index 71b9ca12c3..4509625bc2 100644 --- a/src/util-time.c +++ b/src/util-time.c @@ -1,4 +1,4 @@ -/* Copyright (C) 2007-2016 Open Information Security Foundation +/* Copyright (C) 2007-2020 Open Information Security Foundation * * You can copy, redistribute or modify this Program under the terms of * the GNU General Public License version 2 as published by the Free @@ -191,17 +191,40 @@ void TimeSetIncrementTime(uint32_t tv_sec) } #endif +#ifdef OS_WIN32 +/** \internal + * \brief wrapper around strftime on Windows to provide output + * compatible with posix %z + */ +static inline void WinStrftime(const struct timeval *ts, const struct tm *t, char *str, size_t size) +{ + char time_fmt[64] = { 0 }; + char tz[6] = { 0 }; + const long int tzdiff = -_timezone; + const int h = abs(_timezone) / 3600 + _daylight; + const int m = (abs(_timezone) % 3600) / 60; + snprintf(tz, sizeof(tz), "%c%02d%02d", tzdiff < 0 ? '-' : '+', h, m); + strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S.%%06u", t); + snprintf(str, size, time_fmt, ts->tv_usec); + strlcat(str, tz, size); // append our timezone +} +#endif + void CreateIsoTimeString (const struct timeval *ts, char *str, size_t size) { time_t time = ts->tv_sec; struct tm local_tm; memset(&local_tm, 0, sizeof(local_tm)); struct tm *t = (struct tm*)SCLocalTime(time, &local_tm); - char time_fmt[64] = { 0 }; if (likely(t != NULL)) { +#ifdef OS_WIN32 + WinStrftime(ts, t, str, size); +#else + char time_fmt[64] = { 0 }; strftime(time_fmt, sizeof(time_fmt), "%Y-%m-%dT%H:%M:%S.%%06u%z", t); snprintf(str, size, time_fmt, ts->tv_usec); +#endif } else { snprintf(str, size, "ts-error"); } -- 2.47.2