Avoid unnecessary start time calculations

Rename recur_*_inday() to recur_*_find_occurrence() and use the new
functions whenever we actually care about the start time of an
occurrence.

Reintroduce recur_*_inday() as wrappers to recur_*_find_occurrence() and
pass NULL as start time buffer (which means "skip start time
calculation"). Keep using these when we only want to know if a recurrent
item belongs to a specific day but do not care about the actual start
time.

Signed-off-by: Lukas Fleischer <calcurse@cryptocrack.de>
This commit is contained in:
Lukas Fleischer 2011-10-11 13:06:55 +02:00
parent 28c98f7d01
commit 2bf0249338
5 changed files with 70 additions and 23 deletions

View File

@ -432,6 +432,7 @@ app_arg (int add_line, struct date *day, long date, int print_note,
{ {
struct apoint *apt = LLIST_TS_GET_DATA (i); struct apoint *apt = LLIST_TS_GET_DATA (i);
struct recur_apoint *ra = LLIST_TS_GET_DATA (j); struct recur_apoint *ra = LLIST_TS_GET_DATA (j);
unsigned occurrence;
while (i && regex && regexec (regex, apt->mesg, 0, 0, 0) != 0) while (i && regex && regexec (regex, apt->mesg, 0, 0, 0) != 0)
{ {
@ -447,7 +448,8 @@ app_arg (int add_line, struct date *day, long date, int print_note,
if (apt && ra) if (apt && ra)
{ {
if (apt->start <= recur_apoint_inday (ra, today)) if (recur_apoint_find_occurrence (ra, today, &occurrence) &&
apt->start <= occurrence)
ra = NULL; ra = NULL;
else else
apt = NULL; apt = NULL;

View File

@ -821,6 +821,12 @@ struct recur_event *recur_event_scan (FILE *, struct tm, int, char,
void recur_apoint_write (struct recur_apoint *, FILE *); void recur_apoint_write (struct recur_apoint *, FILE *);
void recur_event_write (struct recur_event *, FILE *); void recur_event_write (struct recur_event *, FILE *);
void recur_save_data (FILE *); void recur_save_data (FILE *);
unsigned recur_item_find_occurrence (long, long, llist_t *, int,
int, long, long, unsigned *);
unsigned recur_apoint_find_occurrence (struct recur_apoint *,
long, unsigned *);
unsigned recur_event_find_occurrence (struct recur_event *, long,
unsigned *);
unsigned recur_item_inday (long, long, llist_t *, int, int, long, unsigned recur_item_inday (long, long, llist_t *, int, int, long,
long); long);
unsigned recur_apoint_inday(struct recur_apoint *, long); unsigned recur_apoint_inday(struct recur_apoint *, long);

View File

@ -225,11 +225,14 @@ day_store_recur_apoints (long date)
LLIST_TS_FIND_FOREACH (&recur_alist_p, date, recur_apoint_inday, i) LLIST_TS_FIND_FOREACH (&recur_alist_p, date, recur_apoint_inday, i)
{ {
struct recur_apoint *rapt = LLIST_TS_GET_DATA (i); struct recur_apoint *rapt = LLIST_TS_GET_DATA (i);
int real_start = recur_apoint_inday (rapt, date); unsigned real_start;
if (recur_apoint_find_occurrence (rapt, date, &real_start))
{
(void)day_add_apoint (RECUR_APPT, rapt->mesg, rapt->note, real_start, (void)day_add_apoint (RECUR_APPT, rapt->mesg, rapt->note, real_start,
rapt->dur, rapt->state, a_nb); rapt->dur, rapt->state, a_nb);
a_nb++; a_nb++;
} }
}
LLIST_TS_UNLOCK (&recur_alist_p); LLIST_TS_UNLOCK (&recur_alist_p);
return a_nb; return a_nb;

View File

@ -507,15 +507,15 @@ notify_check_added (char *mesg, long start, char state)
void void
notify_check_repeated (struct recur_apoint *i) notify_check_repeated (struct recur_apoint *i)
{ {
long real_app_time; unsigned real_app_time;
int update_notify = 0; int update_notify = 0;
time_t current_time; time_t current_time;
current_time = time (NULL); current_time = time (NULL);
pthread_mutex_lock (&notify_app.mutex); pthread_mutex_lock (&notify_app.mutex);
if ((real_app_time = recur_item_inday (i->start, i->dur, &i->exc, if (recur_item_find_occurrence (i->start, i->dur, &i->exc, i->rpt->type,
i->rpt->type, i->rpt->freq, i->rpt->freq, i->rpt->until, get_today (),
i->rpt->until, get_today ()))) &real_app_time))
{ {
if (!notify_app.got_app) if (!notify_app.got_app)
{ {
@ -556,10 +556,11 @@ int
notify_same_recur_item (struct recur_apoint *i) notify_same_recur_item (struct recur_apoint *i)
{ {
int same = 0; int same = 0;
long item_start = 0; unsigned item_start = 0;
item_start = recur_item_inday (i->start, i->dur, &i->exc, i->rpt->type, recur_item_find_occurrence (i->start, i->dur, &i->exc, i->rpt->type,
i->rpt->freq, i->rpt->until, get_today ()); i->rpt->freq, i->rpt->until, get_today (),
&item_start);
pthread_mutex_lock (&notify_app.mutex); pthread_mutex_lock (&notify_app.mutex);
if (notify_app.got_app && item_start == notify_app.time) if (notify_app.got_app && item_start == notify_app.time)
same = 1; same = 1;

View File

@ -615,8 +615,8 @@ exc_inday (struct excp *exc, long day_start)
} }
/* /*
* Check if the recurrent item belongs to the selected day, * Check if the recurrent item belongs to the selected day, and if yes, store
* and if yes, return the real start time. * the start date of the occurrence that belongs to the day in a buffer.
* *
* This function was improved thanks to Tony's patch. * This function was improved thanks to Tony's patch.
* Thanks also to youshe for reporting daylight saving time related problems. * Thanks also to youshe for reporting daylight saving time related problems.
@ -624,8 +624,9 @@ exc_inday (struct excp *exc, long day_start)
* calculation of recurrent dates after a turn of years. * calculation of recurrent dates after a turn of years.
*/ */
unsigned unsigned
recur_item_inday (long item_start, long item_dur, llist_t *item_exc, recur_item_find_occurrence (long item_start, long item_dur, llist_t *item_exc,
int rpt_type, int rpt_freq, long rpt_until, long day_start) int rpt_type, int rpt_freq, long rpt_until,
long day_start, unsigned *occurrence)
{ {
struct date start_date; struct date start_date;
long diff, span; long diff, span;
@ -692,17 +693,51 @@ recur_item_inday (long item_start, long item_dur, llist_t *item_exc,
diff = diff_days (lt_item_day, lt_day); diff = diff_days (lt_item_day, lt_day);
if (diff <= span) if (diff <= span)
{
if (occurrence)
{ {
start_date.dd = lt_item_day.tm_mday; start_date.dd = lt_item_day.tm_mday;
start_date.mm = lt_item_day.tm_mon + 1; start_date.mm = lt_item_day.tm_mon + 1;
start_date.yyyy = lt_item_day.tm_year + 1900; start_date.yyyy = lt_item_day.tm_year + 1900;
return date2sec (start_date, lt_item.tm_hour, lt_item.tm_min); *occurrence = date2sec (start_date, lt_item.tm_hour, lt_item.tm_min);
}
return 1;
} }
else else
return 0; return 0;
} }
unsigned
recur_apoint_find_occurrence (struct recur_apoint *rapt, long day_start,
unsigned *occurrence)
{
return recur_item_find_occurrence (rapt->start, rapt->dur, &rapt->exc,
rapt->rpt->type, rapt->rpt->freq,
rapt->rpt->until, day_start, occurrence);
}
unsigned
recur_event_find_occurrence (struct recur_event *rev, long day_start,
unsigned *occurrence)
{
return recur_item_find_occurrence (rev->day, DAYINSEC, &rev->exc,
rev->rpt->type, rev->rpt->freq,
rev->rpt->until, day_start, occurrence);
}
/* Check if a recurrent item belongs to the selected day. */
unsigned
recur_item_inday (long item_start, long item_dur, llist_t *item_exc,
int rpt_type, int rpt_freq, long rpt_until, long day_start)
{
/* We do not need the (real) start time of the occurrence here, so just
* ignore the buffer. */
return recur_item_find_occurrence (item_start, item_dur, item_exc, rpt_type,
rpt_freq, rpt_until, day_start, NULL);
}
unsigned unsigned
recur_apoint_inday(struct recur_apoint *rapt, long day_start) recur_apoint_inday(struct recur_apoint *rapt, long day_start)
{ {
@ -1011,15 +1046,15 @@ struct notify_app *
recur_apoint_check_next (struct notify_app *app, long start, long day) recur_apoint_check_next (struct notify_app *app, long start, long day)
{ {
llist_item_t *i; llist_item_t *i;
long real_recur_start_time; unsigned real_recur_start_time;
LLIST_TS_LOCK (&recur_alist_p); LLIST_TS_LOCK (&recur_alist_p);
LLIST_TS_FIND_FOREACH (&recur_alist_p, app->time, recur_apoint_starts_before, i) LLIST_TS_FIND_FOREACH (&recur_alist_p, app->time, recur_apoint_starts_before, i)
{ {
struct recur_apoint *rapt = LLIST_TS_GET_DATA (i); struct recur_apoint *rapt = LLIST_TS_GET_DATA (i);
real_recur_start_time = recur_apoint_inday(rapt, day); if (recur_apoint_find_occurrence (rapt, day, &real_recur_start_time) &&
if (real_recur_start_time > start) real_recur_start_time > start)
{ {
app->time = real_recur_start_time; app->time = real_recur_start_time;
app->txt = mem_strdup (rapt->mesg); app->txt = mem_strdup (rapt->mesg);