From 928694af7e79edd0a48e43bb48af6fab9d32da89 Mon Sep 17 00:00:00 2001 From: Ralph Boehme Date: Wed, 27 Nov 2019 15:31:50 +0000 Subject: [PATCH] lib: add timespec_string_buf() BUG: https://bugzilla.samba.org/show_bug.cgi?id=7771 Signed-off-by: Ralph Boehme Reviewed-by: Jeremy Allison --- lib/util/time.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ lib/util/time.h | 5 +++++ 2 files changed, 61 insertions(+) diff --git a/lib/util/time.c b/lib/util/time.c index 16d89a3b7d3..b69e2e02847 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -359,6 +359,62 @@ char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires) return result; } +/**************************************************************************** + Return the date and time as a string +****************************************************************************/ + +const char *timespec_string_buf(const struct timespec *tp, + bool hires, + struct timeval_buf *buf) +{ + time_t t; + struct tm *tm = NULL; + size_t len; + + if (is_omit_timespec(tp)) { + strlcpy(buf->buf, "SAMBA_UTIME_OMIT", sizeof(buf->buf)); + return buf->buf; + } + + t = (time_t)tp->tv_sec; + tm = localtime(&t); + + if (tm == NULL) { + if (hires) { + len = snprintf(buf->buf, sizeof(buf->buf), + "%ld.%09ld seconds since the Epoch", + (long)tp->tv_sec, (long)tp->tv_nsec); + } else { + len = snprintf(buf->buf, sizeof(buf->buf), + "%ld seconds since the Epoch", (long)t); + } + } else if (!hires) { + len = snprintf(buf->buf, sizeof(buf->buf), + "%04d/%02d/%02d %02d:%02d:%02d", + 1900 + tm->tm_year, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec); + } else { + len = snprintf(buf->buf, sizeof(buf->buf), + "%04d/%02d/%02d %02d:%02d:%02d.%09ld", + 1900 + tm->tm_year, + tm->tm_mon + 1, + tm->tm_mday, + tm->tm_hour, + tm->tm_min, + tm->tm_sec, + (long)tp->tv_nsec); + } + if (len == -1) { + return ""; + } + + return buf->buf; +} + char *current_timestring(TALLOC_CTX *ctx, bool hires) { struct timeval tv; diff --git a/lib/util/time.h b/lib/util/time.h index 725be9d8cec..77d0c08c0d8 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -133,6 +133,11 @@ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset); char *timeval_string(TALLOC_CTX *ctx, const struct timeval *tp, bool hires); +struct timeval_buf; +const char *timespec_string_buf(const struct timespec *tp, + bool hires, + struct timeval_buf *buf); + /** Return the current date and time as a string (optionally with microseconds) -- 2.47.3