def to_dateutil_rruleset(self, dtstart=None, dtend=None, cache=False): """ Create a `dateutil.rrule.rruleset` instance from this `Recurrence`. :Parameters: `dtstart` : datetime.datetime The date/time the recurrence rule starts. This value overrides the `dtstart` property specified by the `Recurrence` instance if its set. `dtstart` : datetime.datetime Optionally specify the first occurrence of the occurrence set. Defauts to `self.dtstart` if specified or `datetime.datetime.now()` if not when the occurrence set is generated. `cache` : bool If given, it must be a boolean value specifying to enable or disable caching of results. If you will use the same `dateutil.rrule.rrule` instance multiple times, enabling caching will improve the performance considerably. :Returns: A `dateutil.rrule.rruleset` instance. """ # all datetimes used in dateutil.rrule objects will need to be # normalized to either offset-aware or offset-naive datetimes # to avoid exceptions. dateutil will use the tzinfo from the # given dtstart, which will cascade to other datetime objects. dtstart = dtstart or self.dtstart dtend = dtend or self.dtend if dtend: dtend = normalize_offset_awareness(dtend or self.dtend, dtstart) if cache: # we need to cache an instance for each unique dtstart # value because the occurrence values will differ. cached = self._cache.get(dtstart) if cached: return cached rruleset = dateutil.rrule.rruleset(cache=cache) for rrule in self.rrules: rruleset.rrule(rrule.to_dateutil_rrule(dtstart, dtend, cache)) for exrule in self.exrules: rruleset.exrule(exrule.to_dateutil_rrule(dtstart, dtend, cache)) if dtstart is not None: rruleset.rdate(dtstart) for rdate in self.rdates: rdate = normalize_offset_awareness(rdate, dtstart) if dtend is not None and rdate < dtend: rruleset.rdate(rdate) elif not dtend: rruleset.rdate(rdate) if dtend is not None: rruleset.rdate(dtend) for exdate in self.exdates: exdate = normalize_offset_awareness(exdate, dtstart) if dtend is not None and exdate < dtend: rruleset.exdate(exdate) elif not dtend: rruleset.exdate(exdate) if cache: self._cache[dtstart] = rruleset return rruleset