Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from datetime import date
from workdays import workday
from workdays import networkdays
from workdays import workdays


def test_monday():
Expand Down Expand Up @@ -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
11 changes: 11 additions & 0 deletions workdays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)