From 0056086af6bea38f0a485972c79428b1b3ab7b2a Mon Sep 17 00:00:00 2001 From: Alexander Kuleshov Date: Sun, 7 Feb 2016 22:11:46 +0600 Subject: time-util: merge format_timestamp_internal() and format_timestamp_internal_us() The time_util.c provides format_timestamp_internal() and format_timestamp_internal_us() functions for a timestamp formating. Both functions are very similar and differ only in formats handling. We can add additional boolean parameter to the format_timestamp_internal() function which will represent is a format for us timestamp or not. This allows us to get rid of format_timestamp_internal_us() that is prevent code duplication. We can remove format_timestamp_internal_us() safely, because it is static and has no users outside of the time_util.c. New fourth parameter will be passed inside of the format_timestamp(), format_timestamp_us() and etc, functions, but the public API is not changed. --- src/basic/time-util.c | 46 ++++++++++++++++++---------------------------- 1 file changed, 18 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/basic/time-util.c b/src/basic/time-util.c index 293442cf0e..8c42df9b9f 100644 --- a/src/basic/time-util.c +++ b/src/basic/time-util.c @@ -206,9 +206,11 @@ struct timeval *timeval_store(struct timeval *tv, usec_t u) { return tv; } -static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc) { +static char *format_timestamp_internal(char *buf, size_t l, usec_t t, + bool utc, bool us) { struct tm tm; time_t sec; + int k; assert(buf); assert(l > 0); @@ -219,48 +221,36 @@ static char *format_timestamp_internal(char *buf, size_t l, usec_t t, bool utc) sec = (time_t) (t / USEC_PER_SEC); localtime_or_gmtime_r(&sec, &tm, utc); - if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm) <= 0) + if (us) + k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm); + else + k = strftime(buf, l, "%a %Y-%m-%d %H:%M:%S %Z", &tm); + + if (k <= 0) return NULL; + if (us) { + snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC)); + if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0) + return NULL; + } return buf; } char *format_timestamp(char *buf, size_t l, usec_t t) { - return format_timestamp_internal(buf, l, t, false); + return format_timestamp_internal(buf, l, t, false, false); } char *format_timestamp_utc(char *buf, size_t l, usec_t t) { - return format_timestamp_internal(buf, l, t, true); -} - -static char *format_timestamp_internal_us(char *buf, size_t l, usec_t t, bool utc) { - struct tm tm; - time_t sec; - - assert(buf); - assert(l > 0); - - if (t <= 0 || t == USEC_INFINITY) - return NULL; - - sec = (time_t) (t / USEC_PER_SEC); - localtime_or_gmtime_r(&sec, &tm, utc); - - if (strftime(buf, l, "%a %Y-%m-%d %H:%M:%S", &tm) <= 0) - return NULL; - snprintf(buf + strlen(buf), l - strlen(buf), ".%06llu", (unsigned long long) (t % USEC_PER_SEC)); - if (strftime(buf + strlen(buf), l - strlen(buf), " %Z", &tm) <= 0) - return NULL; - - return buf; + return format_timestamp_internal(buf, l, t, true, false); } char *format_timestamp_us(char *buf, size_t l, usec_t t) { - return format_timestamp_internal_us(buf, l, t, false); + return format_timestamp_internal(buf, l, t, false, true); } char *format_timestamp_us_utc(char *buf, size_t l, usec_t t) { - return format_timestamp_internal_us(buf, l, t, true); + return format_timestamp_internal(buf, l, t, true, true); } char *format_timestamp_relative(char *buf, size_t l, usec_t t) { -- cgit v1.2.3-54-g00ecf