]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libcommon: add ISO_8601_GMTIME that will print UTC-0 timestamps
authorSami Kerola <kerolasa@iki.fi>
Sun, 19 Jun 2016 20:43:40 +0000 (21:43 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 26 Jun 2016 20:58:18 +0000 (21:58 +0100)
When timestamps are intented to be conversable back from string to binary it
is best to stick with UTC-0 timezone.  This is needed in utmpdump(1).

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
include/timeutils.h
lib/timeutils.c

index 265577f5e21761a1eab6576d321a6a8fdc42853d..00d18200db9a0fde03bf2a325a04c41f6d1f8aa5 100644 (file)
@@ -61,7 +61,8 @@ enum {
        ISO_8601_DOTUSEC        = (1 << 3),
        ISO_8601_COMMAUSEC      = (1 << 4),
        ISO_8601_TIMEZONE       = (1 << 5),
-       ISO_8601_SPACE          = (1 << 6)
+       ISO_8601_SPACE          = (1 << 6),
+       ISO_8601_GMTIME         = (1 << 7)
 };
 
 #define ISO_8601_BUFSIZ        32
index 25a163e41ffcc47f945d0c521e3884b04df932d8..fd9aa3e2e3b2d3465a7bd228c072ccb10e98249d 100644 (file)
@@ -396,7 +396,12 @@ static int format_iso_time(struct tm *tm, suseconds_t usec, int flags, char *buf
 /* timeval to ISO 8601 */
 int strtimeval_iso(struct timeval *tv, int flags, char *buf, size_t bufsz)
 {
-       struct tm tm = *localtime(&tv->tv_sec);
+       struct tm tm;
+
+       if (flags & ISO_8601_GMTIME)
+               tm = *gmtime(&tv->tv_sec);
+       else
+               tm = *localtime(&tv->tv_sec);
        return format_iso_time(&tm, tv->tv_usec, flags, buf, bufsz);
 }
 
@@ -409,7 +414,12 @@ int strtm_iso(struct tm *tm, int flags, char *buf, size_t bufsz)
 /* time_t to ISO 8601 */
 int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz)
 {
-       struct tm tm = *localtime(t);
+       struct tm tm;
+
+       if (flags & ISO_8601_GMTIME)
+               tm = *gmtime(t);
+       else
+               tm = *localtime(t);
        return format_iso_time(&tm, 0, flags, buf, bufsz);
 }