Handling time zones and DST for weekly playlist schedules

Backend Live Coding November 13, 2026 python

USE_TZ=True and all timestamps in UTC in the database.

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# ======================================================================
# Q: A customer schedules a playlist "every Monday 09:00-12:00" for screens in
# different countries. How do you handle the times?
#
# USE_TZ=True and all timestamps in UTC in the database. But the schedule is the
# screen's local time, so I store the time and day (09:00, Monday) and the
# screen's time zone (Europe/Athens), and compute the UTC at the moment it is
# needed. Otherwise, at the daylight saving change everything shifts by one
# hour.
# ======================================================================

from zoneinfo import ZoneInfo

def is_slot_active(slot, screen, at_utc):
    local = at_utc.astimezone(ZoneInfo(screen.timezone))
    return (local.weekday() in slot.weekdays
            and slot.start_time <= local.time() < slot.end_time)

# Tests with time-machine/freezegun on the days of the clock change (last Sunday of March/October)