title: How many "premium" hours between datetimes?
body: <p>This is part of a bigger problem we're facing but the problem at the moment is splitting time between two datetimes into two rates based on <em>when</em> those hours are in the day. It's quite arbitrary but we treat 7am-7pm as normal hours and the opposite 12 hours as premium.</p>
  
  <p>So for any given pair of datetimes, we need to grade these down so that we know how many normal hours, or how many premium hours there were in that period. A couple of examples:</p>
  
  <p>If we took the next 24 hours, I'd expect an exact split of 12 hours.</p>
  
  <pre><code>&gt; start = datetime.datetime.now()
  &gt; end = start + datetime.timedelta(1)
  &gt; split_hours(start, end)
  (datetime.timedelta(0, 43200), datetime.timedelta(0, 43200))
  </code></pre>
  
  <p>If we took the next 12 hours, at 20:26, I'd expect 1h26 normal and 10h34m premium rate:</p>
  
  <pre><code>&gt; start = datetime.datetime(2017, 11, 6, 20, 26, 0)
  &gt; end = start + datetime.timedelta(hours=12)
  &gt; split_hours(start, end) 
  (datetime.timedelta(0, 5160), datetime.timedelta(0, 38040))
  </code></pre>
  
  <p>"How do I do that?" is my question. Sorry. I've been thinking through this most of the day but only ever got as far as the following napkin algorithm:</p>
  
  <ul>
  <li>Split range into distinct-date datetime ranges (how?!) and for each:
  
  <ul>
  <li>Count hours before 7am and after 7pm as premium</li>
  <li>Count hours between 7am and 7pm</li>
  </ul></li>
  <li>Total them up.</li>
  </ul>
  
  <p>But even there I don't know how to split things up.</p>
  
  <p>There is also a natural extension —that I'll almost certainly have to implement at some point— that also grades weekend hours as premium too. If I could split time (as in my napkin algorithm) it would be easy to tack on but I still don't like how clumsy that "code" is). If your answer covers that too, you can have my firstborn. Well, no, you can have a bounty or something.</p>
  
  <p>I'm doing this in Python without any real library limits (if eg Pandas Just Does™ this) but if you want to submit a raw C or Pseudo code answer, I'm sure I'll be able to read it.</p>