Compute working days

John Machin sjmachin at lexicon.net
Sat Mar 14 11:04:44 EDT 2009


On Mar 15, 12:13 am, Gonsolo <gons... at gmail.com> wrote:
> I found no solution on the net so I am posting my solution here.
> It can be called with "python cwd 1-1-2009 14-3-2009"
>
> from dateutil.rrule import *
> from dateutil.parser import *
> from datetime import *
> from sys import *

Hmmmm ... I wonder what the style police will have to say about that
little lot :-)

>
> start = parse( argv[1] )
> #end = datetime.now()
> end = parse( argv[2] )
> workdays = ( MO, TU, WE, TH, FR )
>
> r = rrule(DAILY, byweekday=workdays, dtstart = start, until = end)
> print len( list( r ) )

# Look, Ma, no 3rd party modules!
import datetime
for start in range(1, 8):
    print
    d1 = datetime.date(2009, 3, start)
    day1 = d1.toordinal()
    dow1 = (day1 - 1) % 7
    for delta in range(8):
        d2 = datetime.date(2009, 3, start + delta)
        day2 = d2.toordinal()
        dow2 = (day2 - 1) % 7
        workdays = (day2 + 7 - dow2 - day1 + dow1) // 7 * 5 - min
(dow1, 5) - max(4 - dow2, 0)
        print d1, d2, dow1, dow2, workdays
# Assumes both endpoints are included e.g. Mon 2 March to Tue 3 March
is 2 work-days.

HTH,
John



More information about the Python-list mailing list