The Time Paradox: Building Timezone-Safe Python/Django
summary
A production bug moves 47 payments out of Q1 because server-side queries run in UTC while payments are recorded with local offsets, so March 31 20:17 America/New_York becomes 00:17 UTC and lands in Q2. Root causes: Postgres’s session set local time zone comes from Django’s active TZ and Django/Postgres behavior when TIME_ZONE and USE_TZ interact. Fixes: store instants in UTC, store user intent as local time plus IANA tz, regenerate UTC when scheduling, use tz‑aware datetimes, and use a middleware that calls timezone.activate (read TZ from cookie/header/profile). Also prefer Postgres timestamp with time zone and ask for TZ on fiscal reports.
AI-written summary · about
chapters (13)
- 0:00Introduction to time paradox
- 1:27Finance horror story explained
- 5:21What is a timestamp?
- 7:46Customer statement timing issue
- 9:31Recording past vs scheduling future
- 12:20Daylight saving time edge cases
- 14:08How long is a day?
- 15:15Naive vs timezone-aware datetimes
- 16:59PostgreSQL timezone handling
- 17:41Django timezone settings
- 19:08Dynamic timezone activation
- 21:36Three key timezone rules
- 23:02Q&A session begins
