简体   繁体   English

Odoo - 在python中减去2个“时间”字段

[英]Odoo - Subtract 2 “time” fields in python

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

This gives me output as: 这给了我输出:
9.00 9.00
8.57 8.57
0.43 0.43
How can I get: 我怎样才能得到:
9:00 9:00
8:57 8:57
00:03 00:03
Float values are getting subtracted here instead of time. 浮动值在这里被减去而不是时间。

assuming the format of the time values is like in your example, you can convert it to minutes-since-midnight by doing something like 假设时间值的格式与您的示例类似,您可以通过执行类似操作将其转换为分钟 - 午夜

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

then do all your calculations using the new "normalized" values, and later convert it back to your preferred format for display or storage. 然后使用新的“标准化”值进行所有计算,然后将其转换回您首选的显示或存储格式。

Hope this help you ^^: 希望这能帮到你^^:

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)

Result: 结果:

total minute between this two time is 30.0 minutes 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM