Do not assume that days always have 86400 seconds

Make that date membership is computed correctly, even if a day has less
than 86400 seconds (e.g. after changing clocks).

Reported-by: Hakan Jerning <jerning@home.se>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2016-03-27 12:54:10 +02:00
parent e1b6d22669
commit 9e160fac16
8 changed files with 47 additions and 5 deletions

View File

@ -119,8 +119,7 @@ struct apoint *apoint_new(char *mesg, char *note, long start, long dur,
unsigned apoint_inday(struct apoint *i, long *start) unsigned apoint_inday(struct apoint *i, long *start)
{ {
return (i->start <= *start + DAYINSEC return (date_cmp_day(i->start, *start) == 0);
&& i->start + i->dur > *start);
} }
void apoint_sec2str(struct apoint *o, long day, char *start, char *end) void apoint_sec2str(struct apoint *o, long day, char *start, char *end)

View File

@ -1111,6 +1111,7 @@ int get_item_min(long);
struct tm date2tm(struct date, unsigned, unsigned); struct tm date2tm(struct date, unsigned, unsigned);
time_t date2sec(struct date, unsigned, unsigned); time_t date2sec(struct date, unsigned, unsigned);
time_t utcdate2sec(struct date, unsigned, unsigned); time_t utcdate2sec(struct date, unsigned, unsigned);
int date_cmp_day(time_t, time_t);
char *date_sec2date_str(long, const char *); char *date_sec2date_str(long, const char *);
void date_sec2date_fmt(long, const char *, char *); void date_sec2date_fmt(long, const char *, char *);
int date_change(struct tm *, int, int); int date_change(struct tm *, int, int);

View File

@ -107,7 +107,7 @@ struct event *event_new(char *mesg, char *note, long day, int id)
/* Check if the event belongs to the selected day */ /* Check if the event belongs to the selected day */
unsigned event_inday(struct event *i, long *start) unsigned event_inday(struct event *i, long *start)
{ {
return (i->day < *start + DAYINSEC && i->day >= *start); return (date_cmp_day(i->day, *start) == 0);
} }
char *event_tostr(struct event *o) char *event_tostr(struct event *o)

View File

@ -683,7 +683,7 @@ static long diff_years(struct tm lt_start, struct tm lt_end)
static int exc_inday(struct excp *exc, long *day_start) static int exc_inday(struct excp *exc, long *day_start)
{ {
return (exc->st >= *day_start && exc->st < *day_start + DAYINSEC); return (date_cmp_day(exc->st, *day_start) == 0);
} }
/* /*
@ -706,7 +706,7 @@ recur_item_find_occurrence(long item_start, long item_dur,
struct tm lt_day, lt_item, lt_item_day; struct tm lt_day, lt_item, lt_item_day;
time_t t; time_t t;
if (day_start < item_start - DAYINSEC + 1) if (date_cmp_day(day_start, item_start) < 0)
return 0; return 0;
if (rpt_until != 0 && day_start >= rpt_until + item_dur) if (rpt_until != 0 && day_start >= rpt_until + item_dur)

View File

@ -418,6 +418,30 @@ time_t utcdate2sec(struct date day, unsigned hour, unsigned min)
return t; return t;
} }
/* Compare two dates (without comparing times). */
int date_cmp_day(time_t d1, time_t d2)
{
struct tm lt1, lt2;
localtime_r((time_t *)&d1, &lt1);
localtime_r((time_t *)&d2, &lt2);
if (lt1.tm_year < lt2.tm_year)
return -1;
if (lt1.tm_year > lt2.tm_year)
return 1;
if (lt1.tm_mon < lt2.tm_mon)
return -1;
if (lt1.tm_mon > lt2.tm_mon)
return 1;
if (lt1.tm_mday < lt2.tm_mday)
return -1;
if (lt1.tm_mday > lt2.tm_mday)
return 1;
return 0;
}
/* Return a string containing the date, given a date in seconds. */ /* Return a string containing the date, given a date in seconds. */
char *date_sec2date_str(long sec, const char *datefmt) char *date_sec2date_str(long sec, const char *datefmt)
{ {

View File

@ -52,6 +52,7 @@ TESTS = \
next-001.sh \ next-001.sh \
search-001.sh \ search-001.sh \
bug-002.sh \ bug-002.sh \
regress-001.sh \
recur-001.sh \ recur-001.sh \
recur-002.sh \ recur-002.sh \
recur-003.sh \ recur-003.sh \
@ -105,6 +106,7 @@ EXTRA_DIST = \
data/apts-event-006 \ data/apts-event-006 \
data/apts-filter-001 \ data/apts-filter-001 \
data/apts-recur \ data/apts-recur \
data/apts-regress-001 \
data/conf \ data/conf \
data/ical-001.ical \ data/ical-001.ical \
data/ical-002.ical \ data/ical-002.ical \

View File

@ -0,0 +1 @@
03/28/2016 [1] Day after clock adjustment

15
test/regress-001.sh Executable file
View File

@ -0,0 +1,15 @@
#!/bin/sh
. "${TEST_INIT:-./test-init.sh}"
if [ "$1" = 'actual' ]; then
"$CALCURSE" --read-only -D "$DATA_DIR"/ -c "$DATA_DIR/apts-regress-001" \
-Q --filter-type=cal --from=2016-03-27 --days=2
elif [ "$1" = 'expected' ]; then
cat <<EOD
03/28/16:
* Day after clock adjustment
EOD
else
./run-test "$0"
fi