Calculate date 5 days from today, adding an extra day for each day in the next 5 days that is a weekend day
Question: I am testing using Robot Framework and need to create my own Python keyword.

Taking the current date as day 0 (tomorrow as day 1), I am trying to calculate
what the date will be 5 days from today. If any of the days in the next 5 days
is a Saturday I need to add an extra day to my calculation. Same if any of the
days is a Sunday.

As a Python beginner, I'm a little out of my depth so any help would be much
appreciated


Answer: Basically you need to add 5 business days... This should do it:

    
    
    import datetime
    def addBusinessDays(from_date, add_days):
        business_days_to_add = add_days
        current_date = from_date
        while business_days_to_add > 0:
            current_date += datetime.timedelta(days=1)
            weekday = current_date.weekday()
            if weekday >= 5: # sunday = 6
                continue
            business_days_to_add -= 1
        return current_date
    
    #demo:
    print '5 business days from today:'
    print addBusinessDays(datetime.date.today(), 5)
    

Update:

Here is the explanation:

  1. We get the start date(Date that we need to add business days to it)
  2. We use a loop to add days 1 at a time to the date(we use datetime.timedelta(days=1) to add 1 day to date)
  3. After adding each day we check to see if updated date is weekday. If it's weekday we count it otherwise we don't count it and continue

