【问题标题】:Adding Business Days to Date Field将工作日添加到日期字段
【发布时间】:2013-01-28 12:32:21
【问题描述】:

我正在尝试将多个工作日添加到 Django 中的日期字段。这适用于产品订购,我们对不同产品有不同的交货时间,我们希望为每种产品生成一个目标日期。

例如,产品 X 可能需要 10 个工作日才能交付,如果此产品是在 2013 年 1 月 3 日星期五订购的,则目标日期最终应该是 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()

我的假期文件包含以下内容(只是 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

【问题讨论】:

  • 您的问题可能比您意识到的要大得多。工作日不仅因周末而异(可能并不总是 2 天),而且您还必须考虑到国家和宗教节日(也会有所不同)。如果您从事国际业务,情况会变得更加复杂:发货端的周末/节假日、运输途中的周末/节假日(例如海关和航运公司),最后是收货端的周末/节假日。
  • 嗨彼得,幸运的是,这是一个小项目,我不会接触海外市场等:所以我只会在当地日期和节假日工作。假设为简单起见,我可以在交货时间上增加一两天的缓冲时间,以防万一某些公共假期到来,这会使我的任务更容易吗?

标签: python mysql django date models


【解决方案1】:

你检查过这个问题吗?我相信它可能会用几行代码回答你的问题。

python-count-days-ignoring-weekends

【讨论】:

  • 嗨 Ramces,感谢您提供的链接,我在搜索时没有找到该问题。我认为我可能已经得到了一些我想要的东西,虽然我猜它非常笨重并且可以重写得更好,但我仍然是 Python 的一个菜鸟。这就是我想出的(有问题的更新)。也许您会推荐一种更好的方法?
  • 嗯,它在我的一个例子中工作,但其中一些似乎没有工作,所以我可能在某个地方做错了。
  • 另外我不太确定我怎么能在公共假期工作到这个=\
  • 嗨,baz00r,从阅读您早期的 cmets 后,我以为您不关心假期。据我所知,没有办法在 Python 中原生地做到这一点。我做了一些挖掘,在 PyPi 上发现了这个库 BusinessHours。您所要做的就是在单独的文件中设置您的公司不工作/发货的假期。它可以为您计算工作日,而且看起来很简单。希望这会有所帮助。
  • 嗨 Ramces,我的首要任务是让它在周末工作,现在这似乎工作正常,我将在公共假期试一试。我在想我可以创建一个带有日期字段的模型,我们可以手动输入日期字段并检查这些日期是否在日期范围内(从日期到今天)。知道我该怎么做,我敢肯定 datetime 库中有一些东西可以做到这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-05
  • 2015-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多