title: Calculate working hours between 2 dates in PostgreSQL
body: <p>I am developing an algorithm with Postgres (PL/pgSQL) and I need to calculate the number of working hours between 2 timestamps, taking into account that weekends are not working and the rest of the days are counted only from 8am to 15pm.</p>
  
  <p>Examples:</p>
  
  <ul>
  <li><p>From Dec 3rd at 14pm to Dec 4th at 9am should count 2 hours:</p>
  
  <pre><code>3rd = 1, 4th = 1
  </code></pre></li>
  <li><p>From Dec 3rd at 15pm to Dec 7th at 8am should count 8 hours:</p>
  
  <pre><code>3rd = 0, 4th = 8, 5th = 0, 6th = 0, 7th = 0
  </code></pre></li>
  </ul>
  
  <p>It would be great to consider hour fractions as well.</p>