From ba553eeba6f5068b89a604dbfb975b13503cac68 Mon Sep 17 00:00:00 2001 From: Doug Blank Date: Fri, 29 Jan 2010 14:27:25 +0000 Subject: [PATCH] Polish on workday/weekend offset svn: r14164 --- src/plugins/lib/libholiday.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/plugins/lib/libholiday.py b/src/plugins/lib/libholiday.py index 034d582f5..c907cf7b0 100644 --- a/src/plugins/lib/libholiday.py +++ b/src/plugins/lib/libholiday.py @@ -300,8 +300,8 @@ class _Holidays: MONTHS = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'] DAYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'] - WORKDAY = ['mon', 'tue', 'wed', 'thu', 'fri'] - WEEKEND = ['sat', 'sun'] + WORKDAY = range(5) # indexes into above + WEEKEND = (5, 6) # indexes into above def __init__(self, elements, country="US"): self.debug = 0 self.elements = elements @@ -370,7 +370,7 @@ class _Holidays: else: # must be a dayname or "workday" offset = rule["offset"] - + if rule["value"].startswith('>'): # eval exp -> year/num[/day[/month]] y, m, d = date.year, date.month, date.day @@ -388,7 +388,7 @@ class _Holidays: m = int(mon) elif mon == "*": m = date.month - else: + elif mon in self.MONTHS: m = self.MONTHS.index(mon) + 1 dates_of_dayname = self.get_daynames(y, m, dayname) @@ -426,14 +426,19 @@ class _Holidays: if offset[0] == "-": direction = -1 offset = offset[1:] + elif offset[0] == "+": + direction = 1 + offset = offset[1:] if offset == "workday": + # next workday you come to, including this one dow = self.WORKDAY ordinal = ndate.toordinal() while ndate.fromordinal(ordinal).weekday() not in dow: ordinal += direction ndate = ndate.fromordinal(ordinal) elif offset == "weekend": + # next weekend you come to, including this one dow = self.WEEKEND ordinal = ndate.toordinal() while ndate.fromordinal(ordinal).weekday() not in dow: @@ -446,7 +451,7 @@ class _Holidays: while ndate.fromordinal(ordinal).weekday() != dow: ordinal += direction ndate = ndate.fromordinal(ordinal) - + if self.debug: print "ndate:", ndate, "date:", date