postgres-rrule icon indicating copy to clipboard operation
postgres-rrule copied to clipboard

occurences() for rruleset does not respect DTEND

Open GavinRay97 opened this issue 5 years ago • 0 comments

The function that consumes an rruleset and returns occurences passes a tsrange instance of (,) by default, which ignores dtend set in in the rruleset.

The following function fixes this:

CREATE OR REPLACE FUNCTION _rrule.occurrences("rruleset" _rrule.RRULESET)
  RETURNS SETOF TIMESTAMP AS $$
DECLARE
  local_rrule _rrule.rrule := rruleset.rrule;
  tsrange_start TIMESTAMP := rruleset.dtstart;
  tsrange_end TIMESTAMP;
  calculated_range TSRANGE;
BEGIN
  IF rruleset.dtend IS NOT NULL THEN
    tsrange_end := rruleset.dtend;
  ELSIF local_rrule.until IS NOT NULL THEN
    tsrange_end = local_rrule.until;
  END IF;

  IF tsrange_end IS NOT NULL THEN
    calculated_range = tsrange(tsrange_start, tsrange_end);
  ELSE
    calculated_range = tsrange(tsrange_start);
  END IF;

  RETURN QUERY SELECT * FROM _rrule.occurrences("rruleset", calculated_range);
END;
$$ LANGUAGE plpgsql IMMUTABLE STRICT;

GavinRay97 avatar Apr 09 '20 21:04 GavinRay97