这很有效。不是最快或最干净的解决方案,但易于理解,并且不使用任何外部库:#date_calculator.py
import datetime as dt
def add_working_days(start_date, added_days):
# data from https://www.redcort.com/us-federal-bank-holidays/
federal_holidays = [dt.date(2018, 1, 1), dt.date(2018, 1, 15),
dt.date(2018, 2, 19), dt.date(2018, 5, 28), dt.date(2018, 7, 4),
dt.date(2018, 9, 3), dt.date(2018, 10, 8), dt.date(2018, 11, 12),
dt.date(2018, 11, 22), dt.date(2018, 12, 25)]
days_elapsed = 0
while days_elapsed < added_days:
test_date = start_date+dt.timedelta(days=1)
start_date = test_date
if test_date.weekday()>4 or test_date in federal_holidays:
# if a weekend or federal holiday, skip
continue
else:
# if a workday, count as a day
days_elapsed += 1
return start_date
print(add_working_days(dt.date(2018, 1, 11), 10))
#> 2018-01-26
print(add_working_days(dt.date(2018, 1, 11), 115))
#> 2018-06-26