在日期字段中增加工作日
我正在尝试在Django中给一个日期字段加上几个工作日。这是为了产品订购,因为不同的产品有不同的交货时间,我们想为每个产品生成一个目标日期。
举个例子,产品X可能需要10个工作日才能送达。如果这个产品在2013年3月1日的星期五被订购,那么目标日期应该是2013年3月15日的星期五(不算订购当天)。
我查了一下,发现有一些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()
我的假期文件包含以下内容(只是二月份的一个随机工作日)。
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。