From ea76da9935602bdda57a8807e3c882585602a4ec Mon Sep 17 00:00:00 2001 From: juancms Date: Wed, 15 Jan 2020 23:06:52 +0000 Subject: [PATCH] :sparkles: workdays function was added to generate a list with the days between the start and end date --- test_all.py | 21 +++++++++++++++++++++ workdays.py | 11 +++++++++++ 2 files changed, 32 insertions(+) diff --git a/test_all.py b/test_all.py index c8cce74..29a374d 100644 --- a/test_all.py +++ b/test_all.py @@ -3,6 +3,7 @@ from datetime import date from workdays import workday from workdays import networkdays +from workdays import workdays def test_monday(): @@ -91,3 +92,23 @@ def test_networkdays(): weekends = (0,5,6) assert networkdays(date(2015, 8, 1), date(2015, 9, 30), holidays=holidays, weekends=weekends) == 34 + + +def test_workdays(): + start_date = date(2020, 1, 1) + + # test days length + days = workdays(start_date, 24) + assert len(list(days)) == 24 + + # test no include holidays + holidays = [date(2020, 1, 5), date(2020, 1, 20)] + days = list(workdays(start_date, 24, holidays)) + has_holidays = len(set(holidays) & set(days)) + assert has_holidays == 0 + + # test work saturday + # sundays not included + days = list(workdays(start_date, 24, weekends=(6,))) + sundays = [day for day in days if day.weekday() == 6] + assert len(sundays) == 0 diff --git a/workdays.py b/workdays.py index e2df868..1023b95 100644 --- a/workdays.py +++ b/workdays.py @@ -66,3 +66,14 @@ def workday(start_date, days=0, holidays=[], weekends=default_weekends): new_date += delta return new_date + +def workdays(start_date, days=0, holidays=[], weekends=default_weekends): + end_date = workday(start_date, days, holidays, weekends) + total_days = (end_date - start_date).days + + def is_valid(day): + return not ((day.weekday() in weekends) or (day in holidays)) + + days_list = [start_date + timedelta(days=day) for day in range(total_days)] + days = filter(lambda day: is_valid(day), days_list) + return iter(days)