1

I need to create RRULE string with BYDAY parameter, from my date value.

Is there any natural way to do it?

I wrote this utility code for this purpose:

import calendar

fweek_fday, mdays = calendar.monthrange(date.year, date.month)
# Get weekday of first day and total days in current month
mweeks = ((mdays+fweek_fday-1)//7)+1
# Get total weeks in current month
mday, wday = date.day, date.weekday()
# Get current day of month and current day as day of a week
week_days = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
week = ((mday+fweek_fday-1)//7)+(1 if wday>=fweek_fday else 0)
# Get current week no
week = -1 if week == mweeks else week
wday = week_days[wday]

output = "BYDAY=%d%s" % (week, wday)
Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
Maxja
  • 435
  • 1
  • 4
  • 14
  • 1
    Have you tried using an iCalendar library instead of trying to generate/parse the format from scratch? A quick search at PyPI turns up many hits, starting with http://pypi.python.org/pypi/icalendar. – abarnert Oct 31 '12 at 20:57
  • Yep, i need to building iCalendar events. Then I'll send it to google calendar api. – Maxja Oct 31 '12 at 20:58
  • Have you looked at http://labix.org/python-dateutil ? (Except BYDAY is called BYWEEKDAY) – Jon Clements Nov 01 '12 at 12:27
  • If I'm right, _dateutil_ still does't have something like __ str __ function that can output all calculated data into string form. – Maxja Nov 01 '12 at 17:53

2 Answers2

1

as you said in comment there is no module that I've found yet to make a rule out from a set of constraints. should it be possible for you, you may consider the RDATE rather than going for the BYDAY.

another option for you would be:

import datetime
(y,w,d ) = date.isocalendar()
#w will be the iso week number, d the day of week
(y2,wb,d2) = datetime.datetime(date.year,date.month,1).isocalendar()
wkcount = 1 if d>=d2 else 0
# you need to account whether your date is a weekday after the one of the first of the month or not
print "BYDAY=%d%s"%(w-wb+wkcount,date.strftime("%a").upper()[0:2])

however be careful should your rule also include WKST

Auberon Vacher
  • 4,655
  • 1
  • 24
  • 36
0

The natural way to do this—unless you're writing an iCalendar library—is to use a pre-existing iCalendar library that has methods for dealing with recurrence rules, etc., as objects instead of doing string parsing and generation.

It's possible that there is no such library, in which case you're out of luck. But the first thing you should do is look for one.

A quick search at PyPI turns up many possibilities. The first one, iCalendar, is described as "a parser/generator of iCalendar files", which sounds like a good candidate. Skim the docs, or just pip install it and play with it, to see if it can do what you want.

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • My problem is that most of libs from that list required certain values for BYDAY, BYMONTH but I only have **date** when event starts first time, so they couldn't help me to calculate required values! I'm not finished this list yet… – Maxja Nov 01 '12 at 12:31