summaryrefslogtreecommitdiff
path: root/src/shared/time-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-11-23 22:52:46 +0100
committerLennart Poettering <lennart@poettering.net>2012-11-23 22:52:46 +0100
commit92134489ab35f6d173cb6fb6f676c1b5d88f5556 (patch)
tree532583db91c1bdb2e24075110f6b4f3d3bda1742 /src/shared/time-util.c
parent6a741b4a2b395fd186d73074bd6986016c501216 (diff)
time-util: teach parse_timestamp to parse weekdays
Diffstat (limited to 'src/shared/time-util.c')
-rw-r--r--src/shared/time-util.c41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/shared/time-util.c b/src/shared/time-util.c
index 43448e0c63..13d57ba5cd 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:
@@ -360,6 +381,21 @@ int parse_timestamp(const char *t, usec_t *usec) {
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;
k = strptime(t, "%y-%m-%d %H:%M:%S", &tm);
if (k && *k == 0)
@@ -417,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;