From 96439bc6fffe21b8359bd825aa89fc586d36380a Mon Sep 17 00:00:00 2001 From: Federico Mena Quintero Date: Fri, 2 Oct 1998 23:59:35 +0000 Subject: Changed name from time_start_of_day() to be consistent with the other 1998-10-02 Federico Mena Quintero * timeutil.c (time_day_begin): Changed name from time_start_of_day() to be consistent with the other begin/end functions. (time_day_end): Likewise. * calobj.c (ical_object_get_first_weekday): New public function to get the first toggled day in a weekday mask. Since we do not support multiple weekdays in a monthly-by-pos rule, we just fetch the first toggled one. (ical_object_generate_events): Added a missing break statement. * timeutil.c (time_month_end): Made it consistent with the rest of the time begin/end functions -- now it returns the first second of the *next* month. (time_week_end): Actually implemented this function. It will be used when the week view is rewritten. * calobj.c (time_in_range): Fix off-by-one in the comparison of the time against the end time. * gncal-full-day.c (expand_space): Fixed bug where the columns not were being expanded due to a missing "slot + j". svn path=/trunk/; revision=425 --- calendar/pcs/calobj.c | 164 +++++++++++++++++++++++++++++++------------------- 1 file changed, 103 insertions(+), 61 deletions(-) (limited to 'calendar/pcs/calobj.c') diff --git a/calendar/pcs/calobj.c b/calendar/pcs/calobj.c index 33aff8cda8..9e5f41bc27 100644 --- a/calendar/pcs/calobj.c +++ b/calendar/pcs/calobj.c @@ -1025,15 +1025,28 @@ generate (iCalObject *ico, time_t reference, calendarfn cb, void *closure) e_t = mktime (&dt_end); - if (s_t == -1 || e_t == -1){ + if ((s_t == -1) || (e_t == -1)) { g_warning ("Produced invalid dates!\n"); return 0; } - return (*cb)(ico, s_t, e_t, closure); + + return (*cb) (ico, s_t, e_t, closure); +} + +int +ical_object_get_first_weekday (int weekday_mask) +{ + int i; + + for (i = 0; i < 7; i++) + if (weekday_mask & (1 << i)) + return i; + + return -1; } -#define time_in_range(x,a,b) ((x >= a) && (b ? x <= b : 1)) -#define recur_in_range(t,r) (r->enddate ? (t < r->enddate) : 1) +#define time_in_range(t, a, b) ((t >= a) && (b ? (t < b) : 1)) +#define recur_in_range(t, r) (r->enddate ? (t < r->enddate) : 1) /* * Generate every possible event. Invokes the callback routine for @@ -1046,88 +1059,104 @@ generate (iCalObject *ico, time_t reference, calendarfn cb, void *closure) void ical_object_generate_events (iCalObject *ico, time_t start, time_t end, calendarfn cb, void *closure) { - Recurrence *recur = ico->recur; time_t current; - int first_week_day, i; - - if (!ico->recur){ - if ((end && (ico->dtstart < end) && ico->dtend > start) || - (end == 0 && ico->dtend > start)){ + int first_week_day; + + /* If there is no recurrence, just check ranges */ + + if (!ico->recur) { + if ((end && (ico->dtstart < end) && (ico->dtend > start)) + || ((end == 0) && (ico->dtend > start))) { time_t ev_s, ev_e; - ev_s = ico->dtstart < start ? start : ico->dtstart; - ev_e = ico->dtend > end ? end : ico->dtend; - (*cb)(ico, ev_s, ev_e, closure); + /* Clip range */ + + ev_s = MAX (ico->dtstart, start); + ev_e = MIN (ico->dtend, end); + + (* cb) (ico, ev_s, ev_e, closure); } return; } - /* The event has a recurrence rule */ - if (end != 0){ + /* The event has a recurrence rule -- check that we will generate at least one instance */ + + if (end != 0) { if (ico->dtstart > end) return; - if (!IS_INFINITE (recur) && recur->enddate < start) + + if (!IS_INFINITE (ico->recur) && (ico->recur->enddate < start)) return; } + /* Generate the instances */ + current = ico->dtstart; - switch (recur->type){ + + switch (ico->recur->type) { case RECUR_DAILY: do { - if (time_in_range (current, start, end) && recur_in_range (current, recur)){ + if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) if (!generate (ico, current, cb, closure)) return; - } /* Advance */ - current = time_add_day (current, recur->interval); - - if (current == -1){ - g_warning ("RECUR_DAILY: mktime error\n"); + + current = time_add_day (current, ico->recur->interval); + + if (current == -1) { + g_warning ("RECUR_DAILY: time_add_day() returned invalid time"); return; } - } while (current < end || (end == 0)); + } while ((current < end) || (end == 0)); + break; case RECUR_WEEKLY: do { - struct tm *tm = localtime (¤t); - - if (time_in_range (current, start, end) && recur_in_range (current, recur)){ - if (recur->weekday & (1 << tm->tm_wday)) + struct tm *tm; + + tm = localtime (¤t); + + if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) { + /* Weekdays to recur on are specified as a bitmask */ + if (ico->recur->weekday & (1 << tm->tm_wday)) if (!generate (ico, current, cb, closure)) return; } /* Advance by day for scanning the week or by interval at week end */ + if (tm->tm_wday == 6) - current = time_add_day (current, (recur->interval-1) * 7 + 1); + current = time_add_day (current, (ico->recur->interval - 1) * 7 + 1); else current = time_add_day (current, 1); - if (current == -1){ - g_warning ("RECUR_WEEKLY: mktime error\n"); + if (current == -1) { + g_warning ("RECUR_WEEKLY: time_add_day() returned invalid time\n"); return; } } while (current < end || (end == 0)); + break; - + case RECUR_MONTHLY_BY_POS: /* FIXME: We only deal with positives now */ - if (recur->u.month_pos < 0) + if (ico->recur->u.month_pos < 0) { + g_warning ("RECUR_MONTHLY_BY_POS does not support negative positions yet"); return; - - if (recur->u.month_pos == 0) + } + + if (ico->recur->u.month_pos == 0) return; - - first_week_day = 7; - for (i = 6; i >= 0; i--) - if (recur->weekday & (1 << i)) - first_week_day = i; + + first_week_day = ical_object_get_first_weekday (ico->recur->weekday); /* This should not happen, but take it into account */ - if (first_week_day == 7) + if (first_week_day == -1) { + g_warning ("ical_object_get_first_weekday() returned -1"); return; + } do { struct tm tm; @@ -1140,63 +1169,76 @@ ical_object_generate_events (iCalObject *ico, time_t start, time_t end, calendar tm = *localtime (&t); week_day_start = tm.tm_wday; - tm.tm_mday = 7 * (recur->u.month_pos - - ((week_day_start <= first_week_day ) ? 1 : 0)) - - (week_day_start - first_week_day) + 1; - + tm.tm_mday = (7 * (ico->recur->u.month_pos - ((week_day_start <= first_week_day ) ? 1 : 0)) + - (week_day_start - first_week_day) + 1); + t = mktime (&tm); - - if (time_in_range (t, start, end) && recur_in_range (current, recur)) + + if (time_in_range (t, start, end) && recur_in_range (current, ico->recur)) if (!generate (ico, t, cb, closure)) return; - /* Advance a month */ + /* Advance by the appropriate number of months */ + current = mktime (&tm); tm.tm_mday = 1; - tm.tm_mon += recur->interval; + tm.tm_mon += ico->recur->interval; current = mktime (&tm); - if (current == -1){ + if (current == -1) { g_warning ("RECUR_MONTHLY_BY_DAY: mktime error\n"); return; } - } while (current < end || (end == 0)); + } while ((current < end) || (end == 0)); + break; case RECUR_MONTHLY_BY_DAY: do { - struct tm *tm = localtime (¤t); + struct tm *tm; time_t t; int p; + tm = localtime (¤t); + p = tm->tm_mday; - tm->tm_mday = recur->u.month_day; + tm->tm_mday = ico->recur->u.month_day; t = mktime (tm); - if (time_in_range (t, start, end) && recur_in_range (current, recur)) + if (time_in_range (t, start, end) && recur_in_range (current, ico->recur)) if (!generate (ico, t, cb, closure)) return; - /* Advance a month */ + /* Advance by the appropriate number of months */ + tm->tm_mday = p; - tm->tm_mon += recur->interval; + tm->tm_mon += ico->recur->interval; current = mktime (tm); - if (current == -1){ + + if (current == -1) { g_warning ("RECUR_MONTHLY_BY_DAY: mktime error\n"); return; } } while (current < end || (end == 0)); + + break; case RECUR_YEARLY_BY_MONTH: case RECUR_YEARLY_BY_DAY: do { - if (time_in_range (current, start, end) && recur_in_range (current, recur)) + if (time_in_range (current, start, end) && recur_in_range (current, ico->recur)) if (!generate (ico, current, cb, closure)) return; - + /* Advance */ - current = time_add_year (current, recur->interval); + + current = time_add_year (current, ico->recur->interval); } while (current < end || (end == 0)); + + break; + + default: + g_assert_not_reached (); } } @@ -1210,7 +1252,7 @@ duration_callback (iCalObject *ico, time_t start, time_t end, void *closure) (*count)++; if (ico->recur->duration == *count) { - ico->recur->enddate = time_end_of_day (end); + ico->recur->enddate = time_day_end (end); return 0; } return 1; -- cgit v1.2.3