Skip to content

Commit

Permalink
Merge pull request seatgeek#21 from nested-tech/add_businesshours
Browse files Browse the repository at this point in the history
Add businesshours
  • Loading branch information
josegonzalez authored Nov 2, 2017
2 parents 9f432b8 + 970c881 commit 4bf570b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
13 changes: 11 additions & 2 deletions businesstime/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class BusinessTime(object):
business time aware timedeltas between two datetimes.
"""

def __init__(self, business_hours=None, weekends=(5,6), holidays=None):
def __init__(self, business_hours=None, weekends=(5, 6), holidays=None):
if business_hours is None:
business_hours = (datetime.time(9), datetime.time(17))
self.business_hours = business_hours
Expand Down Expand Up @@ -124,7 +124,7 @@ def businesstimedelta(self, d1, d2):
if d1 > d2:
d1, d2, timedelta_direction = d2, d1, -1
else:
timedelta_direction = 1
timedelta_direction = 1
businessdays = self._build_spanning_datetimes(d1, d2)
time = datetime.timedelta()

Expand Down Expand Up @@ -154,3 +154,12 @@ def businesstimedelta(self, d1, d2):
prev = current

return time * timedelta_direction

def businesstime_hours(self, d1, d2):
"""
Returns the number of business hours between d1 and d2, based on the length of the businessday
"""
open_hours = self.open_hours.seconds / 3600
btd = self.businesstimedelta(d1, d2)
btd_hours = btd.seconds / 3600
return btd.days * open_hours + btd_hours
24 changes: 24 additions & 0 deletions businesstime/test/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,27 @@ def test_businesstimedelta_during_during_reverse(self):
self.bt.businesstimedelta(start, end),
timedelta(hours=-7, minutes=-58)
)

def test_businesstime_hours_exactly_one_day(self):
start = datetime(2014, 1, 16, 9, 0)
end = datetime(2014, 1, 17, 9, 0)
self.assertEqual(
self.bt.businesstime_hours(start, end),
8
)

def test_businesstime_hours_one_day(self):
start = datetime(2014, 1, 16, 9, 0)
end = datetime(2014, 1, 17, 15, 0)
self.assertEqual(
self.bt.businesstime_hours(start, end),
14
)

def test_businesstime_hours_one_day_reverse(self):
start = datetime(2014, 1, 17, 9, 0)
end = datetime(2014, 1, 16, 9, 0)
self.assertEqual(
self.bt.businesstime_hours(start, end),
-8
)

0 comments on commit 4bf570b

Please sign in to comment.