richardphibel / rpms / systemd

Forked from rpms/systemd 2 years ago
Clone
572a44
From dc7bf475eda82d02b5a0f243fee6741fab56729c Mon Sep 17 00:00:00 2001
572a44
From: Lennart Poettering <lennart@poettering.net>
572a44
Date: Mon, 11 Nov 2013 03:02:52 +0100
572a44
Subject: [PATCH] timer: properly format relative timestamps in the future
572a44
572a44
---
572a44
 src/shared/time-util.c | 69 ++++++++++++++++++++++++++++++++------------------
572a44
 1 file changed, 44 insertions(+), 25 deletions(-)
572a44
572a44
diff --git a/src/shared/time-util.c b/src/shared/time-util.c
572a44
index 860be61..b8a6bd7 100644
572a44
--- a/src/shared/time-util.c
572a44
+++ b/src/shared/time-util.c
572a44
@@ -191,55 +191,62 @@ char *format_timestamp_us(char *buf, size_t l, usec_t t) {
572a44
 }
572a44
 
572a44
 char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
572a44
+        const char *s;
572a44
         usec_t n, d;
572a44
 
572a44
         n = now(CLOCK_REALTIME);
572a44
 
572a44
-        if (t <= 0 || t > n || t + USEC_PER_DAY*7 <= t)
572a44
+        if (t <= 0 || (t == (usec_t) -1))
572a44
                 return NULL;
572a44
 
572a44
-        d = n - t;
572a44
+        if (n > t) {
572a44
+                d = n - t;
572a44
+                s = "ago";
572a44
+        } else {
572a44
+                d = t - n;
572a44
+                s = "left";
572a44
+        }
572a44
 
572a44
         if (d >= USEC_PER_YEAR)
572a44
-                snprintf(buf, l, "%llu years %llu months ago",
572a44
+                snprintf(buf, l, "%llu years %llu months %s",
572a44
                          (unsigned long long) (d / USEC_PER_YEAR),
572a44
-                         (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH));
572a44
+                         (unsigned long long) ((d % USEC_PER_YEAR) / USEC_PER_MONTH), s);
572a44
         else if (d >= USEC_PER_MONTH)
572a44
-                snprintf(buf, l, "%llu months %llu days ago",
572a44
+                snprintf(buf, l, "%llu months %llu days %s",
572a44
                          (unsigned long long) (d / USEC_PER_MONTH),
572a44
-                         (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY));
572a44
+                         (unsigned long long) ((d % USEC_PER_MONTH) / USEC_PER_DAY), s);
572a44
         else if (d >= USEC_PER_WEEK)
572a44
-                snprintf(buf, l, "%llu weeks %llu days ago",
572a44
+                snprintf(buf, l, "%llu weeks %llu days %s",
572a44
                          (unsigned long long) (d / USEC_PER_WEEK),
572a44
-                         (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY));
572a44
+                         (unsigned long long) ((d % USEC_PER_WEEK) / USEC_PER_DAY), s);
572a44
         else if (d >= 2*USEC_PER_DAY)
572a44
-                snprintf(buf, l, "%llu days ago", (unsigned long long) (d / USEC_PER_DAY));
572a44
+                snprintf(buf, l, "%llu days %s", (unsigned long long) (d / USEC_PER_DAY), s);
572a44
         else if (d >= 25*USEC_PER_HOUR)
572a44
-                snprintf(buf, l, "1 day %lluh ago",
572a44
-                         (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR));
572a44
+                snprintf(buf, l, "1 day %lluh %s",
572a44
+                         (unsigned long long) ((d - USEC_PER_DAY) / USEC_PER_HOUR), s);
572a44
         else if (d >= 6*USEC_PER_HOUR)
572a44
-                snprintf(buf, l, "%lluh ago",
572a44
-                         (unsigned long long) (d / USEC_PER_HOUR));
572a44
+                snprintf(buf, l, "%lluh %s",
572a44
+                         (unsigned long long) (d / USEC_PER_HOUR), s);
572a44
         else if (d >= USEC_PER_HOUR)
572a44
-                snprintf(buf, l, "%lluh %llumin ago",
572a44
+                snprintf(buf, l, "%lluh %llumin %s",
572a44
                          (unsigned long long) (d / USEC_PER_HOUR),
572a44
-                         (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE));
572a44
+                         (unsigned long long) ((d % USEC_PER_HOUR) / USEC_PER_MINUTE), s);
572a44
         else if (d >= 5*USEC_PER_MINUTE)
572a44
-                snprintf(buf, l, "%llumin ago",
572a44
-                         (unsigned long long) (d / USEC_PER_MINUTE));
572a44
+                snprintf(buf, l, "%llumin %s",
572a44
+                         (unsigned long long) (d / USEC_PER_MINUTE), s);
572a44
         else if (d >= USEC_PER_MINUTE)
572a44
-                snprintf(buf, l, "%llumin %llus ago",
572a44
+                snprintf(buf, l, "%llumin %llus %s",
572a44
                          (unsigned long long) (d / USEC_PER_MINUTE),
572a44
-                         (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC));
572a44
+                         (unsigned long long) ((d % USEC_PER_MINUTE) / USEC_PER_SEC), s);
572a44
         else if (d >= USEC_PER_SEC)
572a44
-                snprintf(buf, l, "%llus ago",
572a44
-                         (unsigned long long) (d / USEC_PER_SEC));
572a44
+                snprintf(buf, l, "%llus %s",
572a44
+                         (unsigned long long) (d / USEC_PER_SEC), s);
572a44
         else if (d >= USEC_PER_MSEC)
572a44
-                snprintf(buf, l, "%llums ago",
572a44
-                         (unsigned long long) (d / USEC_PER_MSEC));
572a44
+                snprintf(buf, l, "%llums %s",
572a44
+                         (unsigned long long) (d / USEC_PER_MSEC), s);
572a44
         else if (d > 0)
572a44
-                snprintf(buf, l, "%lluus ago",
572a44
-                         (unsigned long long) d);
572a44
+                snprintf(buf, l, "%lluus %s",
572a44
+                         (unsigned long long) d, s);
572a44
         else
572a44
                 snprintf(buf, l, "now");
572a44
 
572a44
@@ -479,6 +486,18 @@ int parse_timestamp(const char *t, usec_t *usec) {
572a44
                         return r;
572a44
 
572a44
                 goto finish;
572a44
+        } else if (endswith(t, " left")) {
572a44
+                _cleanup_free_ char *z;
572a44
+
572a44
+                z = strndup(t, strlen(t) - 4);
572a44
+                if (!z)
572a44
+                        return -ENOMEM;
572a44
+
572a44
+                r = parse_sec(z, &plus);
572a44
+                if (r < 0)
572a44
+                        return r;
572a44
+
572a44
+                goto finish;
572a44
         }
572a44
 
572a44
         for (i = 0; i < ELEMENTSOF(day_nr); i++) {