From 1c2ae58509a19fb4590f4c71277c0f0c0a27f2fd Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Wed, 8 Oct 2014 15:52:37 +1100 Subject: [PATCH] lib/util: Use snprintf() instead of strftime() in timeval_str_buf() This removes conditional code and ensures that the output is always as expected. Signed-off-by: Martin Schwenke Pair-programmed-with: Amitay Isaacs Reviewed-by: Volker Lendecke --- lib/util/time_basic.c | 14 +++++--------- lib/util/time_basic.h | 2 +- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/util/time_basic.c b/lib/util/time_basic.c index b6e73171e4a..13d0bfdb5e6 100644 --- a/lib/util/time_basic.c +++ b/lib/util/time_basic.c @@ -62,15 +62,11 @@ char *timeval_str_buf(const struct timeval *tp, bool hires, return dst->buf; } -#ifdef HAVE_STRFTIME - len = strftime(dst->buf, sizeof(dst->buf), "%Y/%m/%d %H:%M:%S", tm); -#else - { - const char *asct = asctime(tm); - len = strlcpy(dst->buf, sizeof(dst->buf), - asct ? asct : "unknown"); - } -#endif + len = snprintf(dst->buf, sizeof(dst->buf), + "%04d/%02d/%02d %02d:%02d:%02d", + 1900 + tm->tm_year, tm->tm_mon, tm->tm_mday, + tm->tm_hour, tm->tm_min, tm->tm_sec); + if (hires && (len < sizeof(dst->buf))) { snprintf(dst->buf + len, sizeof(dst->buf) - len, ".%06ld", (long)tp->tv_usec); diff --git a/lib/util/time_basic.h b/lib/util/time_basic.h index 58bc02de3ca..b04dead1009 100644 --- a/lib/util/time_basic.h +++ b/lib/util/time_basic.h @@ -35,7 +35,7 @@ struct timeval_buf { char buf[128]; }; Put a date and time into dst->buf, return it dst->buf (optionally with microseconds) - format is %Y/%m/%d %H:%M:%S if strftime is available + format is %Y/%m/%d %H:%M:%S **/ char *timeval_str_buf(const struct timeval *tp, bool hires, -- 2.11.4.GIT