tags:

views:

237

answers:

3

How can I generate recurring dates using Python? For example I want to generate recurring date for "Third Friday of every second month". I want to generate recurring dates for daily, weekly, monthly, yearly (i.e., same as the recurrence function in Outlook Express).

A: 

You can give dateutil a try - especially its relativedelta and rrule fetures.

Tomasz Zielinski
I use the following code:print list(rrule(DAILY, count=10,dtstart=parse("19/02/2010")))The out put is looks like:[datetime.datetime(2010, 2, 19, 0, 0), datetime.datetime(2010, 2, 20, 0, 0), datetime.datetime(2010, 2, 21, 0, 0), datetime.datetime(2010, 2, 22, 0, 0), datetime.datetime(2010, 2, 23, 0, 0), datetime.datetime(2010, 2, 24, 0, 0), datetime.datetime(2010, 2, 25, 0, 0), datetime.datetime(2010, 2, 26, 0, 0), datetime.datetime(2010, 2, 27, 0, 0), datetime.datetime(2010, 2, 28, 0, 0)]But i want the output like:19/02/201020/02/201021/02/2010...Is it possible to change?
Nimmy
could edit the question to include this code so it can be read?
pwdyson
@nimmyliji: so you're not asking about recurring dates, but about date formatting.
Tomasz Zielinski
No. not like that..... I want to know about recurring date and also the date formatting. you told me to try the dateutil module..... After that only i tried to use dateutil
Nimmy
So please update the question with dateutil problem and update tags accordingly.
Tomasz Zielinski
+3  A: 
import dateutil.rrule as dr
import dateutil.parser as dp
import dateutil.relativedelta as drel

start=dp.parse("19/02/2010")   # Third Friday in Feb 2010

This generates the third Friday of every month

rr = dr.rrule(dr.MONTHLY,byweekday=drel.FR(3),dtstart=start, count=10)

This prints every third Friday:

print map(str,rr)
# ['2010-02-19 00:00:00', '2010-03-19 00:00:00', '2010-04-16 00:00:00', '2010-05-21 00:00:00', '2010-06-18 00:00:00', '2010-07-16 00:00:00', '2010-08-20 00:00:00', '2010-09-17 00:00:00', '2010-10-15 00:00:00', '2010-11-19 00:00:00']

rr is an iterable, so you can use slicing notation to pick out every other item. This prints the third Friday of every other month:

print map(str,rr[::2])
# ['2010-02-19 00:00:00', '2010-04-16 00:00:00', '2010-06-18 00:00:00', '2010-08-20 00:00:00', '2010-10-15 00:00:00']

Above, I used str to prettify the output a little bit. For more flexible string formatting of dates, use strftime: See http://au2.php.net/strftime or the man page for strftime for all the options.

print [d.strftime('%d/%m/%Y') for d in rr[::2]]
# ['19/02/2010', '16/04/2010', '18/06/2010', '20/08/2010', '15/10/2010']
unutbu
Thanks a lot......
Nimmy
+1 because I shamelessly used your excellent example to answer this very similar SO question: http://stackoverflow.com/questions/3099007/date-library-capable-of-calculating-things-like-every-third-tuesday
Adam Bernier
A: 

you may try to write this yourself. you will first need an iterator which generates dates separated by a given interval:

import datetime

def dateiter(start, resolution):
    date = start
    while True:
            yield date
            date += resolution

now, you can generate dates and filter them:

# generate a list of every tuesday of february
# this iterates over every day from now, and filtered according to the rules
# warning: infinite generator below, there is nothing to end the iteration
tuesdays_of_february = (date for date in dateiter(datetime.datetime.now(), datetime.timedelta(days=1)) if date.weekday() == 4 and date.month == 2)

you can call the iterator yourself until you have enough dates:

>>> next(tuesdays_of_february)
datetime.datetime(2010, 2, 19, 14, 25, 46, 171000)

now, you need to limit the results:

>>> from itertools import *
>>> 
>>> # get the five next valid dates:
>>> list(islice(tuesdays_of_february),5)
[datetime.datetime(2010, 2,26, 14, 25, 46, 171000), datetime.datetime(2011, 2, 4
, 14, 25, 46, 171000), datetime.datetime(2011, 2, 11, 14, 25, 46, 171000), datet
ime.datetime(2011, 2, 18, 1    4, 25, 46, 171000), datetime.datetime(2011, 2, 25
, 14, 25, 46, 171000)]
>>> 
>>> # or until a condition is met:
>>> list(takewhile( lambda date: date.year < 2014, tuesdays_of_february ))
[datetime.datetime(2012, 2, 3, 14, 25, 46, 171000), datetime.datetime(2012, 2, 1
0, 14, 25, 46, 171000), datetime.datetime(2012, 2, 17, 14, 25, 46, 171000), date
time.datetime(2012, 2, 24, 14, 25, 46, 171000), datetime.datetime(2013, 2, 1, 14
, 25, 46, 171000), datetime.datetime(2013, 2, 8, 14, 25, 46, 171000), datetime.d
atetime(2013, 2, 15, 14, 25, 46, 171000), datetime.datetime(2013, 2, 22, 14, 25,
 46, 171000)]

don't forget to have a look at the documentation for the datetime module.

Adrien Plisson