Add nanoseconds to timestamp to make it RFC3339Nano

Signed-off-by: Mrunal Patel <mpatel@redhat.com>
This commit is contained in:
Mrunal Patel 2017-04-25 14:10:36 -07:00
parent d25b88583f
commit 84424d3829

View file

@ -93,18 +93,28 @@ 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) {
@ -112,9 +122,9 @@ int set_k8s_timestamp(char *buf, ssize_t buflen)
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)