daandemeyer / rpms / systemd

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