1

I am trying to add a number of business days to a date field in Django. This is for product ordering where we have different lead times for different products and we want to generated a target date for each product.

for example, product X might take 10 business days to deliver, if this product is ordered on Friday date 01/03/2013 the target date should end up being the Friday 15/03/2013 (it excludes the ordering date as a day).

I have had a look around and there seems to be some python libraries to do this kinda stuff but it all seemed overly complicated. I thought that there would be a fairly simple way to do this.

** EDIT **

I am now using the BusinessHours PyPi package, however seem to be getting some problems.

They give you an example on how to call the package.

eg: Class:  BusinessHours(datetime1,datetime2,worktiming=[9 ,18],weekends=[6,7],holidayfile=None)

Class Parameters:
datetime1   - The datetime object with the starting date.
datetime2   - The datetime object with the ending date.
worktiming  - The working office hours . A list containing two values start time and end time
weekends    - The days in a week which have to be considered as weekends sent as a list, 1 for monday ...  7 for sunday.
holidayfile - A file consisting of the predetermined office holidays.Each date starts in a new line and currently must only be in the format dd-mm-yyyy

from BusinessHours import BusinessHours
from datetime import datetime
testing = BusinessHours(datetime(2007,10,15),datetime.now())
print testing.getdays()

Using this information I made the below test script

from BusinessHours import BusinessHours
from datetime import datetime
holidaytextfile = 'holidays.txt' 

testing = BusinessHours(datetime(2013,02,01),datetime(2013,02,28),worktiming=[9 ,18],weekends=[6,7],holidayfile=holidaytextfile)
print testing.getdays()

My holiday file contains the following (just a random business day in Feb)

07-02-2013

When I run the script I get the below error.

Traceback (most recent call last):
File "business_days", line 9, in ?
print testing.getdays()
File "/usr/lib/python2.4/site-packages/BusinessHours.py", line 63, in getdays
holidate = date(int(year) , int(month) , int(day))
NameError: global name 'date' is not defined

This is the section of code from the package.

 60             for definedholiday in definedholidays:
 61                 flag=0;
 62                 day , month , year = definedholiday.split('-')
 63                 holidate = date(int(year) , int(month) , int(day))
 64                 for weekend in self.weekends:
 65                      #check if mentioned holiday lies in defined weekend , shouldnt deduct twice
 66                     if(holidate.isoweekday==weekend):
 67                         flag=1;
 68                         break;

I appreciate any assistance anybody an give me, my Python version is 2.4.3

dlyxzen
  • 243
  • 7
  • 17
  • You may have a *much* bigger problem than you realize. Business days not only varies based on weekends (which may not always be 2 days long), but you also have to account for national and religious holidays (which also vary). And if you are doing international business, it gets even more complicated: weekends/holidays on the shipping end, weekends/holidays in transit (e.g. customs and shipping companies), and finally weekends/holidays at the receiving end. – Peter Rowell Feb 13 '13 at 02:33
  • Hi Peter,luckily this is a small project in which I will have no exposure to overseas markets etc: so I will only be working on local dates and holidays. Lets say for simplicity sake I could just add an extra day or two buffer to the lead times just in case some public holidays land in the block would that make my task easier? – dlyxzen Feb 13 '13 at 02:46

1 Answers1

0

Have you checked out this question? I believe it might answer yours with a few lines of code.

python-count-days-ignoring-weekends

Community
  • 1
  • 1
chirinosky
  • 4,438
  • 1
  • 28
  • 39
  • Hi Ramces, thanks for that link, I did not find that question when I was searching. I *think* I may have got something which does what I want, although I'm guessing its very clunky and could be re-written a lot nicer, I'm still a bit of a noob with Python. This is what I came up with (updated in question). Perhaps you would recomend a better way of doing it? – dlyxzen Feb 13 '13 at 04:31
  • Well it worked on one of my examples but a few of them it doesn't seem to work , so I am probably doing something wrong somewhere along the line. – dlyxzen Feb 13 '13 at 04:36
  • Plus I'm not too sure how I could work in public holidays into this =\ – dlyxzen Feb 13 '13 at 05:12
  • Hi baz00r, from reading your earlier comments I thought you didn't care about holidays. As far as I know, there isn't a way to do it natively within Python. I did some digging around and came across this library [BusinessHours](http://pypi.python.org/pypi/BusinessHours/) on PyPi. All you have to do is set the holidays your company doesn't work/ship on a separate file. It can calculate the business days for you and seems straightforward. Hope this helps. – chirinosky Feb 13 '13 at 17:07
  • Hi Ramces, My first priority was to get it working with weekends, now that seems to be working okay, I'm going to give it a go with public holidays. I was thinking I could create a model with a date field, and we could manually enter date fields and check if any of these dates fall in the date range (fromdate - todate). Any idea how I could do that, I'm sure there is something out of the datetime library that could do this. – dlyxzen Feb 13 '13 at 22:37
  • Also I am checking out the Pypi thing. I appreciate your help. – dlyxzen Feb 13 '13 at 23:04
  • I have downloaded and installed the BusinessHours package as per Ramces reccomendation, and I seem to have it working except for a issue with the date module, I think this may be due to my Python version perhaps (2.4.3) and the module being different. Not too sure. I have outlined the problem above. Any assistance is appreciated. – dlyxzen Feb 14 '13 at 02:40
  • Hi baz00r, to correct the NameError see where the `date` function is implemented in the package and import it into that module. – chirinosky Feb 16 '13 at 20:02