from datetime import date, timedelta

# started from the code of Casey Webster at
# http://groups.google.com/group/comp.lang.python/browse_thread/thread/ddd39a02644540b7

# Define the weekday mnemonics to match the date.weekday function
(MON, TUE, WED, THU, FRI, SAT, SUN) = range(7)
weekends=(SAT,SUN)


def networkdays(start_date, end_date, holidays=[]):
    delta_days = (end_date - start_date).days + 1
    full_weeks, extra_days = divmod(delta_days, 7)
    # num_workdays = how many days/week you work * total # of weeks
    num_workdays = (full_weeks + 1) * (7 - len(weekends))
    # subtract out any working days that fall in the 'shortened week'
    for d in range(1, 8 - extra_days):
        if (end_date + timedelta(d)).weekday() not in weekends:
             num_workdays -= 1
    # skip holidays that fall on weekends
    holidays =  [x for x in holidays if x.weekday() not in weekends]
    # subtract out any holidays 
    for d in holidays:
        if d >= start_date and d <= end_date:
            num_workdays -= 1
    return num_workdays

def _in_between(a,b,x):
    #return cmp(a,x) * cmp(x,b) > 0
    return a <= x and x <= b or b <= x and x <= a


def workday(start_date,days, holidays=[]):
    full_weeks, extra_days = divmod(days,7 - len(weekends))
    new_date = start_date + timedelta(weeks=full_weeks)
    for i in range(extra_days):
        new_date += timedelta(days=1)
        while new_date.weekday() in weekends:
            new_date += timedelta(days=1)
    delta = timedelta(days=1 * cmp(days,0))
    # skip holidays that fall on weekends
    holidays =  [x for x in holidays if x.weekday() not in weekends ]
    holidays =  [x for x in holidays if x <> start_date ]
    for d in sorted(holidays, reverse = (days < 0)):
        # if d in between start and current push it out one working day
        if _in_between(start_date, new_date, d):
            new_date += delta
            while new_date.weekday() in weekends:
                new_date += delta
    return new_date


@@ -1184,20 +1184,23 @@
 rt_date,
+ 
 days
+=0
 , holida
@@ -1477,24 +1477,191 @@
 lta(days=1)%0A
+    # to account for days=0 case%0A    while new_date.weekday() in weekends:%0A        new_date += timedelta(days=1)%0A%0A    # avoid this if no holidays%0A    if holidays:%0A    
     delta = 
@@ -1692,16 +1692,20 @@
 ays,0))%0A
+    
     # sk
@@ -1730,32 +1730,36 @@
 all on weekends%0A
+    
     holidays =  
@@ -1812,24 +1812,28 @@
 kends %5D%0A    
+   
+ 
 holidays =  
@@ -1874,16 +1874,20 @@
 _date %5D%0A
+    
     for 
@@ -1939,16 +1939,20 @@
         
+  
+  
 # if d i
@@ -2007,16 +2007,20 @@
 ing day%0A
+    
         
@@ -2064,32 +2064,36 @@
 d):%0A            
+    
 new_date += delt
@@ -2090,24 +2090,28 @@
 te += delta%0A
+    
             
@@ -2140,32 +2140,36 @@
 () in weekends:%0A
+    
                 

update workdays.py