Pull Last 3 Months

Hyuga hyugaricdeau at gmail.com
Thu Oct 18 10:50:41 EDT 2007


On Oct 18, 12:25 am, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> I prefer the calendar module in that case:
>
> py> import locale
> py> locale.setlocale(locale.LC_ALL, '')
> 'Spanish_Argentina.1252'
> py>
> py> import calendar
> py> calendar.month_abbr[12]
> 'Dic'
> py> def prev_months(since, howmany):
> ...   return [calendar.month_abbr[(since.month-i-2) % 12 + 1] for i in
> range(how
> many)]
> ...
> py> import datetime
> py> prev_months(datetime.datetime(2005,2,10), 4)
> ['Ene', 'Dic', 'Nov', 'Oct']
> py> prev_months(datetime.datetime(2005,10,17), 3)
> ['Sep', 'Ago', 'Jul']

Ah, you beat me to it.  I was going to point out that if you're going
to be using month strings, you should use calendar, since it can also
use the correct locale.  Plus, it offers a ridiculously simple
solution to this problem compared to all the others.

Hyuga




More information about the Python-list mailing list