X-Git-Url: http://pileus.org/git/?a=blobdiff_plain;f=src%2Fcal.c;h=afc18f01670f5abd6a5203287e64190811d7fa6b;hb=d45541aeb31a0e34a2a43df0e112f1f720f80abf;hp=b519d588fb4601d6a33470c2b60997e9bee51591;hpb=296fd1bb5f87b1961e98c7ea4c224219012f7161;p=lackey diff --git a/src/cal.c b/src/cal.c index b519d58..afc18f0 100644 --- a/src/cal.c +++ b/src/cal.c @@ -33,9 +33,9 @@ CAL(dummy); CAL(ical); /* Global data */ -cal_t *CALS; -event_t *EVENTS; -todo_t *TODOS; +cal_t *CAL, *CALS; +event_t *EVENT, *EVENTS; +todo_t *TODO, *TODOS; /* Local data */ static date_t start; @@ -114,7 +114,7 @@ void cal_init(void) cal_load(SEL.year, SEL.month, SEL.day, 1); /* Debug */ -#ifdef DEBUG_CALS +#ifdef CAL_DEBUG for (event_t *e = EVENTS; e; e = e->next) debug("event: %04d-%02d-%02d %02d:%02d: %s - %s", e->start.year, e->start.month, e->start.day, @@ -174,7 +174,7 @@ void cal_load(year_t year, month_t month, day_t day, int days) ical_todos(start, end)); /* Verify events and todos*/ -#ifdef DEBUG_CALS +#ifdef CAL_ERROR for (event_t *cur = EVENTS; cur; cur = cur->next) { if (!cur->cal) error("Missing cal in event '%s'", cur->name); @@ -200,3 +200,33 @@ void cal_config(const char *group, const char *name, const char *key, const char else if (match(group, "ical")) ical_config(group, name, key, value); } + +/* Find event for matching target date */ +event_t *find_event(date_t *target) +{ + int min = 0; + event_t *event = NULL; + + if (EVENT && compare(&EVENT->start, target) == 0) + return EVENT; + + for (event_t *cur = EVENTS; cur; cur = cur->next) { + // Skip events that are on the wrong day + if ((target->year != cur->start.year) || + (target->month != cur->start.month) || + (target->day != cur->start.day)) + continue; + + // We don't want time change or leap seconds here + int diff = (target->hour - cur->start.hour) * 60 * 24 + + (target->min - cur->start.min) * 60 + + (target->sec - cur->start.sec); + + if (event == NULL || ABS(diff) < min) { + min = ABS(diff); + event = cur; + } + } + + return event; +}