Skip to content

Commit

Permalink
chore: format using 'yapf -r -i .'
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Dec 2, 2017
1 parent 5a044b6 commit ed95de2
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 181 deletions.
38 changes: 25 additions & 13 deletions businesstime/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import datetime


__version__ = "0.2.1"


Expand Down Expand Up @@ -53,7 +52,8 @@ def isbusinessday(self, dt):
return not self.isweekend(dt) and not self.isholiday(dt)

def isduringbusinesshours(self, dt):
return self.isbusinessday(dt) and self.business_hours[0] <= dt.time() < self.business_hours[1]
return self.isbusinessday(dt) and self.business_hours[0] <= dt.time(
) < self.business_hours[1]

def iterdays(self, d1, d2):
"""
Expand Down Expand Up @@ -97,20 +97,29 @@ def _build_spanning_datetimes(self, d1, d2):
if len(businessdays) == 0:
return businessdays

businessdays = [datetime.datetime.combine(d, self.business_hours[0]) for d in businessdays]
businessdays = [
datetime.datetime.combine(d, self.business_hours[0])
for d in businessdays
]

if d1 > businessdays[0]:
businessdays[0] = d1

if self.isbusinessday(d2) and d2 >= datetime.datetime.combine(d2, self.business_hours[0]):
businessdays.append(datetime.datetime.combine(d2, self.business_hours[1]))
if self.isbusinessday(d2) and d2 >= datetime.datetime.combine(
d2, self.business_hours[0]):
businessdays.append(
datetime.datetime.combine(d2, self.business_hours[1]))
if d2 < businessdays[-1]:
businessdays[-1] = datetime.datetime.combine(businessdays[-1], d2.time())
businessdays[-1] = datetime.datetime.combine(
businessdays[-1], d2.time())
else:
if len(businessdays) == 1:
businessdays.append(datetime.datetime.combine(businessdays[0], self.business_hours[1]))
businessdays.append(
datetime.datetime.combine(businessdays[0],
self.business_hours[1]))
else:
businessdays[-1] = datetime.datetime.combine(businessdays[-1], self.business_hours[1])
businessdays[-1] = datetime.datetime.combine(
businessdays[-1], self.business_hours[1])

return businessdays

Expand All @@ -130,14 +139,19 @@ def businesstimedelta(self, d1, d2):
if len(businessdays) == 0:
# HACK: manually handle the case when d1 is after business hours while d2 is during
if self.isduringbusinesshours(d2):
time += d2 - datetime.datetime.combine(d2, self.business_hours[0])
time += d2 - datetime.datetime.combine(d2,
self.business_hours[0])

# HACK: manually handle the case where d1 is on an earlier non-business day and d2 is after hours on a business day
elif not self.isbusinessday(d1) and self.isbusinessday(d2):
if d2.time() > self.business_hours[1]:
time += datetime.datetime.combine(d2,self.business_hours[1]) - datetime.datetime.combine(d2,self.business_hours[0])
time += datetime.datetime.combine(
d2,
self.business_hours[1]) - datetime.datetime.combine(
d2, self.business_hours[0])
elif d2.time() > self.business_hours[0]:
time += d2 - datetime.datetime.combine(d2, self.business_hours[0])
time += d2 - datetime.datetime.combine(
d2, self.business_hours[0])

else:
prev = None
Expand All @@ -162,8 +176,6 @@ def businesstimedelta(self, d1, d2):

return time * timedelta_direction



def businesstime_hours(self, d1, d2):
"""
Returns a datetime.timedelta of business hours between d1 and d2,
Expand Down
12 changes: 7 additions & 5 deletions businesstime/holidays/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,28 @@ class Holidays(object):

rules = []

def month_length(self, year,month):
return calendar.monthrange(year,month)[1]
def month_length(self, year, month):
return calendar.monthrange(year, month)[1]

def _day_rule_matches(self, rule, dt):
return dt.month == rule.get("month") and dt.day == rule.get("day")

def _weekday_rule_matches(self, rule, dt):
if dt.month == rule.get("month") and dt.weekday() == rule.get("weekday"):
if dt.month == rule.get("month") and dt.weekday() == rule.get(
"weekday"):
# Check for +week specification
if math.floor((dt.day - 1) / 7) == rule.get("week") - 1:
return True
# Check for -week specification
length = self.month_length(dt.year,dt.month)
length = self.month_length(dt.year, dt.month)
if math.floor((length - dt.day) / 7) + 1 == rule.get("week") * -1:
return True
return False

def isholiday(self, dt):
for r in self.rules:
if self._day_rule_matches(r, dt) or self._weekday_rule_matches(r, dt):
if self._day_rule_matches(r, dt) or self._weekday_rule_matches(
r, dt):
return True
return False

Expand Down
10 changes: 5 additions & 5 deletions businesstime/holidays/aus.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class QueenslandPublicHolidays(Holidays):
date(2015, 1, 1),
date(2016, 1, 1),
date(2017, 1, 1),
date(2017, 1, 2), # extra because 1 Jan is a Sunday
date(2017, 1, 2), # extra because 1 Jan is a Sunday
date(2018, 1, 1),

# Australia Day
Expand Down Expand Up @@ -91,15 +91,15 @@ class QueenslandPublicHolidays(Holidays):
date(2014, 12, 25),
date(2015, 12, 25),
date(2016, 12, 25),
date(2016, 12, 27), # extra because 25 Dec is a Sunday
date(2016, 12, 27), # extra because 25 Dec is a Sunday
date(2017, 12, 25),
date(2018, 12, 25),

# Boxing Day
date(2013, 12, 26),
date(2014, 12, 26),
date(2015, 12, 26),
date(2015, 12, 28), # extra because 26 Dec is a Sunday
date(2015, 12, 28), # extra because 26 Dec is a Sunday
date(2016, 12, 26),
date(2017, 12, 26),
date(2018, 12, 26),
Expand All @@ -108,8 +108,8 @@ class QueenslandPublicHolidays(Holidays):
def isholiday(self, dt):
if dt.year < self._coverage_start_year or dt.year > self._coverage_end_year:
raise NotImplementedError(
'QueenslandPublicHolidays only covers the years %s to %s'
% (self._coverage_start_year, self._coverage_end_year))
'QueenslandPublicHolidays only covers the years %s to %s' %
(self._coverage_start_year, self._coverage_end_year))
return any(holiday == dt for holiday in self.holidays)


Expand Down
6 changes: 5 additions & 1 deletion businesstime/holidays/usa.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ class USFederalHolidays(Holidays):

rules = [
dict(name="New Year's Day", month=1, day=1),
dict(name="Birthday of Martin Luther King, Jr.", month=1, weekday=0, week=3),
dict(
name="Birthday of Martin Luther King, Jr.",
month=1,
weekday=0,
week=3),
dict(name="Washington's Birthday", month=2, weekday=0, week=3),
dict(name="Memorial Day", month=5, weekday=0, week=-1),
dict(name="Independence Day", month=7, day=4),
Expand Down
Loading

0 comments on commit ed95de2

Please sign in to comment.