X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Ftime-util.c;h=e192d5ef58d712302231575fce57808f7176ef5e;hb=9f26c90cb50c45d4549c4dd569917b4ac143b94d;hp=7f477100b3ddebe6640fb65373f088529e1805be;hpb=decad9103eab4c5f24dbc55dc7d2cdabce87a302;p=elogind.git diff --git a/src/shared/time-util.c b/src/shared/time-util.c index 7f477100b..e192d5ef5 100644 --- a/src/shared/time-util.c +++ b/src/shared/time-util.c @@ -46,6 +46,11 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { int64_t delta; assert(ts); + if (u == (usec_t) -1) { + ts->realtime = ts->monotonic = (usec_t) -1; + return ts; + } + ts->realtime = u; if (u == 0) @@ -64,6 +69,27 @@ dual_timestamp* dual_timestamp_from_realtime(dual_timestamp *ts, usec_t u) { return ts; } +dual_timestamp* dual_timestamp_from_monotonic(dual_timestamp *ts, usec_t u) { + int64_t delta; + assert(ts); + + if (u == (usec_t) -1) { + ts->realtime = ts->monotonic = (usec_t) -1; + return ts; + } + + ts->monotonic = u; + delta = (int64_t) now(CLOCK_MONOTONIC) - (int64_t) u; + + ts->realtime = now(CLOCK_REALTIME); + if ((int64_t) ts->realtime > delta) + ts->realtime -= delta; + else + ts->realtime = 0; + + return ts; +} + usec_t timespec_load(const struct timespec *ts) { assert(ts); @@ -204,6 +230,8 @@ char *format_timespan(char *buf, size_t l, usec_t t) { const char *suffix; usec_t usec; } table[] = { + { "y", USEC_PER_YEAR }, + { "month", USEC_PER_MONTH }, { "w", USEC_PER_WEEK }, { "d", USEC_PER_DAY }, { "h", USEC_PER_HOUR }, @@ -284,11 +312,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 +361,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; @@ -359,6 +409,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) @@ -416,6 +481,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;