Thomas Eapen · Selected work

Counting time without counting it twice

Merge overlapping and contained intervals before summing their duration.

Synthetic example · independently invented dataOne-page print edition · A3 landscape
100 minsum of raw durations
80 mindistinct covered time
20 minduplicate coverage removed
10 mingap preserved

SYNTHETIC INPUT / ONE DAY

Four records, two covered spans

Invented activity intervals
ActivityStartEndDuration
Activity A09:1009:4030 min
Activity B09:3010:1040 min
Activity C09:5010:0010 min
Activity D10:2010:4020 min
Four overlapping activity intervals and their combined coverageThe raw durations sum to 100 minutes. Merging nested and overlapping intervals gives 60 minutes from 09:10 to 10:10, plus 20 minutes from 10:20 to 10:40: 80 distinct minutes.09:0009:3010:0010:30Activity A30mActivity B40mActivity C10mActivity D20mCombined60m20mContained intervals add no new coverage; the ten-minute gap remains.
The merged result is 09:10–10:10 and 10:20–10:40: 60 + 20 = 80 minutes.

THE METHOD / PYTHON

Sort and extend the current span

def merge_intervals(intervals):
    if any(end < start for start, end in intervals):
        raise ValueError('An interval cannot end before it starts.')
    merged = []
    for start, end in sorted(intervals):
        if start == end:
            continue
        if merged and start <= merged[-1][1]:
            merged[-1][1] = max(merged[-1][1], end)
        else:
            merged.append([start, end])
    return merged

Selected code from the new demonstration that computes these results.

  • Normalise timestamps to one unit before calculating.
  • Reject reversed intervals; zero-length records add no time.
  • Retain gaps instead of spanning the entire first-to-last window.

The important distinction Adding record durations gives 100 minutes; measuring the union gives 80. The nested 09:50–10:00 record contributes no additional covered time.