Introduce macro DAY

DAY(t) is midnight (the day) of time_t t.

Signed-off-by: Lars Henriksen <LarsHenriksen@get2net.dk>
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lars Henriksen 2020-11-15 10:23:10 +01:00 committed by Lukas Fleischer
parent 0af69778b5
commit 3599766cee
8 changed files with 32 additions and 39 deletions

View File

@ -668,7 +668,7 @@ static inline void key_generic_cmd(void)
* The selected day need not be the (item) start day * The selected day need not be the (item) start day
* for multi-day occurrences. * for multi-day occurrences.
*/ */
day = update_time_in_date(item->start, 0, 0); day = DAY(item->start);
if (item->type == RECUR_EVNT) { if (item->type == RECUR_EVNT) {
rev = item->item.rev; rev = item->item.rev;
more = recur_prev_occurrence(rev->day, -1, rev->rpt, more = recur_prev_occurrence(rev->day, -1, rev->rpt,

View File

@ -145,6 +145,7 @@
#define DAYLEN(date) (NEXTDAY(date) - (date)) #define DAYLEN(date) (NEXTDAY(date) - (date))
#define ENDOFDAY(date) (NEXTDAY(date) - 1) #define ENDOFDAY(date) (NEXTDAY(date) - 1)
#define HOURINSEC (HOURINMIN * MININSEC) #define HOURINSEC (HOURINMIN * MININSEC)
#define DAY(date) (update_time_in_date(date, 0, 0))
/* Calendar window. */ /* Calendar window. */
#define CALHEIGHT 8 #define CALHEIGHT 8

View File

@ -100,7 +100,7 @@ int day_sel_index(void)
/* If still not found, stay on the same day. */ /* If still not found, stay on the same day. */
VECTOR_FOREACH(&day_items, i) { VECTOR_FOREACH(&day_items, i) {
p = VECTOR_NTH(&day_items, i); p = VECTOR_NTH(&day_items, i);
if (p->order == update_time_in_date(sel_data.order, 0, 0)) if (p->order == DAY(sel_data.order))
return i; return i;
} }
return -1; return -1;
@ -832,7 +832,7 @@ int day_paste_item(struct day_item *p, time_t date)
/* wanted: until = shift + old_until */ /* wanted: until = shift + old_until */
if (p->item.rapt->rpt->until && if (p->item.rapt->rpt->until &&
overflow_add( overflow_add(
date - update_time_in_date(p->item.rapt->start, 0, 0), date - DAY(p->item.rapt->start),
p->item.rapt->rpt->until, p->item.rapt->rpt->until,
&until) &until)
) )

View File

@ -584,7 +584,7 @@ ical_store_apoint(char *mesg, char *note, time_t start, long dur,
* calcurse until day must be changed to the day before. * calcurse until day must be changed to the day before.
*/ */
if (rpt->until) { if (rpt->until) {
day = update_time_in_date(rpt->until, 0, 0); day = DAY(rpt->until);
if (recur_item_find_occurrence(start, dur, rpt, NULL, if (recur_item_find_occurrence(start, dur, rpt, NULL,
day, NULL) && day, NULL) &&
get_item_time(rpt->until) < get_item_time(start)) get_item_time(rpt->until) < get_item_time(start))
@ -1550,7 +1550,7 @@ ical_read_event(FILE * fdi, FILE * log, unsigned *noevents,
char *msg; char *msg;
dur = vevent_type == EVENT ? -1 : vevent.dur; dur = vevent_type == EVENT ? -1 : vevent.dur;
day = update_time_in_date(vevent.start, 0, 0); day = DAY(vevent.start);
msg = _("rrule does not match start day (%s)."); msg = _("rrule does not match start day (%s).");
if (vevent.count) { if (vevent.count) {

View File

@ -237,25 +237,25 @@ void io_dump_apts(const char *fmt_apt, const char *fmt_rapt,
LLIST_FOREACH(&recur_elist, i) { LLIST_FOREACH(&recur_elist, i) {
struct recur_event *rev = LLIST_GET_DATA(i); struct recur_event *rev = LLIST_GET_DATA(i);
time_t day = update_time_in_date(rev->day, 0, 0); time_t day = DAY(rev->day);
print_recur_event(fmt_rev, day, rev); print_recur_event(fmt_rev, day, rev);
} }
LLIST_TS_FOREACH(&recur_alist_p, i) { LLIST_TS_FOREACH(&recur_alist_p, i) {
struct recur_apoint *rapt = LLIST_GET_DATA(i); struct recur_apoint *rapt = LLIST_GET_DATA(i);
time_t day = update_time_in_date(rapt->start, 0, 0); time_t day = DAY(rapt->start);
print_recur_apoint(fmt_rapt, day, rapt->start, rapt); print_recur_apoint(fmt_rapt, day, rapt->start, rapt);
} }
LLIST_TS_FOREACH(&alist_p, i) { LLIST_TS_FOREACH(&alist_p, i) {
struct apoint *apt = LLIST_TS_GET_DATA(i); struct apoint *apt = LLIST_TS_GET_DATA(i);
time_t day = update_time_in_date(apt->start, 0, 0); time_t day = DAY(apt->start);
print_apoint(fmt_apt, day, apt); print_apoint(fmt_apt, day, apt);
} }
LLIST_FOREACH(&eventlist, i) { LLIST_FOREACH(&eventlist, i) {
struct event *ev = LLIST_TS_GET_DATA(i); struct event *ev = LLIST_TS_GET_DATA(i);
time_t day = update_time_in_date(ev->day, 0, 0); time_t day = DAY(ev->day);
print_event(fmt_ev, day, ev); print_event(fmt_ev, day, ev);
} }
} }

View File

@ -517,8 +517,7 @@ char *recur_apoint_scan(FILE *f, struct tm start, struct tm end,
/* Does it occur on the start day? */ /* Does it occur on the start day? */
if (!recur_item_find_occurrence(tstart, tend - tstart, rpt, NULL, if (!recur_item_find_occurrence(tstart, tend - tstart, rpt, NULL,
update_time_in_date(tstart, 0, 0), DAY(tstart), NULL)) {
NULL)) {
char *fmt = _("recurrence error: not on start day (%s)"); char *fmt = _("recurrence error: not on start day (%s)");
return day_ins(&fmt, tstart); return day_ins(&fmt, tstart);
} }
@ -590,8 +589,7 @@ char *recur_event_scan(FILE * f, struct tm start, int id,
/* Does it occur on the start day? */ /* Does it occur on the start day? */
if (!recur_item_find_occurrence(tstart, -1, rpt, NULL, if (!recur_item_find_occurrence(tstart, -1, rpt, NULL,
update_time_in_date(tstart, 0, 0), DAY(tstart), NULL)) {
NULL)) {
char *fmt = _("recurrence error: not on start day (%s)"); char *fmt = _("recurrence error: not on start day (%s)");
return day_ins(&fmt, tstart); return day_ins(&fmt, tstart);
} }
@ -1209,7 +1207,7 @@ static int expand_monthly(time_t start, long dur, struct rpt *rpt, llist_t *exc,
-WEEKINDAYS -WEEKINDAYS
); );
r.until = date_sec_change( r.until = date_sec_change(
update_time_in_date(nstart, 0, 0), DAY(nstart),
0, 0,
r.freq * WEEKINDAYS r.freq * WEEKINDAYS
); );
@ -1243,7 +1241,7 @@ static int expand_monthly(time_t start, long dur, struct rpt *rpt, llist_t *exc,
-WEEKINDAYS -WEEKINDAYS
); );
r.until = date_sec_change( r.until = date_sec_change(
update_time_in_date(nstart, 0, 0), DAY(nstart),
0, 0,
r.freq * WEEKINDAYS r.freq * WEEKINDAYS
); );
@ -1349,7 +1347,7 @@ static int expand_yearly(time_t start, long dur, struct rpt *rpt, llist_t *exc,
-WEEKINDAYS -WEEKINDAYS
); );
r.until = date_sec_change( r.until = date_sec_change(
update_time_in_date(nstart, 0, 0), DAY(nstart),
0, 0,
r.freq * WEEKINDAYS r.freq * WEEKINDAYS
); );
@ -1394,7 +1392,7 @@ static int expand_yearly(time_t start, long dur, struct rpt *rpt, llist_t *exc,
-WEEKINDAYS -WEEKINDAYS
); );
r.until = date_sec_change( r.until = date_sec_change(
update_time_in_date(nstart, 0, 0), DAY(nstart),
0, 0,
r.freq * WEEKINDAYS r.freq * WEEKINDAYS
); );
@ -1840,7 +1838,7 @@ int recur_nth_occurrence(time_t s, long d, struct rpt *r, llist_t *e, int n,
return 0; return 0;
for (n--, *nth = s; n > 0; n--) { for (n--, *nth = s; n > 0; n--) {
day = update_time_in_date(*nth, 0, 0); day = DAY(*nth);
if (!recur_next_occurrence(s, d, r, e, day, nth)) if (!recur_next_occurrence(s, d, r, e, day, nth))
break; break;
} }
@ -1856,13 +1854,13 @@ int recur_prev_occurrence(time_t s, long d, struct rpt *r, llist_t *e,
{ {
time_t prev_day, next; time_t prev_day, next;
if (day <= update_time_in_date(s, 0, 0)) if (day <= DAY(s))
return 0; return 0;
next = *prev = s; next = *prev = s;
while (update_time_in_date(next, 0, 0) < day) { while (DAY(next) < day) {
/* Set new previous and next. */ /* Set new previous and next. */
*prev = next; *prev = next;
prev_day = update_time_in_date(*prev, 0, 0); prev_day = DAY(*prev);
recur_next_occurrence(s, d, r, e, prev_day, &next); recur_next_occurrence(s, d, r, e, prev_day, &next);
/* Multi-day appointment */ /* Multi-day appointment */
if (next == *prev) if (next == *prev)

View File

@ -79,7 +79,7 @@ void ui_day_find_sel(void)
*/ */
time_t ui_day_sel_date(void) time_t ui_day_sel_date(void)
{ {
return update_time_in_date(ui_day_get_sel()->order, 0, 0); return DAY(ui_day_get_sel()->order);
} }
/* /*
@ -184,10 +184,9 @@ static void update_start_time(time_t *start, long *dur, struct rpt *rpt, int mov
newtime = day_edit_time(*start, *dur, move); newtime = day_edit_time(*start, *dur, move);
if (!newtime) if (!newtime)
break; break;
if (rpt && !recur_item_find_occurrence( if (rpt && !recur_item_find_occurrence(newtime, *dur, rpt, NULL,
newtime, *dur, rpt, NULL, DAY(newtime),
update_time_in_date(newtime, 0, 0), NULL)) {
NULL)) {
msg = day_ins(&msg_match, newtime); msg = day_ins(&msg_match, newtime);
status_mesg(msg, msg_enter); status_mesg(msg, msg_enter);
mem_free(msg); mem_free(msg);
@ -786,10 +785,7 @@ static int update_rept(time_t start, long dur, struct rpt **rpt, llist_t *exc,
continue; continue;
} }
/* Until is midnight of the day. */ /* Until is midnight of the day. */
nrpt.until = date_sec_change( nrpt.until = date_sec_change(DAY(start), 0, days);
update_time_in_date(start, 0, 0),
0, days
);
} else if (*timstr == '#') { } else if (*timstr == '#') {
char *eos; char *eos;
count = strtol(timstr + 1, &eos, 10); count = strtol(timstr + 1, &eos, 10);
@ -802,7 +798,7 @@ static int update_rept(time_t start, long dur, struct rpt **rpt, llist_t *exc,
keys_wgetch(win[KEY].p); keys_wgetch(win[KEY].p);
continue; continue;
} }
nrpt.until = update_time_in_date(until, 0, 0); nrpt.until = DAY(until);
break; break;
} else { } else {
int year, month, day; int year, month, day;
@ -816,7 +812,7 @@ static int update_rept(time_t start, long dur, struct rpt **rpt, llist_t *exc,
nrpt.until = date2sec(d, 0, 0); nrpt.until = date2sec(d, 0, 0);
} }
/* Conmpare days (midnights) - until-day may equal start day. */ /* Conmpare days (midnights) - until-day may equal start day. */
if (nrpt.until >= update_time_in_date(start, 0, 0)) if (nrpt.until >= DAY(start))
break; break;
mem_free(timstr); mem_free(timstr);
@ -884,7 +880,7 @@ static int update_rept(time_t start, long dur, struct rpt **rpt, llist_t *exc,
keys_wgetch(win[KEY].p); keys_wgetch(win[KEY].p);
goto cleanup; goto cleanup;
} }
nrpt.until = update_time_in_date(until, 0, 0); nrpt.until = DAY(until);
} }
/* /*
* Check whether the start occurrence matches the recurrence rule, in * Check whether the start occurrence matches the recurrence rule, in
@ -895,8 +891,7 @@ static int update_rept(time_t start, long dur, struct rpt **rpt, llist_t *exc,
char *msg_match = char *msg_match =
_("Repetition must begin on start day (%s); " _("Repetition must begin on start day (%s); "
"any change discarded."); "any change discarded.");
if (!recur_item_find_occurrence(start, dur, &nrpt, NULL, if (!recur_item_find_occurrence(start, dur, &nrpt, NULL, DAY(start),
update_time_in_date(start, 0, 0),
NULL)) { NULL)) {
mem_free(outstr); mem_free(outstr);
outstr = day_ins(&msg_match, start); outstr = day_ins(&msg_match, start);
@ -1525,7 +1520,7 @@ void ui_day_draw(int n, WINDOW *win, int y, int hilt, void *cb_data)
{ {
struct day_item *item = day_get_item(n); struct day_item *item = day_get_item(n);
/* The item order always indicates the date. */ /* The item order always indicates the date. */
time_t date = update_time_in_date(item->order, 0, 0); time_t date = DAY(item->order);
int width = lb_apt.sw.w - 2, is_slctd; int width = lb_apt.sw.w - 2, is_slctd;
hilt = hilt && (wins_slctd() == APP); hilt = hilt && (wins_slctd() == APP);

View File

@ -1643,7 +1643,7 @@ static void print_date(time_t date, time_t day, const char *extformat)
if (!strcmp(extformat, "epoch")) { if (!strcmp(extformat, "epoch")) {
printf("%ld", (long)date); printf("%ld", (long)date);
} else { } else {
time_t day_start = update_time_in_date(day, 0, 0); time_t day_start = DAY(day);
time_t day_end = date_sec_change(day_start, 0, 1); time_t day_end = date_sec_change(day_start, 0, 1);
struct tm lt; struct tm lt;
@ -2079,8 +2079,7 @@ char *day_ins(char **template, time_t t)
{ {
char *day, *msg; char *day, *msg;
day = date_sec2date_str(update_time_in_date(t, 0 , 0), day = date_sec2date_str(DAY(t), DATEFMT(conf.input_datefmt));
DATEFMT(conf.input_datefmt));
asprintf(&msg, *template, day); asprintf(&msg, *template, day);
mem_free(day); mem_free(day);
return msg; return msg;