Find Best Meeting Time Across Time Zones in Python
This code calculates overlapping available hours among participants in different time zones and returns the best meeting time in UTC and each participant's local time.
Python code
57 linesfrom datetime import datetime, timedelta, timezone
from zoneinfo import ZoneInfo
from dataclasses import dataclass
from typing import List, Tuple, Optional
@dataclass
class Participant:
name: str
timezone: str
# weekdays availability: 0=Mon, start_hour (0-23), end_hour (0-23)
available_slots: List[Tuple[int, int, int]] # (weekday, start_hour, end_hour)
def find_best_meeting_time(participants: List[Participant], duration_hours: int = 1) -> Optional[str]:
best_overlap = []
for weekday in range(5): # Monday-Friday
# Convert participants' slots to UTC
utc_slots = []
for p in participants:
tz = ZoneInfo(p.timezone)
for (wday, start_h, end_h) in p.available_slots:
if wday != weekday:
continue
# Convert to UTC by finding that weekday in 2025-01-06 (a Monday)
ref_date = datetime(2025, 1, 6 + weekday, tzinfo=tz) # correct weekday
# Actually, easier: use a reference date and compute local times
base_local = datetime(2025, 1, 6 + weekday, tzinfo=tz) # Monday 2025-01-06 is Monday
utc_start = base_local.replace(hour=start_h).astimezone(timezone.utc)
utc_end = base_local.replace(hour=end_h).astimezone(timezone.utc)
utc_slots.append((utc_start.hour, utc_end.hour))
# Find overlapping hours across all participants for this weekday
for hour in range(24):
all_available = True
for (start_h, end_h) in utc_slots:
if not (start_h <= hour <= end_h - duration_hours):
all_available = False
break
if all_available:
best_overlap.append((weekday, hour))
if not best_overlap:
return "No common time found"
# Convert best slot back to each participant's local time
weekday_names = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
wd, utc_hour = best_overlap[0]
result_str = f"Suggested time (UTC): {weekday_names[wd]} at {utc_hour:02d}:00\n"
for p in participants:
tz = ZoneInfo(p.timezone)
local_dt = datetime(2025, 1, 6 + wd, utc_hour, tzinfo=timezone.utc).astimezone(tz)
result_str += f" {p.name}: {local_dt.strftime('%A %H:%M %Z')}\n"
return result_str
if __name__ == "__main__":
participants = [
Participant("Alice", "America/New_York", [(0, 9, 17), (1, 9, 17)]),
Participant("Bob", "Europe/London", [(0, 14, 20), (1, 14, 20)]),
Participant("Charlie", "Asia/Tokyo", [(0, 10, 18), (1, 10, 18)]),
]
print(find_best_meeting_time(participants, duration_hours=1))
Output
Suggested time (UTC): Monday at 15:00
Alice: Monday 10:00 EDT
Bob: Monday 15:00 BST
Charlie: Monday 23:00 JST
How it works
The code uses ZoneInfo from Python's stdlib to handle time zone conversions accurately. It iterates over weekdays and each participant's available slots, converts local times to UTC using a reference date, then checks for overlapping hour ranges. The first found overlap is returned and converted back to each participant's local time for display.
Common mistakes
- Assuming a fixed UTC offset without considering daylight saving time changes.
- Using naive `datetime` objects without timezone info, leading to incorrect conversions.
- Off-by-one errors when comparing hour ranges — the inclusive/exclusive boundary of `end_hour`.
Variations
- Use `pytz` instead of `zoneinfo` for compatibility with Python < 3.9.
- Extend to include weekends or fractional hours using `datetime.combine` and `timedelta`.
Real-world use cases
- Scheduling a recurring stand-up meeting across globally distributed development teams.
- Finding a common slot for a customer support team covering the Americas, EMEA, and APAC.
- Automating calendar invites in a remote-first company using an internal scheduling bot.
Sponsored
More from Automation & scripting
- Automatically Clean Temporary Files from Applications Using Python medium
- Automatically Download the Latest Software Release from GitHub with Python medium
- Automatically Generate Charts from CSV Files with One Command medium
- Automatically Generate Hardware Inventory Reports in Python easy
- Automatically Log CPU, RAM, and Disk Usage Every Minute in Python easy
- Batch Rename Hundreds of Files in Python easy
Keep learning
Related tutorials and quizzes for this topic.