将营业日添加到日期字段

时间:2013-02-13 02:28:51

标签: python mysql django date models

我想在Django的日期字段中添加一些工作日。这是针对不同产品的不同交货时间的产品订购,我们希望为每种产品生成目标日期。

例如,产品X可能需要10个工作日才能交付,如果此产品在星期五日期01/03/2013订购,则目标日期最终应该是星期五15/03/2013(它将订购日期排除为一天)。

我已经浏览了一下,似乎有一些python库可以做到这一点,但这一切看起来都过于复杂。我认为有一种相当简单的方法可以做到这一点。

**编辑**

我现在正在使用BusinessHours PyPi包,但似乎遇到了一些问题。

他们举例说明了如何调用包。

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()

使用此信息,我制作了以下测试脚本

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()

我的假日文件包含以下内容(仅在2月份的随机工作日)

07-02-2013

当我运行脚本时,我收到以下错误。

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

这是包中的代码部分。

 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;

我感谢任何人给予我的帮助,我的Python版本是2.4.3

1 个答案:

答案 0 :(得分:0)

你看过这个问题吗?我相信它可以用几行代码回答你的问题。

python-count-days-ignoring-weekends