Skip to content

Instantly share code, notes, and snippets.

@jbking
Created May 30, 2011 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbking/998517 to your computer and use it in GitHub Desktop.
Save jbking/998517 to your computer and use it in GitHub Desktop.
Generate simple year month list
# -*- coding:utf-8 -*-
from datetime import date
class YearMonth(object):
""" List year/month from specified to today """
def __init__(self, year, month, format='%Y/%m'):
self.year = year
self.month = month
self.format = format
def __iter__(self):
""" Generate year/month list dynamically. """
specified_month = date(self.year, self.month, 1)
month = date.today().replace(day=1)
while month >= specified_month:
s = month.strftime(self.format)
yield (s, s)
y = month.year
m = month.month
if m == 1:
month = month.replace(year=(y - 1), month=12)
else:
month = month.replace(month=(m - 1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment