Python Django时间场差

Python Django时间场差,python,django,Python,Django,我需要比较两个时间字段的值并计算总工作时间 我的功能是我所尝试的 def difft(start,end): a,b,c,d = start.hour, start.minute, start.second, start.microsecond w,x,y,z = end.hour, end.minute, end.second, end.microsecond delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000

我需要比较两个时间字段的值并计算总工作时间

我的功能是我所尝试的

def difft(start,end):
    a,b,c,d = start.hour, start.minute, start.second, start.microsecond
    w,x,y,z = end.hour, end.minute, end.second, end.microsecond
    delt = (w-a)*60 + (x-b) + (y-c)/60. + (z-d)/60000000
    return delt + 1440 if delt<0 else delt

def myfunction():
    attendance = StaffAttendance.objects.get(id=1)

    time_sheets = []    

    t_times = TimeSheet.objects.filter(staff_attendance=attendance,is_deleted=False).order_by("time")
    for t in t_times:
        time_sheets.append(t.time)

    #timesheets = [datetime.time(4, 0), datetime.time(6, 0)]

    total_time = 0
    times = []

    for time in time_sheets: 
        if time in times:
            m = difft(times[0],time)
            total_time += m
            times = []
        else:
            times.append(time)
if time in times:此块始终返回False


如何从django时间字段计算总工作小时数。我是Python/Django新手。所以我对python date不太了解。你能帮我解决这个问题吗。谢谢

如果时间表具有唯一的时间数据,则If语句将始终返回false。我不知道你想在for循环中做什么。也许您想检查时间是否不在时间内,这样您就得到了未处理的时间,您可以使用difft函数,并可以将已处理的时间附加到时间列表中。

您可以放置一个打印语句来查看循环中的时间表和时间值吗?如果时间在时间内:将始终返回false,因为时间是一个空列表[].可能的副本