From 84424d3829727d4895827287ef947de5bf015d4f Mon Sep 17 00:00:00 2001 From: Mrunal Patel Date: Tue, 25 Apr 2017 14:10:36 -0700 Subject: [PATCH] Add nanoseconds to timestamp to make it RFC3339Nano Signed-off-by: Mrunal Patel --- conmon/conmon.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/conmon/conmon.c b/conmon/conmon.c index 8e9f56d4..933ac4a2 100644 --- a/conmon/conmon.c +++ b/conmon/conmon.c @@ -93,28 +93,38 @@ static GOptionEntry entries[] = { NULL } }; -/* strlen("1997-03-25T13:20:42+01:00") + 1 */ -#define TSBUFLEN 26 +/* strlen("1997-03-25T13:20:42.999999999+01:00") + 1 */ +#define TSBUFLEN 36 int set_k8s_timestamp(char *buf, ssize_t buflen) { - time_t now = time(NULL); struct tm *tm; + struct timespec ts; char off_sign = '+'; int off, len, err = -1; - if ((tm = localtime(&now)) == NULL) + if (clock_gettime(CLOCK_REALTIME, &ts) < 0) { + /* If CLOCK_REALTIME is not supported, we set nano seconds to 0 */ + if (errno == EINVAL) { + ts.tv_nsec = 0; + } else { + return err; + } + } + + if ((tm = localtime(&ts.tv_sec)) == NULL) return err; + off = (int) tm->tm_gmtoff; if (tm->tm_gmtoff < 0) { off_sign = '-'; off = -off; } - len = snprintf(buf, buflen, "%d-%02d-%02dT%02d:%02d:%02d%c%02d:%02d", + len = snprintf(buf, buflen, "%d-%02d-%02dT%02d:%02d:%02d.%09ld%c%02d:%02d", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, - tm->tm_hour, tm->tm_min, tm->tm_sec, + tm->tm_hour, tm->tm_min, tm->tm_sec, ts.tv_nsec, off_sign, off / 3600, off % 3600); if (len < buflen)