Я написал следующий гадкий (как мне кажется) и громоздкий код:
#!/bin/env python
# -*- coding: utf-8 -*-
from calendar import monthrange
def date_range(date1, date2):
dates_list = []
if date1[0] != date2[0]:
year = date1[0]
month = date1[1]
if date2[0]-date1[0] == 1:
for day in xrange(date1[2], monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
for month in xrange(date1[1]+1, 13):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
year = date2[0]
for month in xrange(1, date2[1]):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
month = date2[1]
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
else:
for day in xrange(date1[2], monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
for month in xrange(date1[1]+1, 13):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
for year in xrange(date1[0]+1, date2[0]):
for month in xrange(1, 13):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
year = date2[0]
for month in xrange(1, date2[1]):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
month = date2[1]
for day in xrange(1, date2[2]+1):
dates_list.append([year, month, day])
else:
year = date1[0]
if date1[1] != date2[1]:
month = date1[1]
for day in xrange(date1[2], monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
if date2[1]-date1[1] == 1:
month = date2[1]
for day in xrange(1, date2[2]+1):
dates_list.append([year, month, day])
else:
for month in xrange(date1[1]+1, date2[1]):
for day in xrange(1, monthrange(year, month)[1]+1):
dates_list.append([year, month, day])
month = date2[1]
for day in xrange(1, date2[2]+1):
dates_list.append([year, month, day])
else:
month = date1[1]
for day in xrange(date1[2], date2[2]+1):
dates_list.append([year, month, day])
return dates_list
if __name__ == "__main__":
print date_range((2008, 10, 4), (2011, 3, 2))
Существует ли готовое решение? Если нет — подскажите где можно оптимизировать мой код.
Заранее благодарен.