]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
lib: add timespec_string_buf()
authorRalph Boehme <slow@samba.org>
Wed, 27 Nov 2019 15:31:50 +0000 (15:31 +0000)
committerJeremy Allison <jra@samba.org>
Fri, 6 Dec 2019 00:17:36 +0000 (00:17 +0000)
BUG: https://bugzilla.samba.org/show_bug.cgi?id=7771

Signed-off-by: Ralph Boehme <slow@samba.org>
Reviewed-by: Jeremy Allison <jra@samba.org>
lib/util/time.c
lib/util/time.h

index 16d89a3b7d36da71d51e82f9f07fe5a7a8404e85..b69e2e02847e2c818c584ada92addc4100020841 100644 (file)
@@ -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;
index 725be9d8cec72a00c8f7bd6011369255231d778b..77d0c08c0d8981dbf45754055166f759fab51f09 100644 (file)
@@ -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)