From 95151e3f0c2a9935abe6e498fd056a57075ea0c5 Mon Sep 17 00:00:00 2001 From: Lars Henriksen Date: Wed, 7 Oct 2020 19:51:18 +0200 Subject: Add p(revious) command The p(rev) command finds the previous occurrence of a recurrent item, analogous to the n(ext) command. A bug in the next command is corrected.. Signed-off-by: Lars Henriksen Signed-off-by: Lukas Fleischer --- src/recur.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/recur.c') diff --git a/src/recur.c b/src/recur.c index 3c93de2..3a16d1e 100644 --- a/src/recur.c +++ b/src/recur.c @@ -1846,3 +1846,27 @@ int recur_nth_occurrence(time_t s, long d, struct rpt *r, llist_t *e, int n, } return !n; } + +/* + * Finds the previous occurrence - the most recent before day - and returns it + * in the provided buffer. + */ +int recur_prev_occurrence(time_t s, long d, struct rpt *r, llist_t *e, + time_t day, time_t *prev) +{ + time_t prev_day, next; + + if (day <= update_time_in_date(s, 0, 0)) + return 0; + next = *prev = s; + while (update_time_in_date(next, 0, 0) < day) { + /* Set new previous and next. */ + *prev = next; + prev_day = update_time_in_date(*prev, 0, 0); + recur_next_occurrence(s, d, r, e, prev_day, &next); + /* Multi-day appointment */ + if (next == *prev) + next = NEXTDAY(*prev); + } + return 1; +} -- cgit v1.2.3-54-g00ecf