rrule

The rrule module offers a small, complete, and very fast, implementation of the recurrence rules documented in the iCalendar RFC, including support for caching of results.

Classes

class dateutil.rrule.rrule(freq, dtstart=None, interval=1, wkst=None, count=None, until=None, bysetpos=None, bymonth=None, bymonthday=None, byyearday=None, byeaster=None, byweekno=None, byweekday=None, byhour=None, byminute=None, bysecond=None, cache=False)[source]

That’s the base of the rrule operation. It accepts all the keywords defined in the RFC as its constructor parameters (except byday, which was renamed to byweekday) and more. The constructor prototype is:

rrule(freq)

Where freq must be one of YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, or SECONDLY.

Note

Per RFC section 3.3.10, recurrence instances falling on invalid dates and times are ignored rather than coerced:

Recurrence rules may generate recurrence instances with an invalid date (e.g., February 30) or nonexistent local time (e.g., 1:30 AM on a day where the local time is moved forward by an hour at 1:00 AM). Such recurrence instances MUST be ignored and MUST NOT be counted as part of the recurrence set.

This can lead to possibly surprising behavior when, for example, the start date occurs at the end of the month:

>>> from dateutil.rrule import rrule, MONTHLY
>>> from datetime import datetime
>>> start_date = datetime(2014, 12, 31)
>>> list(rrule(freq=MONTHLY, count=4, dtstart=start_date))
... 
[datetime.datetime(2014, 12, 31, 0, 0),
 datetime.datetime(2015, 1, 31, 0, 0),
 datetime.datetime(2015, 3, 31, 0, 0),
 datetime.datetime(2015, 5, 31, 0, 0)]

Additionally, it supports the following keyword arguments:

Parameters:
  • dtstart – The recurrence start. Besides being the base for the recurrence, missing parameters in the final recurrence instances will also be extracted from this date. If not given, datetime.now() will be used instead.

  • interval – The interval between each freq iteration. For example, when using YEARLY, an interval of 2 means once every two years, but with HOURLY, it means once every two hours. The default interval is 1.

  • wkst – The week start day. Must be one of the MO, TU, WE constants, or an integer, specifying the first day of the week. This will affect recurrences based on weekly periods. The default week start is got from calendar.firstweekday(), and may be modified by calendar.setfirstweekday().

  • count

    If given, this determines how many occurrences will be generated.

    Note

    As of version 2.5.0, the use of the keyword until in conjunction with count is deprecated, to make sure dateutil is fully compliant with RFC-5545 Sec. 3.3.10. Therefore, until and count must not occur in the same call to rrule.

  • until

    If given, this must be a datetime instance specifying the upper-bound limit of the recurrence. The last recurrence in the rule is the greatest datetime that is less than or equal to the value specified in the until parameter.

    Note

    As of version 2.5.0, the use of the keyword until in conjunction with count is deprecated, to make sure dateutil is fully compliant with RFC-5545 Sec. 3.3.10. Therefore, until and count must not occur in the same call to rrule.

  • bysetpos – If given, it must be either an integer, or a sequence of integers, positive or negative. Each given integer will specify an occurrence number, corresponding to the nth occurrence of the rule inside the frequency period. For example, a bysetpos of -1 if combined with a MONTHLY frequency, and a byweekday of (MO, TU, WE, TH, FR), will result in the last work day of every month.

  • bymonth – If given, it must be either an integer, or a sequence of integers, meaning the months to apply the recurrence to.

  • bymonthday – If given, it must be either an integer, or a sequence of integers, meaning the month days to apply the recurrence to.

  • byyearday – If given, it must be either an integer, or a sequence of integers, meaning the year days to apply the recurrence to.

  • byeaster – If given, it must be either an integer, or a sequence of integers, positive or negative. Each integer will define an offset from the Easter Sunday. Passing the offset 0 to byeaster will yield the Easter Sunday itself. This is an extension to the RFC specification.

  • byweekno – If given, it must be either an integer, or a sequence of integers, meaning the week numbers to apply the recurrence to. Week numbers have the meaning described in ISO8601, that is, the first week of the year is that containing at least four days of the new year.

  • byweekday – If given, it must be either an integer (0 == MO), a sequence of integers, one of the weekday constants (MO, TU, etc), or a sequence of these constants. When given, these variables will define the weekdays where the recurrence will be applied. It’s also possible to use an argument n for the weekday instances, which will mean the nth occurrence of this weekday in the period. For example, with MONTHLY, or with YEARLY and BYMONTH, using FR(+1) in byweekday will specify the first friday of the month where the recurrence happens. Notice that in the RFC documentation, this is specified as BYDAY, but was renamed to avoid the ambiguity of that keyword.

  • byhour – If given, it must be either an integer, or a sequence of integers, meaning the hours to apply the recurrence to.

  • byminute – If given, it must be either an integer, or a sequence of integers, meaning the minutes to apply the recurrence to.

  • bysecond – If given, it must be either an integer, or a sequence of integers, meaning the seconds to apply the recurrence to.

  • cache – If given, it must be a boolean value specifying to enable or disable caching of results. If you will use the same rrule instance multiple times, enabling caching will improve the performance considerably.

after(dt, inc=False)

Returns the first recurrence after the given datetime instance. The inc keyword defines what happens if dt is an occurrence. With inc=True, if dt itself is an occurrence, it will be returned.

before(dt, inc=False)

Returns the last recurrence before the given datetime instance. The inc keyword defines what happens if dt is an occurrence. With inc=True, if dt itself is an occurrence, it will be returned.

between(after, before, inc=False, count=1)

Returns all the occurrences of the rrule between after and before. The inc keyword defines what happens if after and/or before are themselves occurrences. With inc=True, they will be included in the list, if they are found in the recurrence set.

count()

Returns the number of recurrences in this set. It will have go through the whole recurrence, if this hasn’t been done before.

replace(**kwargs)[source]

Return new rrule with same attributes except for those attributes given new values by whichever keyword arguments are specified.

xafter(dt, count=None, inc=False)

Generator which yields up to count recurrences after the given datetime instance, equivalent to after.

Parameters:
  • dt – The datetime at which to start generating recurrences.

  • count – The maximum number of recurrences to generate. If None (default), dates are generated until the recurrence rule is exhausted.

  • inc – If dt is an instance of the rule and inc is True, it is included in the output.

Yields:

Yields a sequence of datetime objects.

class dateutil.rrule.rruleset(cache=False)[source]

The rruleset type allows more complex recurrence setups, mixing multiple rules, dates, exclusion rules, and exclusion dates. The type constructor takes the following keyword arguments:

Parameters:

cache – If True, caching of results will be enabled, improving performance of multiple queries considerably.

after(dt, inc=False)

Returns the first recurrence after the given datetime instance. The inc keyword defines what happens if dt is an occurrence. With inc=True, if dt itself is an occurrence, it will be returned.

before(dt, inc=False)

Returns the last recurrence before the given datetime instance. The inc keyword defines what happens if dt is an occurrence. With inc=True, if dt itself is an occurrence, it will be returned.

between(after, before, inc=False, count=1)

Returns all the occurrences of the rrule between after and before. The inc keyword defines what happens if after and/or before are themselves occurrences. With inc=True, they will be included in the list, if they are found in the recurrence set.

count()

Returns the number of recurrences in this set. It will have go through the whole recurrence, if this hasn’t been done before.

exdate(exdate)[source]

Include the given datetime instance in the recurrence set exclusion list. Dates included that way will not be generated, even if some inclusive rrule or rdate matches them.

exrule(exrule)[source]

Include the given rrule instance in the recurrence set exclusion list. Dates which are part of the given recurrence rules will not be generated, even if some inclusive rrule or rdate matches them.

rdate(rdate)[source]

Include the given datetime instance in the recurrence set generation.

rrule(rrule)[source]

Include the given rrule instance in the recurrence set generation.

xafter(dt, count=None, inc=False)

Generator which yields up to count recurrences after the given datetime instance, equivalent to after.

Parameters:
  • dt – The datetime at which to start generating recurrences.

  • count – The maximum number of recurrences to generate. If None (default), dates are generated until the recurrence rule is exhausted.

  • inc – If dt is an instance of the rule and inc is True, it is included in the output.

Yields:

Yields a sequence of datetime objects.

Functions

dateutil.rrule.rrulestr(s, **kwargs)

Parses a string representation of a recurrence rule or set of recurrence rules.

Parameters:
  • s – Required, a string defining one or more recurrence rules.

  • dtstart – If given, used as the default recurrence start if not specified in the rule string.

  • cache – If set True caching of results will be enabled, improving performance of multiple queries considerably.

  • unfold – If set True indicates that a rule string is split over more than one line and should be joined before processing.

  • forceset – If set True forces a dateutil.rrule.rruleset to be returned.

  • compatible – If set True forces unfold and forceset to be True.

  • ignoretz – If set True, time zones in parsed strings are ignored and a naive datetime.datetime object is returned.

  • tzids – If given, a callable or mapping used to retrieve a datetime.tzinfo from a string representation. Defaults to dateutil.tz.gettz().

  • tzinfos – Additional time zone names / aliases which may be present in a string representation. See dateutil.parser.parse() for more information.

Returns:

Returns a dateutil.rrule.rruleset or dateutil.rrule.rrule

rrule examples

These examples were converted from the RFC.

Prepare the environment.

>>> from dateutil.rrule import *
>>> from dateutil.parser import *
>>> from datetime import *

>>> import pprint
>>> import sys
>>> sys.displayhook = pprint.pprint

Daily, for 10 occurrences.

 >>> list(rrule(DAILY, count=10,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 3, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 9, 5, 9, 0),
  datetime.datetime(1997, 9, 6, 9, 0),
  datetime.datetime(1997, 9, 7, 9, 0),
  datetime.datetime(1997, 9, 8, 9, 0),
  datetime.datetime(1997, 9, 9, 9, 0),
  datetime.datetime(1997, 9, 10, 9, 0),
  datetime.datetime(1997, 9, 11, 9, 0)]

Daily until December 24, 1997

 >>> list(rrule(DAILY,
 ...            dtstart=parse("19970902T090000"),
 ...            until=parse("19971224T000000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 3, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  ...
  datetime.datetime(1997, 12, 21, 9, 0),
  datetime.datetime(1997, 12, 22, 9, 0),
  datetime.datetime(1997, 12, 23, 9, 0)]

Every other day, 5 occurrences.

 >>> list(rrule(DAILY, interval=2, count=5,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 9, 6, 9, 0),
  datetime.datetime(1997, 9, 8, 9, 0),
  datetime.datetime(1997, 9, 10, 9, 0)]

Every 10 days, 5 occurrences.

 >>> list(rrule(DAILY, interval=10, count=5,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 12, 9, 0),
  datetime.datetime(1997, 9, 22, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0),
  datetime.datetime(1997, 10, 12, 9, 0)]

Everyday in January, for 3 years.

 >>> list(rrule(YEARLY, bymonth=1, byweekday=range(7),
 ...            dtstart=parse("19980101T090000"),
 ...            until=parse("20000131T090000")))
 [datetime.datetime(1998, 1, 1, 9, 0),
  datetime.datetime(1998, 1, 2, 9, 0),
  ...
  datetime.datetime(1998, 1, 30, 9, 0),
  datetime.datetime(1998, 1, 31, 9, 0),
  datetime.datetime(1999, 1, 1, 9, 0),
  datetime.datetime(1999, 1, 2, 9, 0),
  ...
  datetime.datetime(1999, 1, 30, 9, 0),
  datetime.datetime(1999, 1, 31, 9, 0),
  datetime.datetime(2000, 1, 1, 9, 0),
  datetime.datetime(2000, 1, 2, 9, 0),
  ...
  datetime.datetime(2000, 1, 30, 9, 0),
  datetime.datetime(2000, 1, 31, 9, 0)]

Same thing, in another way.

 >>> list(rrule(DAILY, bymonth=1,
 ...            dtstart=parse("19980101T090000"),
 ...            until=parse("20000131T090000")))
 [datetime.datetime(1998, 1, 1, 9, 0),
  ...
  datetime.datetime(2000, 1, 31, 9, 0)]

Weekly for 10 occurrences.

 >>> list(rrule(WEEKLY, count=10,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 9, 9, 0),
  datetime.datetime(1997, 9, 16, 9, 0),
  datetime.datetime(1997, 9, 23, 9, 0),
  datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 10, 7, 9, 0),
  datetime.datetime(1997, 10, 14, 9, 0),
  datetime.datetime(1997, 10, 21, 9, 0),
  datetime.datetime(1997, 10, 28, 9, 0),
  datetime.datetime(1997, 11, 4, 9, 0)]

Every other week, 6 occurrences.

 >>> list(rrule(WEEKLY, interval=2, count=6,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 16, 9, 0),
  datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 10, 14, 9, 0),
  datetime.datetime(1997, 10, 28, 9, 0),
  datetime.datetime(1997, 11, 11, 9, 0)]

Weekly on Tuesday and Thursday for 5 weeks.

 >>> list(rrule(WEEKLY, count=10, wkst=SU, byweekday=(TU,TH),
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 9, 9, 9, 0),
  datetime.datetime(1997, 9, 11, 9, 0),
  datetime.datetime(1997, 9, 16, 9, 0),
  datetime.datetime(1997, 9, 18, 9, 0),
  datetime.datetime(1997, 9, 23, 9, 0),
  datetime.datetime(1997, 9, 25, 9, 0),
  datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0)]

Every other week on Tuesday and Thursday, for 8 occurrences.

 >>> list(rrule(WEEKLY, interval=2, count=8,
 ...            wkst=SU, byweekday=(TU,TH),
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 9, 16, 9, 0),
  datetime.datetime(1997, 9, 18, 9, 0),
  datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0),
  datetime.datetime(1997, 10, 14, 9, 0),
  datetime.datetime(1997, 10, 16, 9, 0)]

Monthly on the 1st Friday for ten occurrences.

 >>> list(rrule(MONTHLY, count=10, byweekday=FR(1),
 ...            dtstart=parse("19970905T090000")))
 [datetime.datetime(1997, 9, 5, 9, 0),
  datetime.datetime(1997, 10, 3, 9, 0),
  datetime.datetime(1997, 11, 7, 9, 0),
  datetime.datetime(1997, 12, 5, 9, 0),
  datetime.datetime(1998, 1, 2, 9, 0),
  datetime.datetime(1998, 2, 6, 9, 0),
  datetime.datetime(1998, 3, 6, 9, 0),
  datetime.datetime(1998, 4, 3, 9, 0),
  datetime.datetime(1998, 5, 1, 9, 0),
  datetime.datetime(1998, 6, 5, 9, 0)]

Every other month on the 1st and last Sunday of the month for 10 occurrences.

 >>> list(rrule(MONTHLY, interval=2, count=10,
 ...            byweekday=(SU(1), SU(-1)),
 ...            dtstart=parse("19970907T090000")))
 [datetime.datetime(1997, 9, 7, 9, 0),
  datetime.datetime(1997, 9, 28, 9, 0),
  datetime.datetime(1997, 11, 2, 9, 0),
  datetime.datetime(1997, 11, 30, 9, 0),
  datetime.datetime(1998, 1, 4, 9, 0),
  datetime.datetime(1998, 1, 25, 9, 0),
  datetime.datetime(1998, 3, 1, 9, 0),
  datetime.datetime(1998, 3, 29, 9, 0),
  datetime.datetime(1998, 5, 3, 9, 0),
  datetime.datetime(1998, 5, 31, 9, 0)]

Monthly on the second to last Monday of the month for 6 months.

 >>> list(rrule(MONTHLY, count=6, byweekday=MO(-2),
 ...            dtstart=parse("19970922T090000")))
 [datetime.datetime(1997, 9, 22, 9, 0),
  datetime.datetime(1997, 10, 20, 9, 0),
  datetime.datetime(1997, 11, 17, 9, 0),
  datetime.datetime(1997, 12, 22, 9, 0),
  datetime.datetime(1998, 1, 19, 9, 0),
  datetime.datetime(1998, 2, 16, 9, 0)]

Monthly on the third to the last day of the month, for 6 months.

 >>> list(rrule(MONTHLY, count=6, bymonthday=-3,
 ...            dtstart=parse("19970928T090000")))
 [datetime.datetime(1997, 9, 28, 9, 0),
  datetime.datetime(1997, 10, 29, 9, 0),
  datetime.datetime(1997, 11, 28, 9, 0),
  datetime.datetime(1997, 12, 29, 9, 0),
  datetime.datetime(1998, 1, 29, 9, 0),
  datetime.datetime(1998, 2, 26, 9, 0)]

Monthly on the 2nd and 15th of the month for 5 occurrences.

 >>> list(rrule(MONTHLY, count=5, bymonthday=(2,15),
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 15, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0),
  datetime.datetime(1997, 10, 15, 9, 0),
  datetime.datetime(1997, 11, 2, 9, 0)]

Monthly on the first and last day of the month for 3 occurrences.

 >>> list(rrule(MONTHLY, count=5, bymonthday=(-1,1,),
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 10, 1, 9, 0),
  datetime.datetime(1997, 10, 31, 9, 0),
  datetime.datetime(1997, 11, 1, 9, 0),
  datetime.datetime(1997, 11, 30, 9, 0)]

Every 18 months on the 10th thru 15th of the month for 10 occurrences.

 >>> list(rrule(MONTHLY, interval=18, count=10,
 ...            bymonthday=range(10,16),
 ...            dtstart=parse("19970910T090000")))
 [datetime.datetime(1997, 9, 10, 9, 0),
  datetime.datetime(1997, 9, 11, 9, 0),
  datetime.datetime(1997, 9, 12, 9, 0),
  datetime.datetime(1997, 9, 13, 9, 0),
  datetime.datetime(1997, 9, 14, 9, 0),
  datetime.datetime(1997, 9, 15, 9, 0),
  datetime.datetime(1999, 3, 10, 9, 0),
  datetime.datetime(1999, 3, 11, 9, 0),
  datetime.datetime(1999, 3, 12, 9, 0),
  datetime.datetime(1999, 3, 13, 9, 0)]

Every Tuesday, every other month, 6 occurrences.

 >>> list(rrule(MONTHLY, interval=2, count=6, byweekday=TU,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 9, 9, 0),
  datetime.datetime(1997, 9, 16, 9, 0),
  datetime.datetime(1997, 9, 23, 9, 0),
  datetime.datetime(1997, 9, 30, 9, 0),
  datetime.datetime(1997, 11, 4, 9, 0)]

Yearly in June and July for 4 occurrences.

 >>> list(rrule(YEARLY, count=4, bymonth=(6,7),
 ...            dtstart=parse("19970610T090000")))
 [datetime.datetime(1997, 6, 10, 9, 0),
  datetime.datetime(1997, 7, 10, 9, 0),
  datetime.datetime(1998, 6, 10, 9, 0),
  datetime.datetime(1998, 7, 10, 9, 0)]

Every 3rd year on the 1st, 100th and 200th day for 4 occurrences.

 >>> list(rrule(YEARLY, count=4, interval=3, byyearday=(1,100,200),
 ...            dtstart=parse("19970101T090000")))
 [datetime.datetime(1997, 1, 1, 9, 0),
  datetime.datetime(1997, 4, 10, 9, 0),
  datetime.datetime(1997, 7, 19, 9, 0),
  datetime.datetime(2000, 1, 1, 9, 0)]

Every 20th Monday of the year, 3 occurrences.

 >>> list(rrule(YEARLY, count=3, byweekday=MO(20),
 ...            dtstart=parse("19970519T090000")))
 [datetime.datetime(1997, 5, 19, 9, 0),
  datetime.datetime(1998, 5, 18, 9, 0),
  datetime.datetime(1999, 5, 17, 9, 0)]

Monday of week number 20 (where the default start of the week is Monday), 3 occurrences.

 >>> list(rrule(YEARLY, count=3, byweekno=20, byweekday=MO,
 ...            dtstart=parse("19970512T090000")))
 [datetime.datetime(1997, 5, 12, 9, 0),
  datetime.datetime(1998, 5, 11, 9, 0),
  datetime.datetime(1999, 5, 17, 9, 0)]

The week number 1 may be in the last year.

 >>> list(rrule(WEEKLY, count=3, byweekno=1, byweekday=MO,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 12, 29, 9, 0),
  datetime.datetime(1999, 1, 4, 9, 0),
  datetime.datetime(2000, 1, 3, 9, 0)]

And the week numbers greater than 51 may be in the next year.

 >>> list(rrule(WEEKLY, count=3, byweekno=52, byweekday=SU,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 12, 28, 9, 0),
  datetime.datetime(1998, 12, 27, 9, 0),
  datetime.datetime(2000, 1, 2, 9, 0)]

Only some years have week number 53:

 >>> list(rrule(WEEKLY, count=3, byweekno=53, byweekday=MO,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1998, 12, 28, 9, 0),
  datetime.datetime(2004, 12, 27, 9, 0),
  datetime.datetime(2009, 12, 28, 9, 0)]

Every Friday the 13th, 4 occurrences.

 >>> list(rrule(YEARLY, count=4, byweekday=FR, bymonthday=13,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1998, 2, 13, 9, 0),
  datetime.datetime(1998, 3, 13, 9, 0),
  datetime.datetime(1998, 11, 13, 9, 0),
  datetime.datetime(1999, 8, 13, 9, 0)]

Every four years, the first Tuesday after a Monday in November, 3 occurrences (U.S. Presidential Election day):

 >>> list(rrule(YEARLY, interval=4, count=3, bymonth=11,
 ...            byweekday=TU, bymonthday=(2,3,4,5,6,7,8),
 ...            dtstart=parse("19961105T090000")))
 [datetime.datetime(1996, 11, 5, 9, 0),
  datetime.datetime(2000, 11, 7, 9, 0),
  datetime.datetime(2004, 11, 2, 9, 0)]

The 3rd instance into the month of one of Tuesday, Wednesday or Thursday, for the next 3 months:

 >>> list(rrule(MONTHLY, count=3, byweekday=(TU,WE,TH),
 ...            bysetpos=3, dtstart=parse("19970904T090000")))
 [datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 10, 7, 9, 0),
  datetime.datetime(1997, 11, 6, 9, 0)]

The 2nd to last weekday of the month, 3 occurrences.

 >>> list(rrule(MONTHLY, count=3, byweekday=(MO,TU,WE,TH,FR),
 ...            bysetpos=-2, dtstart=parse("19970929T090000")))
 [datetime.datetime(1997, 9, 29, 9, 0),
  datetime.datetime(1997, 10, 30, 9, 0),
  datetime.datetime(1997, 11, 27, 9, 0)]

Every 3 hours from 9:00 AM to 5:00 PM on a specific day.

 >>> list(rrule(HOURLY, interval=3,
 ...            dtstart=parse("19970902T090000"),
 ...            until=parse("19970902T170000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 2, 12, 0),
  datetime.datetime(1997, 9, 2, 15, 0)]

Every 15 minutes for 6 occurrences.

 >>> list(rrule(MINUTELY, interval=15, count=6,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 2, 9, 15),
  datetime.datetime(1997, 9, 2, 9, 30),
  datetime.datetime(1997, 9, 2, 9, 45),
  datetime.datetime(1997, 9, 2, 10, 0),
  datetime.datetime(1997, 9, 2, 10, 15)]

Every hour and a half for 4 occurrences.

 >>> list(rrule(MINUTELY, interval=90, count=4,
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 2, 10, 30),
  datetime.datetime(1997, 9, 2, 12, 0),
  datetime.datetime(1997, 9, 2, 13, 30)]

Every 20 minutes from 9:00 AM to 4:40 PM for two days.

 >>> list(rrule(MINUTELY, interval=20, count=48,
 ...            byhour=range(9,17), byminute=(0,20,40),
 ...            dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 2, 9, 20),
  ...
  datetime.datetime(1997, 9, 2, 16, 20),
  datetime.datetime(1997, 9, 2, 16, 40),
  datetime.datetime(1997, 9, 3, 9, 0),
  datetime.datetime(1997, 9, 3, 9, 20),
  ...
  datetime.datetime(1997, 9, 3, 16, 20),
  datetime.datetime(1997, 9, 3, 16, 40)]

An example where the days generated makes a difference because of wkst.

 >>> list(rrule(WEEKLY, interval=2, count=4,
 ...            byweekday=(TU,SU), wkst=MO,
 ...            dtstart=parse("19970805T090000")))
 [datetime.datetime(1997, 8, 5, 9, 0),
  datetime.datetime(1997, 8, 10, 9, 0),
  datetime.datetime(1997, 8, 19, 9, 0),
  datetime.datetime(1997, 8, 24, 9, 0)]

 >>> list(rrule(WEEKLY, interval=2, count=4,
 ...            byweekday=(TU,SU), wkst=SU,
 ...            dtstart=parse("19970805T090000")))
 [datetime.datetime(1997, 8, 5, 9, 0),
  datetime.datetime(1997, 8, 17, 9, 0),
  datetime.datetime(1997, 8, 19, 9, 0),
  datetime.datetime(1997, 8, 31, 9, 0)]

rruleset examples

Daily, for 7 days, jumping Saturday and Sunday occurrences.

 >>> set = rruleset()
 >>> set.rrule(rrule(DAILY, count=7,
 ...                 dtstart=parse("19970902T090000")))
 >>> set.exrule(rrule(YEARLY, byweekday=(SA,SU),
 ...                  dtstart=parse("19970902T090000")))
 >>> list(set)
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 3, 9, 0),
  datetime.datetime(1997, 9, 4, 9, 0),
  datetime.datetime(1997, 9, 5, 9, 0),
  datetime.datetime(1997, 9, 8, 9, 0)]

Weekly, for 4 weeks, plus one time on day 7, and not on day 16.

 >>> set = rruleset()
 >>> set.rrule(rrule(WEEKLY, count=4,
 ...           dtstart=parse("19970902T090000")))
 >>> set.rdate(datetime.datetime(1997, 9, 7, 9, 0))
 >>> set.exdate(datetime.datetime(1997, 9, 16, 9, 0))
 >>> list(set)
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 7, 9, 0),
  datetime.datetime(1997, 9, 9, 9, 0),
  datetime.datetime(1997, 9, 23, 9, 0)]

rrulestr() examples

Every 10 days, 5 occurrences.

 >>> list(rrulestr("""
 ... DTSTART:19970902T090000
 ... RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5
 ... """))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 12, 9, 0),
  datetime.datetime(1997, 9, 22, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0),
  datetime.datetime(1997, 10, 12, 9, 0)]

Same thing, but passing only the RRULE value.

 >>> list(rrulestr("FREQ=DAILY;INTERVAL=10;COUNT=5",
 ...               dtstart=parse("19970902T090000")))
 [datetime.datetime(1997, 9, 2, 9, 0),
  datetime.datetime(1997, 9, 12, 9, 0),
  datetime.datetime(1997, 9, 22, 9, 0),
  datetime.datetime(1997, 10, 2, 9, 0),
  datetime.datetime(1997, 10, 12, 9, 0)]

Notice that when using a single rule, it returns an rrule instance, unless forceset was used.

 >>> rrulestr("FREQ=DAILY;INTERVAL=10;COUNT=5")
 <dateutil.rrule.rrule object at 0x...>

 >>> rrulestr("""
 ... DTSTART:19970902T090000
 ... RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5
 ... """)
 <dateutil.rrule.rrule object at 0x...>

 >>> rrulestr("FREQ=DAILY;INTERVAL=10;COUNT=5", forceset=True)
 <dateutil.rrule.rruleset object at 0x...>

But when an rruleset is needed, it is automatically used.

 >>> rrulestr("""
 ... DTSTART:19970902T090000
 ... RRULE:FREQ=DAILY;INTERVAL=10;COUNT=5
 ... RRULE:FREQ=DAILY;INTERVAL=5;COUNT=3
 ... """)
 <dateutil.rrule.rruleset object at 0x...>