Polish on workday/weekend offset

svn: r14164
This commit is contained in:
Doug Blank 2010-01-29 14:27:25 +00:00
parent dd4be4b0e5
commit ba553eeba6

View File

@ -300,8 +300,8 @@ class _Holidays:
MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun',
'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
WORKDAY = ['mon', 'tue', 'wed', 'thu', 'fri'] WORKDAY = range(5) # indexes into above
WEEKEND = ['sat', 'sun'] WEEKEND = (5, 6) # indexes into above
def __init__(self, elements, country="US"): def __init__(self, elements, country="US"):
self.debug = 0 self.debug = 0
self.elements = elements self.elements = elements
@ -388,7 +388,7 @@ class _Holidays:
m = int(mon) m = int(mon)
elif mon == "*": elif mon == "*":
m = date.month m = date.month
else: elif mon in self.MONTHS:
m = self.MONTHS.index(mon) + 1 m = self.MONTHS.index(mon) + 1
dates_of_dayname = self.get_daynames(y, m, dayname) dates_of_dayname = self.get_daynames(y, m, dayname)
@ -426,14 +426,19 @@ class _Holidays:
if offset[0] == "-": if offset[0] == "-":
direction = -1 direction = -1
offset = offset[1:] offset = offset[1:]
elif offset[0] == "+":
direction = 1
offset = offset[1:]
if offset == "workday": if offset == "workday":
# next workday you come to, including this one
dow = self.WORKDAY dow = self.WORKDAY
ordinal = ndate.toordinal() ordinal = ndate.toordinal()
while ndate.fromordinal(ordinal).weekday() not in dow: while ndate.fromordinal(ordinal).weekday() not in dow:
ordinal += direction ordinal += direction
ndate = ndate.fromordinal(ordinal) ndate = ndate.fromordinal(ordinal)
elif offset == "weekend": elif offset == "weekend":
# next weekend you come to, including this one
dow = self.WEEKEND dow = self.WEEKEND
ordinal = ndate.toordinal() ordinal = ndate.toordinal()
while ndate.fromordinal(ordinal).weekday() not in dow: while ndate.fromordinal(ordinal).weekday() not in dow: