0

I am creating my own .ics parser.

I am using icalendar python module. It works great but I would like to get list of datetimes for events which have RRULE set.

I have starting date as datetime object instance and RRULE parsed like this:

CaselessDict({'FREQ': ['MONTHLY'], 'INTERVAL': [1], 'BYDAY': ['4TH']})

But I cannot figure out how to make a list of datetimes from these two things.

Thank you

Blaskovic
  • 366
  • 4
  • 20

1 Answers1

0

You can use the python-dateutil library for generating rrules, eg:

from dateutil.rrule import rrule, MONTHLY

dts = list(rrule(MONTHLY, interval=10, byweekday=4, count=3))
# [datetime.datetime(2013, 11, 29, 15, 44, 45), datetime.datetime(2014, 9, 5, 15, 44, 45), datetime.datetime(2014, 9, 12, 15, 44, 45)]

Adjust arguments as needed.

Jon Clements
  • 138,671
  • 33
  • 247
  • 280
  • So I have to cover all possibilities with some IFs and others? There can be a lot of them. – Blaskovic Nov 27 '13 at 16:21
  • @Blaskovic why would you want to do that... You can pass in the variables from the dictionary that you have eg: `interval=your_CaselessDict['INTERVAL']` etc... You might have to create a mapping for the frequency though... – Jon Clements Nov 27 '13 at 16:27
  • Because you can get something like this: WKST=SU;BYDAY=TU,TH See: http://www.kanzaki.com/docs/ical/rrule.html – Blaskovic Nov 27 '13 at 16:32
  • 1
    @Blaskovic Okay.. so make sure you've got the constants imported `from dateutil.rrule import MO, TU, WE, TH, FR, SA, SU` etc... and you map `TU` as text to the TU variable... so it ends up as `byweekday=[TU,TH]` etc... – Jon Clements Nov 27 '13 at 16:34