Search code examples
pythonpython-datetimepython-dateutilrrule

python-dateutil - RRule - Different times for different weekdays


I'm using rrule as shown here: https://labix.org/python-dateutil#head-470fa22b2db72000d7abe698a5783a46b0731b57

I'm wondering if it somehow possible to create a rule where different times are specified for different weekdays e.g. WEEKLY Thursday 6pm and Saturday 10am

Hope someone can help :)


Solution

  • A single rrule can not specify both pairs of days and hours, but you could use a rrule.ruleset to combine rrules:

    import datetime as DT
    import dateutil.rrule as RR
    today = DT.date.today()
    
    aset = RR.rruleset()
    aset.rrule(RR.rrule(RR.WEEKLY, byweekday=RR.TH, byhour=18, count=3, dtstart=today))
    aset.rrule(RR.rrule(RR.WEEKLY, byweekday=RR.SA, byhour=10, count=3, dtstart=today))
    for date in aset:
        print(date)
    

    yields

    2015-03-26 18:00:00
    2015-03-28 10:00:00
    2015-04-02 18:00:00
    2015-04-04 10:00:00
    2015-04-09 18:00:00
    2015-04-11 10:00:00