]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/timeutils: Require '@' prefix for seconds since the Epoch timestamp
authorPeter Ujfalusi <peter.ujfalusi@linux.intel.com>
Wed, 7 Sep 2022 05:41:41 +0000 (08:41 +0300)
committerKarel Zak <kzak@redhat.com>
Mon, 12 Sep 2022 12:22:24 +0000 (14:22 +0200)
Since the seconds since the Epoch is just a number it might be better to
require special prefix to indicate the intention that the user wants to
interpret the number as seconds since the Epoch.

Use the same '@' character as prefix as used by systemd.time to make it
easier to integrate in scripts intended to be used on systems with or
without systemd.

Fix also the initial support which discarded the seconds from the converted
timestamp.

Signed-off-by: Peter Ujfalusi <peter.ujfalusi@linux.intel.com>
lib/timeutils.c

index 6dda2e8deefbea3d0c2bf956a9c5dc6f2ae17879..4eaef9533ab2696c766f0477d7be3362d3ba0f66 100644 (file)
@@ -181,7 +181,7 @@ int parse_timestamp(const char *t, usec_t *usec)
         *
         *   2012-09-22 16:34:22
         *   2012-09-22T16:34:22
-        *   1348331662           (seconds since the Epoch (1970-01-01 00:00 UTC))
+        *   @1348331662          (seconds since the Epoch (1970-01-01 00:00 UTC))
         *   2012-09-22 16:34     (seconds will be set to 0)
         *   2012-09-22           (time will be set to 00:00:00)
         *   16:34:22             (date will be set to today)
@@ -233,7 +233,12 @@ int parse_timestamp(const char *t, usec_t *usec)
                        return r;
 
                goto finish;
+       } else if (t[0] == '@') {
+               k = strptime(t + 1, "%s", &tm);
+               if (k && *k == 0)
+                       goto finish;
 
+               return -EINVAL;
        } else if (endswith(t, " ago")) {
                char *z;
 
@@ -326,13 +331,6 @@ int parse_timestamp(const char *t, usec_t *usec)
                goto finish;
        }
 
-       tm = copy;
-       k = strptime(t, "%s", &tm);
-       if (k && *k == 0) {
-               tm.tm_sec = 0;
-               goto finish;
-       }
-
        return -EINVAL;
 
  finish: