diff options
author | Volker Lendecke <vl@samba.org> | 2014-07-29 14:43:39 +0000 |
---|---|---|
committer | Michael Adam <obnox@samba.org> | 2014-07-31 18:49:46 +0200 |
commit | 2aa1492c7bc2fa5fe9943c27a294aa9efea6e701 (patch) | |
tree | e6264533b17c4693bae862403d03e92c17530704 /lib | |
parent | 9026820e5cadc1f42ca2d88fdb53c0c715e2f221 (diff) | |
download | samba-2aa1492c7bc2fa5fe9943c27a294aa9efea6e701.tar.gz |
lib: Add timeval_str_buf
Similarly to server_id_str_buf it does not do any allocation
Signed-off-by: Volker Lendecke <vl@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/time.c | 39 | ||||
-rw-r--r-- | lib/util/time.h | 12 |
2 files changed, 51 insertions, 0 deletions
diff --git a/lib/util/time.c b/lib/util/time.c index 03345ec8d1f..c502dc40a01 100644 --- a/lib/util/time.c +++ b/lib/util/time.c @@ -342,6 +342,45 @@ _PUBLIC_ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset) } +char *timeval_str_buf(const struct timeval *tp, bool hires, + struct timeval_buf *dst) +{ + time_t t; + struct tm *tm; + size_t len; + + t = (time_t)tp->tv_sec; + tm = localtime(&t); + + if (tm == NULL) { + if (hires) { + snprintf(dst->buf, sizeof(dst->buf), + "%ld.%06ld seconds since the Epoch", + (long)tp->tv_sec, (long)tp->tv_usec); + } else { + snprintf(dst->buf, sizeof(dst->buf), + "%ld seconds since the Epoch", (long)t); + } + 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 + if (hires && (len < sizeof(dst->buf))) { + snprintf(dst->buf + len, sizeof(dst->buf) - len, + ".%06ld", (long)tp->tv_usec); + } + + return dst->buf; +} + /**************************************************************************** Return the date and time as a string ****************************************************************************/ diff --git a/lib/util/time.h b/lib/util/time.h index 8595bb06137..44d4f455686 100644 --- a/lib/util/time.h +++ b/lib/util/time.h @@ -119,6 +119,18 @@ time_t pull_dos_date2(const uint8_t *date_ptr, int zone_offset); **/ time_t pull_dos_date3(const uint8_t *date_ptr, int zone_offset); +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 +**/ + +char *timeval_str_buf(const struct timeval *tp, bool hires, + struct timeval_buf *dst); + /** Return a date and time as a string (optionally with microseconds) |