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