I'm in calendar hell, and I'm hoping there exists a Python module out there that does what I want.
I'm making a Python web app that deals with subscriptions. It's conceptually similar to a cell phone plan: You start your subscription on a certain date (say 1.13.2011
), and for every billing month you have a bunch of "sessions" (phone calls), that you would be charged for.
We need to:
For example, if you signed up on 1.13.2011
, and made a phone call on 1.20.2011
, it would count on your first billing month. Same for a phone call on 2.10.2011
. But if you were to make a phone call on 2.15.2011
, it will count on your second billing month.
Regarding start and end dates: If today is 2.15.2011
, then the start date of the current month is 2.13.2011
and its end date is 3.13.2011
.
You may be thinking this is not so complicated, but then you have to consider that months have different lengths. The rule for handling this is that if your subscription started on the 30th of whatever month, its cutoff dates on each month would be min(30, n_days_in_that_month)
. This goes for 29, 30 and 31 as well.
I tried coding this, but it got too complex. What I'm looking for is a ready-made, existing module that does these things.
For the love of God don't post an answer with a sketch of an implementation! This is useless for me. I appreciate your intentions, but in calendar hell, sketches of implementations do not help. I already have a sketch of an implementation, and debugging yours will take just as long as debugging mine.
I am only interested in using an existing module that handles such calendar tasks. Do you know one?
http://labix.org/python-dateutil
Ram's edit: The dateutil.rrule.rrule
class is the one that did exactly what I wanted.