Sub topic 14
Optional: Find Free Gaps with a Script
This chapter is optional. It automates the audit and placement steps from earlier chapters. You can protect your time without it.
Prerequisites: You can read basic Python (loops, functions, tuples) and you know how to export or list your calendar events. You also need the idea of block length from "Choose block length and placement".
Why script it
Looking at a crowded calendar and spotting a 90-minute gap is easy on one day. It is tedious across a week, and it is easy to misjudge when meetings overlap. A short function can list every gap that is long enough for focused work. You can then pick the best ones for recurring blocks.
The idea
Treat each day as a timeline from the start of your working hours to the end. Walk through the busy intervals in order and keep a cursor, the earliest moment not yet covered by a meeting. Whenever the next meeting starts at least min_length after the cursor, that stretch is a free block.
The code
from datetime import datetime, timedelta
def free_blocks(busy, day_start, day_end, min_length):
"""Return (start, end) gaps of at least min_length between busy intervals."""
free = []
cursor = day_start
for start, end in sorted(busy):
if start - cursor >= min_length:
free.append((cursor, start))
cursor = max(cursor, end)
if day_end - cursor >= min_length:
free.append((cursor, day_end))
return free
busy = [
(datetime(2026, 11, 30, 10, 0), datetime(2026, 11, 30, 10, 30)),
(datetime(2026, 11, 30, 13, 0), datetime(2026, 11, 30, 14, 0)),
]
gaps = free_blocks(
busy,
datetime(2026, 11, 30, 9, 0),
datetime(2026, 11, 30, 17, 0),
timedelta(minutes=90),
)
for start, end in gaps:
print(start.strftime("%H:%M"), "-", end.strftime("%H:%M"))Running it prints:
10:30 - 13:00
14:00 - 17:00The 09:00 to 10:00 gap is only 60 minutes, so it is skipped. The 10:30 to 13:00 gap is 150 minutes, and the afternoon gap is 180 minutes.
Why `cursor = max(cursor, end)`
Meetings can overlap or nest. Suppose one meeting runs 10:00 to 12:00 and another runs 10:30 to 11:00. If you set the cursor to the second meeting's end, you would wrongly report a gap from 11:00 to 12:00. Taking the maximum keeps the cursor at 12:00.
Common errors
- Mixing naive and timezone-aware datetimes. Subtracting them raises
TypeError. Make every value in one call either naive or aware. - Events outside working hours. A meeting that starts after
day_endproduces a gap that runs past the end of your day. Drop or clip those events first. - All-day events. A birthday or "out of office" entry covers the whole day. Decide whether to include it before you pass it in.
- Tentative or declined events. Only include the statuses that really stop you from working.
- Using one day's result for the week. Run the function per day, then compare which weekdays and times keep showing up. Those are good candidates for recurring blocks.
Exercise
A day runs 09:00 to 17:00. Busy intervals are 09:30 to 12:00 and 10:00 to 11:00. With a minimum length of 60 minutes, which gaps does free_blocks return, and why is the nested 10:00 to 11:00 meeting harmless?
Show answer
It returns only 12:00 to 17:00. The 09:00 to 09:30 gap is under 60 minutes. For the nested meeting, the cursor is 12:00 after the first interval, and max(cursor, end) keeps it at 12:00 when the 10:00 to 11:00 meeting is processed, so no false gap appears between 11:00 and 12:00.