Thomas Eapen · Selected work
From absence intervals to a clear report
A worked example that clips records to the reporting window, removes overlap within each person and allocates their absence to time periods.
INVENTED INPUTS
Five records, four people
| Person | Start | End |
|---|---|---|
| A | 08:20 | 10:20 |
| A | 09:50 | 10:50 |
| B | 09:30 | 12:10 |
| C | 11:15 | 13:45 |
| D | 13:00 | 14:20 |
Person A’s two records overlap by 30 minutes. Person D’s record extends 20 minutes beyond 14:00. Convert times to minute offsets before calculating.
Reporting periods are 08:00–10:00, 10:00–12:00 and 12:00–14:00.
SELECTED PYTHON
The calculation
records contains the input rows in minutes after 08:00. periods contains the three boundary pairs. merged_intervals merges overlapping records within one person. defaultdict is imported from collections.
def allocate_absence(records, periods):
grouped = defaultdict(list)
for record in records:
grouped[record['person']].append(
(record['start'], record['end'])
)
result = {}
for person, intervals in sorted(grouped.items()):
spans = merged_intervals(intervals)
result[person] = [
sum(max(0, min(end, right) - max(start, left))
for start, end in spans)
for left, right in periods
]
return resultExtracted from the new helper that computes this example.
COMPUTED RESULT
Keep each person’s contribution
520person-minutes of absence570 − 20 − 30 = 520. The three reporting periods sum to the same total.
Clip, deduplicate, then allocateThe interval calculation is max(0, min(end, period end) − max(start, period start)). Boundaries are half-open, so adjacent periods do not count the same minute twice.
Measure person-timeOverlapping records for one person count once. Absences of different people are summed separately, even when their clock times overlap. This is not the union of the whole team’s clock time.