ryantimwilson / rpms / systemd

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