title: How to add and subtract times in python
body: <p>I want to write a simple timesheet script. My input file looks like</p>
  
  <pre><code>9:00 17:00
  10:45 12:35
  11:00 15:00
  </code></pre>
  
  <p>I would like to read it in, compute the number of hours worked per day and then sum those hours up. When the day started before 12:00 and ended after 13:00 I would like to subtract half an hour too for lunch break.</p>
  
  <p>My attempt so far is:</p>
  
  <pre><code>import sys
  from datetime import datetime
  
  gap = datetime.strptime('00:30','%H:%M')
  hours = []
  for line in sys.stdin:
      (start_time,end_time) = line.split()
      start_time = datetime.strptime(start_time, '%H:%M')
      end_time = datetime.strptime(end_time, '%H:%M')
      #if start_time before 12:00 and end_time after 13:00 then subtract gap
      hours.append(end_time-start_time)       
  print sum(hours)
  </code></pre>
  
  <p>I don't know how to get the if statement line to work and summing the hours doesn't seem to work either as you can't sum datetime.timedelta types.</p>
  
  <hr>
  
  <p>Thanks to the link in the comments, replacing sum(hours) with reduce(operator.add, hours) works.</p>
  
  <p>The remaining part is how to test if start_time is before 12:00 and end_time is after 13:00 and if so to reduce the timedelta by half an hour.</p>