I'd like to subtract 开发者_开发百科two values, one in the current record, then the next in the next record...they are time clock entries and I want to calculate the amount of time an employee spend on his/her break, so I'll have to subtract the time the employee clocked out, and the time the employee clocked back in. This will be done for several records, then at the end of it all I also want to have total of all the breaks taken.
So how can I do this? I'm doing this in Django BTW.
UPDATE The records look a bit like this:
employee_id, rec_date, start_time, end_time
18, 2010-08-23, 09:58:00, 14:13:00
18, 2010-08-23, 14:39:00, 18:47:00
19, 2010-08-23, 14:15:00, 18:31:00
21, 2010-08-23, 12:05:00, 14:52:00
21, 2010-08-23, 15:23:00, 18:49:00
21, 2010-08-31, 08:00:00, 12:00:00
21, 2010-08-31, 12:45:00, 19:00:00
You'll have to iterate over the clock actions and do the calculation manually. Something of this sort:
breaks = 0
break_start = None
clock_actions = user.clock_actions.filter(date=desired_day).order_by('date')
for action in clock_actions:
if action.type == 'CLOCK IN':
break_start = action.time
elif break_start is not None:
breaks = breaks + action.time - break_start
break_start = None
You may have to adjust the subtraction to work with timedelta objects, depending on your choice of field for time.
精彩评论