You're trying to do too much in one loop.  Subtracting the gaps is difficult because it's difficult to identify the gaps.  Identifying the gaps is difficult because of all the weird ways the intervals overlap, so remove the overlapping interval problem first.

Make one pass to flatten the intervals, using an algorithm from [this question](https://softwareengineering.stackexchange.com/q/241373/3965).  Then one pass to make a list of gaps, which should be very simple with the flattened list.

*Now* you're ready to loop through your original list of intervals.  For each interval, see how many gaps occur before it, and subtract the cumulative time of each.