chiark / gitweb /
macro: use C11 static_assert() macro for static assertions
[elogind.git] / src / shared / time-util.c
index 3323683339f58a3c4fb328256ba16c9816ebf026..13d57ba5cd70ecaa595065406bfb16ff98f2cbd6 100644 (file)
@@ -142,7 +142,7 @@ char *format_timestamp(char *buf, size_t l, usec_t t) {
         return buf;
 }
 
-char *format_timestamp_pretty(char *buf, size_t l, usec_t t) {
+char *format_timestamp_relative(char *buf, size_t l, usec_t t) {
         usec_t n, d;
 
         n = now(CLOCK_REALTIME);
@@ -284,11 +284,32 @@ void dual_timestamp_deserialize(const char *value, dual_timestamp *t) {
 }
 
 int parse_timestamp(const char *t, usec_t *usec) {
+        static const struct {
+                const char *name;
+                const int nr;
+        } day_nr[] = {
+                { "Sunday",    0 },
+                { "Sun",       0 },
+                { "Monday",    1 },
+                { "Mon",       1 },
+                { "Tuesday",   2 },
+                { "Tue",       2 },
+                { "Wednesday", 3 },
+                { "Wed",       3 },
+                { "Thursday",  4 },
+                { "Thu",       4 },
+                { "Friday",    5 },
+                { "Fri",       5 },
+                { "Saturday",  6 },
+                { "Sat",       6 },
+        };
+
         const char *k;
         struct tm tm, copy;
         time_t x;
         usec_t plus = 0, minus = 0, ret;
-        int r;
+        int r, weekday = -1;
+        unsigned i;
 
         /*
          * Allowed syntaxes:
@@ -312,6 +333,7 @@ int parse_timestamp(const char *t, usec_t *usec) {
 
         x = time(NULL);
         assert_se(localtime_r(&x, &tm));
+        tm.tm_isdst = -1;
 
         if (streq(t, "now"))
                 goto finish;
@@ -344,6 +366,34 @@ int parse_timestamp(const char *t, usec_t *usec) {
                         return r;
 
                 goto finish;
+
+        } else if (endswith(t, " ago")) {
+                _cleanup_free_ char *z;
+
+                z = strndup(t, strlen(t) - 4);
+                if (!z)
+                        return -ENOMEM;
+
+                r = parse_usec(z, &minus);
+                if (r < 0)
+                        return r;
+
+                goto finish;
+        }
+
+        for (i = 0; i < ELEMENTSOF(day_nr); i++) {
+                size_t skip;
+
+                if (!startswith_no_case(t, day_nr[i].name))
+                        continue;
+
+                skip = strlen(day_nr[i].name);
+                if (t[skip] != ' ')
+                        continue;
+
+                weekday = day_nr[i].nr;
+                t += skip + 1;
+                break;
         }
 
         copy = tm;
@@ -403,6 +453,9 @@ finish:
         if (x == (time_t) -1)
                 return -EINVAL;
 
+        if (weekday >= 0 && tm.tm_wday != weekday)
+                return -EINVAL;
+
         ret = (usec_t) x * USEC_PER_SEC;
 
         ret += plus;