1,105,281 Community Members

First workday of month (except holidays)

Member Avatar
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
 
1
 

This code determines if datetime object represent first workday of month, but it does not consider the special days like New Year (this year weekend but last year Friday).

Reaction to Basic efforts in: http://www.daniweb.com/software-development/legacy-and-other-languages/threads/362098

There algorithm idea given by jon.kiparsky

based docstring says 'workday' when it should be 'weekday':

''' Determine first workday based on day of month and weekday (0 == Monday) '''
from datetime import datetime

def first_workday(theday):
    ''' Determine first workday based on day of month and workday (0 == Monday) '''
    return ((theday.day == 3 and theday.weekday() == 0) or
            (theday.day == 2 and theday.weekday() == 0) or
            (theday.day == 1 and theday.weekday() < 5))


for test in (datetime.today(), datetime(2011, 5, 2), datetime(2010, 5, 2), datetime(2011, 5, 1),
             datetime(2011, 4, 1), datetime(2011, 3, 1), datetime(2011, 1, 1), datetime(2012, 9, 1),
            datetime(2012, 9, 3), datetime(2010, 1, 1)): # wrong as New Year is Holiday
        print 'The day is' + ('' if first_workday(test) else ' not'), 'first workday:',  test.strftime('%Y/%m/%d, %A')
    
'''Output:
The day is not first workday: 2011/05/07, Saturday
The day is first workday: 2011/05/02, Monday
The day is not first workday: 2010/05/02, Sunday
The day is not first workday: 2011/05/01, Sunday
The day is first workday: 2011/04/01, Friday
The day is first workday: 2011/03/01, Tuesday
The day is not first workday: 2011/01/01, Saturday
The day is not first workday: 2012/09/01, Saturday
The day is first workday: 2012/09/03, Monday
The day is first workday: 2010/01/01, Friday
'''
Member Avatar
Tony Veijalainen

Specialties:
IT/Science/Contracts/Religious translation/interpreting FIN-ENG-FIN
Python programming

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 
Member Avatar
pyTony
pyMod
6,103 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

Using date and one less or branch (using in), if you feel five and/ors and six comparisons are too much:

from datetime import date

def isfirst_workday(theday):
    ''' Determine first workday based on day of month and weekday (0 == Monday) '''
    return ((theday.day in (2,3) and theday.weekday() == 0) or
            (theday.day == 1 and theday.weekday() < 5))

if __name__ == '__main__':
    for test in (date.today(), date(2011, 5, 2), date(2010, 5, 2), date(2011, 5, 1),
             date(2011, 4, 1), date(2011, 3, 1), date(2011, 1, 1), date(2012, 9, 1),
            date(2012, 9, 3), date(2010, 1, 1)): # wrong as New Year is Holiday
        print 'The day is' + ('' if isfirst_workday(test) else ' not'), 'first workday:',  test.strftime('%Y/%m/%d, %A')
    
'''Output:
The day is not first workday: 2011/05/07, Saturday
The day is first workday: 2011/05/02, Monday
The day is not first workday: 2010/05/02, Sunday
The day is not first workday: 2011/05/01, Sunday
The day is first workday: 2011/04/01, Friday
The day is first workday: 2011/03/01, Tuesday
The day is not first workday: 2011/01/01, Saturday
The day is not first workday: 2012/09/01, Saturday
The day is first workday: 2012/09/03, Monday
The day is first workday: 2010/01/01, Friday
'''
You
Post:
Start New Discussion
View similar articles that have also been tagged: