1.11M Members

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
'''``````
Tony Veijalainen

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

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
'''``````
Isn't it about time forums rewarded their contributors?

Earn rewards points for helping others. Gain kudos. Cash out. Get better answers yourself.

It's as simple as contributing editorial or replying to discussions labeled or OP Kudos

You
This is an OP Kudos discussion and contributors may be rewarded
Post:
View similar articles that have also been tagged: