def get_date_range(start, end, workdays=None, holidays=None, skip_non_workdays=True):
"""
This function calculates the durations between 2 dates skipping non workdays and holidays as specified
:rtype : tuple
:param start: date
:param end: date
:param workdays: string [Comma Separated Values, 0 - Monday through to 6 - Sunday e.g "0,1,2,3,4"]
:param holidays: list
:param skip_non_workdays: boolean
:return:
"""
from datetime import timedelta
duration = 0
# define workdays
if workdays is None:
workdays = [0, 1, 2, 3, 4]
else:
workdays = workdays.split(",")
# check if we need to skip non workdays
if skip_non_workdays is False:
workdays = [0, 1, 2, 3, 4, 5, 6]
# validate dates
if end < start:
return False, "End date is before start date"
# now its time for us to iterate
i = start
while i <= end:
# first let's give benefit of the doubt
incr = True
# lets see if day is in the workday array if not then fault it's existence here
try:
workdays.index(i.weekday())
except ValueError:
incr = False
# lets check if day is an holiday, charge guilty if so.
# We are checking the index in holiday array
try:
holidays.index(i)
incr = False
except (ValueError, AttributeError):
pass
if incr:
duration += 1
print "This day passed the criterion %s" % i
i += timedelta(1)
return True, duration
休日のため、この質問を参照してください。http://stackoverflow.com/questions/1986207/holiday-calendars-fileを-formats-et-al –