]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lsclocks: add relative time
authorThomas Weißschuh <thomas@t-8ch.de>
Fri, 30 Jun 2023 12:58:42 +0000 (14:58 +0200)
committerThomas Weißschuh <thomas@t-8ch.de>
Tue, 4 Jul 2023 12:15:20 +0000 (14:15 +0200)
Signed-off-by: Thomas Weißschuh <thomas@t-8ch.de>
misc-utils/lsclocks.1.adoc
misc-utils/lsclocks.c

index 1eb8b952a79a0fc05f94bcdfc7ce2366586a2f58..38d665f727571d634e6bb0e116c34b770be8bee8 100644 (file)
@@ -66,6 +66,9 @@ ISO8601 formatted version of *TIME*.
 RESOLUTION <``number``>::
 Clock resolution as returned by *clock_getres()*.
 
+REL_TIME <``string``>::
+*TIME* time formatted as time range.
+
 
 == AUTHORS
 
index 304ae8ae3f19c126ca670cafad7b88a7ba792846..4ca07af15a6911977fb64e7a549964b0c5f121d5 100644 (file)
@@ -98,6 +98,7 @@ enum {
        COL_TIME,
        COL_ISO_TIME,
        COL_RESOLUTION,
+       COL_REL_TIME,
 };
 
 /* column names */
@@ -117,6 +118,7 @@ static const struct colinfo infos[] = {
        [COL_TIME]       = { "TIME",       1, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("numeric time") },
        [COL_ISO_TIME]   = { "ISO_TIME",   1, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("human readable ISO time") },
        [COL_RESOLUTION] = { "RESOLUTION", 1, SCOLS_FL_RIGHT, SCOLS_JSON_NUMBER, N_("resolution") },
+       [COL_REL_TIME]   = { "REL_TIME",   1, SCOLS_FL_RIGHT, SCOLS_JSON_STRING, N_("human readable relative time") },
 };
 
 static int column_name_to_id(const char *name, size_t namesz)
@@ -224,7 +226,7 @@ int main(int argc, char **argv)
        clockid_t clock = -1;
 
        struct timespec resolution, now;
-       char buf[FORMAT_TIMESTAMP_MAX];
+       char buf[BUFSIZ];
 
        enum {
                OPT_OUTPUT_ALL = CHAR_MAX + 1
@@ -361,6 +363,14 @@ int main(int argc, char **argv)
                                        if (!rc)
                                                scols_line_format_timespec(ln, j, &resolution);
                                        break;
+                               case COL_REL_TIME:
+                                       if (now.tv_nsec == -1)
+                                               break;
+                                       rc = strtimespec_relative(&now, buf, sizeof(buf));
+                                       if (rc)
+                                               errx(EXIT_FAILURE, _("failed to format relative time"));
+                                       scols_line_set_data(ln, j, buf);
+                                       break;
                        }
                }
        }