title: Bracketing time intervals in R/tidyverse
body: <p>I need to figure out how much time is overlap between an interval and a predetermined bracket of hours during the day.</p>
  <p>Here's the example data</p>
  <pre><code>require(tidyverse)
  example &lt;- tibble::tribble(
    ~`&quot;Session&quot;`, ~`&quot;SessionStartTime&quot;`,   ~`&quot;SessionEndTime&quot;`, ~`&quot;TotalDuration&quot;`,
              1L, &quot;2020-09-10 09:05:50&quot;, &quot;2020-09-11 05:50:02&quot;,             1244.2,
              2L, &quot;2020-09-10 23:55:20&quot;, &quot;2020-09-11 01:20:20&quot;,                 85,
              3L, &quot;2020-09-11 01:11:30&quot;, &quot;2020-09-11 02:12:02&quot;,   60.5333333333333,
              4L, &quot;2020-09-11 02:20:30&quot;, &quot;2020-09-11 03:00:02&quot;,   39.5333333333333
    )
  </code></pre>
  <p>And here's the output I'm looking for:</p>
  <pre><code>output &lt;- tibble::tribble(
    ~Session,   ~`00:00-03:00`, ~`03:00-06:00`, ~`06:00-09:00`, ~`09:00-12:00`, ~`12:00-15:00`, ~`15:00-18:00`, ~`18:00-21:00`,   ~`21:00-00:00`,
          1L,        &quot;3 hours&quot;,   &quot;2.82 hours&quot;,             NA,             NA,             NA,             NA,             NA, &quot;2.902778 hours&quot;,
          2L, &quot;1.338889 hours&quot;,             NA,             NA,             NA,             NA,             NA,             NA,  &quot;4.666667 mins&quot;,
          3L,  &quot;60.53333 mins&quot;,             NA,             NA,             NA,             NA,             NA,             NA,               NA,
          4L, &quot;2.341667 hours&quot;,    &quot;2 seconds&quot;,             NA,             NA,             NA,             NA,             NA,               NA
    )
  
  </code></pre>
  <p>I've tried lubridate::interval but run into problems with sessions overlapping with multiple brackets, and sessions spanning across days.
  I've also tried chron but doesn't handle the difference between 5:00 the next day and 23:00 the previous day.</p>