diff options
| author | Thomas Weißschuh <thomas@t-8ch.de> | 2023-06-27 08:57:09 +0200 |
|---|---|---|
| committer | Thomas Weißschuh <thomas@t-8ch.de> | 2023-06-28 14:58:32 +0200 |
| commit | b9abaae31272d984d16a28edb0bcd42e2fd81f5d (patch) | |
| tree | 8904649252b1845cc7432d6c3f02391d526a5b0b | |
| parent | 136f4874f417c6e115f1c8feddffb938c446512a (diff) | |
| download | util-linux-b9abaae31272d984d16a28edb0bcd42e2fd81f5d.tar.gz | |
lib/timeutils: implement timespec formatting
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
| -rw-r--r-- | include/timeutils.h | 1 | ||||
| -rw-r--r-- | lib/timeutils.c | 23 |
2 files changed, 18 insertions, 6 deletions
diff --git a/include/timeutils.h b/include/timeutils.h index f0a75a4a86..4257bd1eda 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -77,6 +77,7 @@ enum { int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz); int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz); int strtime_iso(const time_t *t, int flags, char *buf, size_t bufsz); +int strtimespec_iso(const struct timespec *t, int flags, char *buf, size_t bufsz); #define UL_SHORTTIME_THISYEAR_HHMM (1 << 1) diff --git a/lib/timeutils.c b/lib/timeutils.c index 7cf0f76521..75126c0bd2 100644 --- a/lib/timeutils.c +++ b/lib/timeutils.c @@ -522,24 +522,35 @@ static int format_iso_time(const struct tm *tm, uint32_t nsec, int flags, char * return -1; } -/* timeval to ISO 8601 */ -int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz) +/* timespec to ISO 8601 */ +int strtimespec_iso(const struct timespec *ts, int flags, char *buf, size_t bufsz) { struct tm tm; struct tm *rc; if (flags & ISO_GMTIME) - rc = gmtime_r(&tv->tv_sec, &tm); + rc = gmtime_r(&ts->tv_sec, &tm); else - rc = localtime_r(&tv->tv_sec, &tm); + rc = localtime_r(&ts->tv_sec, &tm); if (rc) - return format_iso_time(&tm, tv->tv_usec * NSEC_PER_USEC, flags, buf, bufsz); + return format_iso_time(&tm, ts->tv_nsec, flags, buf, bufsz); - warnx(_("time %"PRId64" is out of range."), (int64_t)(tv->tv_sec)); + warnx(_("time %"PRId64" is out of range."), (int64_t)(ts->tv_sec)); return -1; } +/* timeval to ISO 8601 */ +int strtimeval_iso(const struct timeval *tv, int flags, char *buf, size_t bufsz) +{ + struct timespec ts = { + .tv_sec = tv->tv_sec, + .tv_nsec = tv->tv_usec * NSEC_PER_USEC, + }; + + return strtimespec_iso(&ts, flags, buf, bufsz); +} + /* struct tm to ISO 8601 */ int strtm_iso(const struct tm *tm, int flags, char *buf, size_t bufsz) { |
