X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Ftime-util.c;h=13d57ba5cd70ecaa595065406bfb16ff98f2cbd6;hb=f791c684a3d248baa83a3167086826ba6238d7f5;hp=3d00b20eec84069d75a2168542ff8d4fb1448a55;hpb=bbb8486e1709ee297a1eb803f88041e1da1fa436;p=elogind.git diff --git a/src/shared/time-util.c b/src/shared/time-util.c index 3d00b20ee..13d57ba5c 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -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;