如何计算我的例子中的小时数:
((完成 - 开始) - 暂停)
表单视图中的格式
start = 02/10/2017 07:55:54
finish = 02/10/2017 16:50:54
暂停= 1小时(默认)
在控制台日期时间看起来像这样:2017-02-10 07:55:54
需要这个==>结果= 07:55
How in @api.depends('start', 'finish')
def calculate_hours(self):
#
答案 0 :(得分:3)
您可以使用timedelta或相对delta来计算差异。在此之前,您必须转换为datetime对象。这就是我要做的事情
from openerp import api, fields
# if use relativedelta
from dateutil.relativedelta import relativedelta
@api.depends('start', 'finish')
def calculate_hours(self):
for record in self:
start_dt = fields.Datetime.from_string(record.start)
finish_dt = fields.Datetime.from_string(record.finish)
# using relativedelta
# pause time = 1, you could change to the number you desire.
pause = relativedelta(hours=1)
difference = relativedelta(finish_dt, start_dt) - pause
# from relative delta you could get hours or minutes as you wanted
# hours = difference.hours
# minutes = difference.minutes
# using timedelta
# pause = timedelta(seconds=3600)
# difference = finish_dt - start_dt - pause
# first element of timedelta is days, if you want to calculate the difference of days in hours, add difference.days * 24
# hours = int(difference.seconds/3600)
# minutes = (difference.seconds % 3600) / 60
# your code ....
希望我的回答符合您的要求。