【问题标题】:Odoo - Subtract 2 "time" fields in pythonOdoo - 在python中减去2个“时间”字段
【发布时间】:2023-04-07 00:54:01
【问题描述】:
for emp in employee:
  contract_id = contract_pool.search(cr, uid, [('employee_id','=',emp.employee_id.id)], context=context)
     for contract in contract_pool.browse(cr, uid, contract_id, context=context):
        for attendance in contract.working_hours.attendance_ids:
          if day == attendance.dayofweek:
             planned_time_in = attendance.hour_from
             planned_time_out = attendance.hour_to
             planned_wrkng_hrs = planned_time_out - planned_time_in
             print planned_wrkng_hrs
  actual_time_in = emp.time_in
  actual_time_out = emp.time_out
  actual_wrkd_hrs = actual_time_out - actual_time_in
  print actual_wrkd_hrs
  hrs_short = planned_wrkng_hrs - actual_wrkd_hrs
  print hrs_short

这给了我如下输出:
9.00
8.57
0.43
我如何获得:
9:00
8:57
00:03
此处减去浮点值而不是时间。

【问题讨论】:

  • 将时间转换为秒,然后进行运算,一旦得到秒的结果,除以 60 分钟,3600 小时

标签: python openerp odoo-9


【解决方案1】:

假设时间值的格式与您的示例中的一样,您可以通过执行类似的操作将其转换为 minutes-since-midnight

time_in_mins = int(original_time) * 60 + (original_time - int(original(time)) * 100

然后使用新的“标准化”值进行所有计算,然后将其转换回您的首选格式以进行显示或存储。

【讨论】:

    【解决方案2】:

    希望对你有所帮助^^:

    from datetime import time
    from datetime import datetime, date
    import math
    # on thing hour part cannot be more that 23 and 
    first_time = 23.5
    secend_time = 23.0
    
    # convert float to hour and minute
    f_hour, f_time = int(math.floor(first_time)), int(round((first_time % 1) * 60))
    s_hour, s_time = int(math.floor(secend_time)), int(round((secend_time % 1) * 60))
    # create datetime object to perform substraction
    duration = datetime.combine(date.min,time(f_hour,f_time))- datetime.combine(date.min,time(s_hour,s_time))
    print "total minute between this two time is %s minutes " % (duration.total_seconds() / 60)
    

    结果:

    total minute between this two time is 30.0 minutes 
    

    【讨论】:

    • 这里的 date.min 是什么?
    • 它只是一个最小日期 0001-01-01 创建一个带时间的日期,所以结果是 0001-01-01 23:30:00 0001-01-01 23:00:00 没有'不适合你?
    猜你喜欢
    • 1970-01-01
    • 2020-04-09
    • 2023-02-14
    • 2016-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-14
    • 1970-01-01
    相关资源
    最近更新 更多