Calculating days using Business Days for the Calendar

Discussion in 'Python' started by David Stockwell, Sep 20, 2004.

  1. I''m wondering if the Calendar object has an option to only do calculations
    based on business days (ie M-F). Additionally does it have a way to get
    holidays into itself?

    Currently I'm considering converting my calculations over to business days
    and am wondering if there is anything built-in. I've tried searching but so
    far haven't seen such a thing built in.

    I'm thinking I'm just going to have to bite the bullet and create my own
    approach to
    1) figure out the day of the week of my starting date (Calendar.weekday()
    should do the trick)
    2) determine the number of days up to my finish date (i do that now)
    3) determine the number of non-business days (weekends + holidays)
    4) subtract off the number in #3 from #2


    David
    -------
    Surf a wave to the future with a free tracfone
    http://cellphone.duneram.com/index.html

    _________________________________________________________________
    FREE pop-up blocking with the new MSN Toolbar – get it now!
    http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/
     
    David Stockwell, Sep 20, 2004
    #1
    1. Advertising

  2. David Stockwell

    Robin Becker Guest

    David Stockwell wrote:
    > I''m wondering if the Calendar object has an option to only do
    > calculations based on business days (ie M-F). Additionally does it have
    > a way to get holidays into itself?
    >
    > Currently I'm considering converting my calculations over to business
    > days and am wondering if there is anything built-in. I've tried
    > searching but so far haven't seen such a thing built in.
    >
    > I'm thinking I'm just going to have to bite the bullet and create my own
    > approach to
    > 1) figure out the day of the week of my starting date
    > (Calendar.weekday() should do the trick)
    > 2) determine the number of days up to my finish date (i do that now)
    > 3) determine the number of non-business days (weekends + holidays)
    > 4) subtract off the number in #3 from #2
    >
    >
    > David
    > -------
    > Surf a wave to the future with a free tracfone
    > http://cellphone.duneram.com/index.html
    >

    .... ReportLab's version of jeff Bauer's normalDate module has a BusinessDate
    object which does businessday diffs etc etc.

    --
    Robin Becker
     
    Robin Becker, Sep 20, 2004
    #2
    1. Advertising

  3. David Stockwell wrote:

    > I''m wondering if the Calendar object has an option to only do
    > calculations based on business days (ie M-F). Additionally does it have
    > a way to get holidays into itself?
    >
    > Currently I'm considering converting my calculations over to business
    > days and am wondering if there is anything built-in. I've tried
    > searching but so far haven't seen such a thing built in.
    >
    > I'm thinking I'm just going to have to bite the bullet and create my own
    > approach to
    > 1) figure out the day of the week of my starting date
    > (Calendar.weekday() should do the trick)
    > 2) determine the number of days up to my finish date (i do that now)
    > 3) determine the number of non-business days (weekends + holidays)
    > 4) subtract off the number in #3 from #2


    AFAIK - there is no built-in to calculate this. Part of the problem may
    be that the European work week often includes Saturdays.

    I created my own - in fact, that was my very first Python program. (That
    was before the new datetime and dateutil modules.) I've long since lost
    that script (left it on my work computer), but there's better ways to do
    it now anyway.

    Some notes on rolling your own:
    Be careful on determining the number of days in your step (2) above -
    you can easily end up with off-by-one errors.

    Also, you'll need to set up your own list (or dict) of holidays - they
    vary by region (and by union). I created a script for that so I could
    add to it, pickled it and imported it at need.

    dateutil is great and the documentation is pretty kewl - but has an
    unfortunate tendency toward "from module import *"... Other than that,
    it's a really kewl module - be sure to check out the date parser.

    Here's a snippet of code that does the job but would need to be prettied up:

    import datetime
    from dateutil import rrule

    alpha=datetime.date(2004, 01, 01) # change to accept input
    omega=datetime.date(2004, 02, 01) # change to accept input

    dates=rrule.rruleset() # create an rrule.rruleset instance

    dates.rrule(rrule.rrule(rrule.FREQ_DAILY, dtstart=alpha, until=omega))
    # this set is INCLUSIVE of alpha and omega

    dates.exrule(rrule.rrule(rrule.FREQ_DAILY,
    byweekday=(rrule.SA, rrule.SU),
    dtstart=alpha))
    # here's where we exclude the weekend dates

    print len(list(dates)) # there's probably a faster way to handle this

    HTH
    Anna
     
    Anna Martelli Ravenscroft, Sep 20, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerrit
    Replies:
    0
    Views:
    388
    Gerrit
    Sep 20, 2004
  2. Terry Reedy
    Replies:
    0
    Views:
    430
    Terry Reedy
    Sep 20, 2004
  3. clintonb
    Replies:
    9
    Views:
    887
    James Kanze
    May 23, 2007
  4. krishnakant Mane
    Replies:
    7
    Views:
    468
    Shane Geiger
    Oct 16, 2007
  5. Phrogz
    Replies:
    2
    Views:
    140
    Phrogz
    Apr 6, 2007
Loading...

Share This Page