diff --git a/dba/rrule_functions.sql b/dba/rrule_functions.sql index 9aab1b31..f33731a1 100644 --- a/dba/rrule_functions.sql +++ b/dba/rrule_functions.sql @@ -641,13 +641,19 @@ DECLARE repeatrule ALIAS FOR $3; in_mindate ALIAS FOR $4; in_maxdate ALIAS FOR $5; + base_date TIMESTAMP WITH TIME ZONE; mindate TIMESTAMP WITH TIME ZONE; maxdate TIMESTAMP WITH TIME ZONE; BEGIN - IF dtstart IS NULL OR dtend IS NULL THEN + IF dtstart IS NULL THEN RETURN NULL; END IF; + IF dtend IS NULL THEN + base_date := dtstart; + ELSE + base_date := dtend; + END IF; IF in_mindate IS NULL THEN mindate := current_date - '10 years'::interval; @@ -659,14 +665,14 @@ BEGIN maxdate := current_date + '10 years'::interval; ELSE -- If we add the duration onto the event, then an overlap occurs if dtend <= increased end of range. - maxdate := in_maxdate + (dtend - dtstart); + maxdate := in_maxdate + (base_date - dtstart); END IF; IF repeatrule IS NULL THEN - RETURN (dtstart <= maxdate AND dtend >= mindate); + RETURN (dtstart <= maxdate AND base_date >= mindate); END IF; - SELECT d INTO mindate FROM rrule_event_instances_range( dtend, repeatrule, mindate, maxdate, 3 ) d LIMIT 1; + SELECT d INTO mindate FROM rrule_event_instances_range( base_date, repeatrule, mindate, maxdate, 60 ) d LIMIT 1; RETURN FOUND; END;