mirror of
https://github.com/samba-team/samba.git
synced 2025-03-22 02:50:28 +03:00
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>
This commit is contained in:
parent
9026820e5c
commit
2aa1492c7b
@ -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
|
||||
****************************************************************************/
|
||||
|
@ -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)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user