솔루션 :
from functools import total_ordering
from datetime import timedelta, datetime
@total_ordering
class DateRange(object):
def __init__(self, start, end):
assert start <= end
self.start = start
self.end = end
def __contains__(self, other):
return self.start <= other and self.end >= other
def __lt__(self, other):
if self.start == other.start:
return self.end < other.end
return self.start < other.start
def __eq__(self, other):
return self.start == other.start and self.end == other.end
def __str__(self):
return '<%s, %s>' % (self.start.strftime('%Y-%m-%d'), self.end.strftime('%Y-%m-%d'))
def __iter__(self):
class DateIterator(object):
def __init__(self, start, end):
self.current = start
self.end = end
def next(self):
if self.current > self.end:
raise StopIteration()
self.current += timedelta(days=1)
return self.current
return DateIterator(self.start, self.end)
__repr__ = __str__
dates = [('2011-01-01', '2011-01-14'), ('2011-01-15','2011-01-31'), ('2011-02-01','2011-02-14'), ('2011-03-01','2011-03-14'), ('2011-03-16','2011-03-31')]
dates = [DateRange(datetime.strptime(start, '%Y-%m-%d'), datetime.strptime(end, '%Y-%m-%d')) for start, end in dates]
dates = sorted(dates)
missing = []
previous = None
for date_range in dates:
if previous is not None and previous < date_range.start:
missing.append(DateRange(previous, date_range.start + timedelta(days=-1)))
previous = date_range.end + timedelta(days=1)
print missing
는 같은 달 동안 일을 추가하고 실제 필요한 일에 conpare. – Rahul
출력을 정확히 보이게하려면 어떻게해야합니까? – timgeb
@timgeb 누락 날짜 목록으로 이상적으로 – Greg