Sub topic 14
Automate recurring blocks and find free time with code
This chapter assumes you already create and edit recurring events in a digital calendar. It also assumes you can read a short Python function. You don't need to write much code. The goal is to stop rebuilding the same blocks by hand each week and to find open time without scanning the calendar by eye.
Two jobs worth automating
Two chores repeat in time blocking:
- Creating the same blocks every week, such as a daily focus block.
- Finding gaps between meetings where a new task can fit.
Recurring events handle the first. A small script handles the second.
Recurring blocks as an iCalendar file
Every major calendar app imports iCalendar (.ics) files. A recurrence rule (RRULE) tells the app how often an event repeats. This file creates a 90-minute focus block every weekday from 09:00, starting Monday 5 January 2026.
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Example//Time Blocking//EN
BEGIN:VEVENT
UID:[email protected]
DTSTAMP:20260101T080000Z
DTSTART:20260105T090000
DTEND:20260105T103000
RRULE:FREQ=WEEKLY;BYDAY=MO,TU,WE,TH,FR
SUMMARY:Focus block: deep work
TRANSP:OPAQUE
END:VEVENT
END:VCALENDARKey fields:
| Field | Purpose |
|---|---|
| `UID` | A unique ID. Re-importing the same UID updates the event instead of duplicating it. |
| `DTSTART` / `DTEND` | The first occurrence. The 90-minute length comes from the difference. |
| `RRULE` | Repeats weekly on the listed days. |
| `TRANSP:OPAQUE` | Shows you as busy, so others cannot book over the block. |
The start time has no Z or time zone, so it is floating local time. The block stays at 09:00 wherever your calendar is set, which suits a daily routine. A fixed time zone suits events tied to one place.
Finding free slots with Python
Once meetings are on the calendar, the next question is where a task fits. The function below takes the workday and a list of busy intervals. It returns the gaps.
from datetime import datetime
def free_slots(day_start, day_end, busy):
slots = []
cursor = day_start
for start, end in sorted(busy):
if start > cursor:
slots.append((cursor, start))
cursor = max(cursor, end)
if cursor < day_end:
slots.append((cursor, day_end))
return slots
day_start = datetime(2026, 1, 5, 9, 0)
day_end = datetime(2026, 1, 5, 17, 0)
busy = [
(datetime(2026, 1, 5, 13, 0), datetime(2026, 1, 5, 14, 0)),
(datetime(2026, 1, 5, 10, 0), datetime(2026, 1, 5, 10, 30)),
]
for start, end in free_slots(day_start, day_end, busy):
print(start.strftime('%H:%M'), '-', end.strftime('%H:%M'))
# 09:00 - 10:00
# 10:30 - 13:00
# 14:00 - 17:00The function works like this:
- It sorts the busy intervals by start time, so the input order does not matter. The example lists the 13:00 meeting first.
- A
cursormarks the end of the last busy time seen. If the next meeting starts after the cursor, the stretch between them is free. max(cursor, end)handles overlapping meetings. A short meeting inside a longer one does not pull the cursor backward.- After the loop, any time left before the end of the day becomes the final slot.
Common errors
- Mixing naive and timezone-aware datetimes. Python raises an error when you compare them. Use one kind throughout.
- Forgetting buffers. The function returns raw gaps. Subtract your padding from each slot before placing tasks, or you will pack the day too tightly.
- Treating every gap as usable. A 15-minute slot cannot hold a 90-minute deep work block. Filter slots by the length each block type needs.
- Recurring blocks that nobody reviews. Automation creates blocks that persist. Check them in your weekly review and delete the ones you no longer use.
Exercise
In the Python example, a new 30-minute meeting from 09:30 to 10:00 is added to the busy list. What does the output become, and why does the function still work if the meeting list is unsorted?
Show answer
The output becomes 09:00 - 09:30, 10:30 - 13:00, and 14:00 - 17:00. The 09:30 meeting ends at 10:00, which is exactly when the existing gap would have started, so the first slot shrinks to 09:00 - 09:30. The 10:00 - 10:30 meeting then covers the rest of the morning until 10:30. The function calls sorted(busy) first, so it always processes meetings in start-time order regardless of how they were listed.