Skip to content

Commit

Permalink
Add flexible days_between util function
Browse files Browse the repository at this point in the history
  • Loading branch information
OmeGak committed Jul 18, 2014
1 parent e6e4ae0 commit 77895f0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions indico/util/date_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,13 @@ def get_datetime_from_request(prefix='', default=None, source=None):
parsed_time = default.time()

return datetime.combine(parsed_date, parsed_time)


def days_between(start_date, end_date, include_weekends=True, inclusive=False):
if inclusive:
daterange = (end_date - start_date).days + 1
daygenerator = (start_date + timedelta(delta) for delta in xrange(daterange))
else:
daterange = (end_date - start_date).days - 1
daygenerator = (start_date + timedelta(delta + 1) for delta in xrange(daterange))
return daterange if include_weekends else sum(day.weekday() < 5 for day in daygenerator)

0 comments on commit 77895f0

Please sign in to comment.