簡體   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

這給了我輸出:
9.00
8.57
0.43
我怎樣才能得到:
9:00
8:57
00:03
浮動值在這里被減去而不是時間。

假設時間值的格式與您的示例類似,您可以通過執行類似操作將其轉換為分鍾 - 午夜

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

然后使用新的“標准化”值進行所有計算,然后將其轉換回您首選的顯示或存儲格式。

希望這能幫到你^^:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM