如何在两个日期时间字段之间产生差异导致小时,odoo?

时间:2015-11-30 13:12:00

标签: python openerp

我想用两个日期时间字段来区分,结果是以小时为单位。我没有收到任何错误,但这不起作用。任何人都可以帮忙解决这个问题。

@api.one
@api.onchange('end_production_date')
def onchange_production_dates(self):
    begin_production_date=self.begin_production_date
    end_production_date=self.end_production_date
    production_duration=self.production_duration

    value = {}
    if not begin_production_date:
        return value
    if not end_production_date and not production_duration:
        duration = 1.00
        value[production_duration] = duration

    start = datetime.strptime(begin_production_date, "%Y-%m-%d %H:%M:%S")
    if end_production_date and not production_duration:
        end = datetime.strptime(end_production_date, "%Y-%m-%d %H:%M:%S")
        diff = end - start
        duration = float(diff.days)* 24 + (float(diff.seconds) / 3600)
        value[production_duration] = round(duration, 2)
    elif not end_production_date:
        end = start + timedelta(hours=production_duration)
        value[end_production_date] = end.strftime("%Y-%m-%d %H:%M:%S")
    elif end_production_date and production_duration:
        end = datetime.strptime(end_production_date, "%Y-%m-%d %H:%M:%S")
        diff = end - start
        duration = float(diff.days)* 24 + (float(diff.seconds) / 3600)
        value['production_duration'] = round(duration, 2)

    return {'value': value}

由于

2 个答案:

答案 0 :(得分:2)

我在total_seconds对象上使用timedelta方法:

total_hours = (end - start).total_seconds() / 3600

编辑:60分你会得到分钟......

答案 1 :(得分:0)

我认为你的问题在于

duration = float(diff.days)* 24 + (float(diff.seconds) / 3600)

这不是计算单个单位持续时间的方法。给出累积时间的唯一字段是total_seconds()。所以你应该这样做:

duration = float(diff.total_seconds()) / 3600.