The Time Paradox

How your code can be wrong
without changing a single line

Denny Biasiolli

 

Django Day Copenhagen

Oct 2026

Right now, in this room

It's (almost) lunchtime in Copenhagen

Before breakfast in New York

Almost dinner in Tokyo

Same instant. Three different meals,
depending who you ask.

Still a timezone talk.
The meals are just evidence.

A Story from the Finance Team

April 1, 2026, 4:52 PM CET

The Head of Finance in New York
joins the Q1 close call

Why is our US East Coast revenue
$2.3 million lower than expected?

47 large March 31 payments never made it into Q1
No, it's not an April Fool's joke

$2.3M just disappeared from Q1

  • Wrong tax provisions and revenue recognition
  • Payments (and sales commissions) booked in Q2
  • Financial statements don't match the Q1
  • Board deck and auditors start asking hard questions

The query Finance trusted

# Quarter Revenue & Cash Report (board + tax + commissions)

# received as parameters
quarter_start = "2026-01-01"
quarter_end   = "2026-04-01"

qs = (Payment.objects
    .filter(paid_at__gte=quarter_start,
            paid_at__lt=quarter_end)
    .annotate(quarter=TruncQuarter("paid_at"))
    .values("quarter")
    .annotate(total=Sum("amount"))
)

Simple. Correct. Obviously right.
Trusted by the CFO, controllers, and the entire finance team.

Example of a payment
that leaked into Q2

A customer in New York pays $340k
on March 31 at 20:17 PM local time

Recorded correctly as
paid_at 2026-03-31 20:17:00-04:00

All the queries on the server run in UTC
and classify the payment as 2026-Q2

Same payment. Same database. Different quarter.

-- paid_at = 2026-03-31 20:17:00-04:00

-- what users expect in New York
SET TIMEZONE TO 'America/New_York';
SELECT to_char(paid_at, 'YYYY-"Q"Q');
-- 2026-Q1

-- what the server has in UTC
SET TIMEZONE TO 'UTC';
SELECT to_char(paid_at, 'YYYY-"Q"Q');
-- 2026-Q2
  • paid_at never changes
  • Every single paid_at value we display in the frontend (API+JSON)
    is formatted by the browser using the user's local timezone
  • The dashboard summary is generated server-side
    using the server timezone

Welcome to timezone therapy

How many of you have been personally victimized
by a timezone bug?

I used to think: "It's just a number, how hard can it be?"
I was wrong. So very, very wrong

Quick one: how long is a day?

A timestamp has no timezone

1781255609

It's the same number everywhere on Earth

The timezone only matters
when you display it to a human

Python counts this in seconds. JavaScript in milliseconds.
Copy-paste and you're in the year 55,000.

What is a timezone?

A region that agrees to pretend it's the same time

India: UTC+5:30    Nepal: UTC+5:45
Half-hours. Quarter-hours. Because why not?!

Timezones in the Real World

World Timezones Map Date: 2021, Source: Wikimedia Commons

Timezones timeline

Timezone Timeline Created with everytimezone.com

A bug with no code changes

January: "Send the daily statement at 9:00 AM New York"

One Monday in March, every New York statement
goes out at 10:00 AM

Check the logs. Everything looks normal.
Check the deploys. Nothing changed.
Check the code. Nothing changed.
Check yourself. Have I changed?

The US sprang forward. Europe hadn't.

The world moved. Your UTC timestamp didn't.

The Two Faces of Time

(that look identical until they aren't)

  1. Recording the past (or present)
    order created, log entry, error occurred
  2. Scheduling the future
    reminder, report, recurring task

Recording when something happened:
Record in UTC

UTC is the universal reference time,
it never shifts for daylight saving

  • Unambiguous, always the same reference point
  • Convertible to any local timezone
  • The right choice for anything that already happened

UTC is the boring reliable friend who never cancels plans
Appreciate UTC

Scheduling when something should happen:
Store the intent

Finance doesn't want the daily report at 14:00 UTC

They want it at 9:00 AM in their day,
every single day, regardless of UTC offset

Right after they've eaten their breakfast.

Scheduling when something should happen:
Store the intent

If you store a fixed UTC time and DST changes, the report suddenly runs at the wrong local time

Instead, store 09:00 America/New_York
and recalculate the actual UTC execution time

The intent stays constant
The UTC execution time adapts

UTC-5 ≠ America/New_York

UTC-5 (fixed offset) America/New_York (timezone)
The offset at one specific instant A set of rules that change over time
Always UTC-5, forever UTC-5 Nov-Mar, UTC-4 Mar-Nov
→ Use timezone identifiers, not fixed offsets

IANA Time Zone Database

The standard behind every OS
and programming language

Defines rules for every timezone:
DST start/end, offsets, historical changes

In 2011, Samoa skipped an entire day (Dec 30th) by moving across the date line.
Someone had to update this database. Imagine the commit message

So how long was that day?

DST creates two tricky edge cases

(DST was invented by someone who hates programmers)

  1. Spring forward — the clock skips an hour
  2. Fall back — the clock repeats an hour

1. Spring Forward: the phantom hour

In New York, DST starts on March 8, 2026 at 2:00 AM

The clock jumps: 1:59:59 AM → 3:00:00 AM

2:30 AM on March 8, 2026 never happened

Some libraries throw. Others silently move you to 3:30.
Know what yours does, and warn the user.

2. Fall Back: the duplicate hour

In New York, DST ends on Nov 1, 2026 at 2:00 AM

The clock jumps back: 1:59:59 AM → 1:00:00 AM

1:30 AM happens twice

If it's your alarm clock, pick the second one.
You deserve the extra sleep.

How long is a day?

  • March 8, 2026 in New York: 23 hours long
  • November 1, 2026 in New York: 25 hours long

This matters when scheduling the next run:

# UTC — 10:00 AM after spring-forward
next_run = now + timedelta(hours=24)

# timezone-aware in New York — still 09:00 AM
next_run = now + timedelta(days=1)  # same as hours=24

The datetime must be timezone-aware
in the zone you're scheduling for.
A naive datetime has no DST to respect.

Timezone in Python

datetime object

  • naive, no timezone information
    using the system's local timezone
  • aware, with timezone information
    using the specified timezone

A naive datetime assumes everyone works in the same timezone,
like people who think it's always sunny everywhere
because it's sunny outside their window

Timezone in Python

Example

from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9+

# Naive, 09:00 local timezone
datetime(2026, 10, 26, 9, 0)  # try to avoid this

# Aware, 09:00 in New York
datetime(2026, 10, 26, 9, 0,
         tzinfo=ZoneInfo("America/New_York"))

Timezone in PostgreSQL

timestamptz column

  • Short name for timestamp with time zone
  • Doesn't store the timezone itself
  • Normalizes the timestamp to UTC upon storage
  • Converts it back to the desired timezone upon retrieval

Timezone in PostgreSQL

Postgres can set the timezone in six different places.
Server level, connection string, database, user, session, transaction.
Django uses the last one:

SET LOCAL TIMEZONE TO '<SERVER_TIMEZONE_HERE>';

Per transaction. Per query. That's the lever behind the $2.3M slide.

Django

Django has built-in support for timezones

You can enable it in your settings with

USE_TZ = True

All DateTime fields will be stored in UTC in the database and will be converted to the active timezone when retrieved

Django Timezone setting

You can set the default timezone for your Django application using the TIME_ZONE setting

TIME_ZONE = "UTC"

By default, it's set to UTC, but you can change it to any valid timezone string

PostgreSQL + Django

When USE_TZ is enabled
Django wraps each query in a transaction that sets
SET LOCAL TIMEZONE
to the currently active timezone

The Problem in all of this

All users will see timestamps in the same timezone set in TIME_ZONE, regardless of their location

And that timezone will be used to query the database too

😞

Same report. New York time.

from zoneinfo import ZoneInfo
from django.utils import timezone

timezone.activate(ZoneInfo("America/New_York"))

Payment.objects.filter(
    paid_at__gte="2026-01-01",
    paid_at__lt="2026-04-01",
)
# March 31, 20:17 in New York stays in Q1
# Nobody memorized an offset

Where does the timezone come from?

Who Where the TZ comes from
Fiscal report they type it
Browser cookie
API X-Timezone
Logged-in human profile

Ship the combination. That's the middleware.

Django Middleware solution

import zoneinfo
from django.utils import timezone

if request.user.is_authenticated:
    tzname = request.user.profile.timezone
else:
    tzname = (request.headers.get("X-Timezone")
              or request.COOKIES.get("django_timezone"))

if tzname:
    timezone.activate(zoneinfo.ZoneInfo(tzname))

Django then converts timestamps to that timezone on the way out.
Cookie, header, and curl examples are in the repo.

Three rules to live by

1. Use timezone-aware datetimes

Don't use naive datetimes, or you'll have to do timezone math manually

And timezone math is not real math!

Three rules to live by

2. Store in UTC, display in local time

UTC is the reliable, unambiguous way
to store timestamps

Local time is what users expect to see

That includes your logs.
[2026-03-31 20:17:00] payment received $340k
[2026-04-01 00:17:00Z] payment received $340k
If it doesn't say Z, every post-mortem is a timezone puzzle.

Three rules to live by

3. Ask for the timezone for sensitive operations
(like fiscal reports)

Don't rely on the server's timezone
or a fixed timezone for all users

A day is 23, 24, or 25 hours. Test all three.
Remember: timezone math is not real math!
Have a good cry and then test, test, test.

You may now talk about food.
It's lunchtime. Here.

Thank you!

Denny Biasiolli
dennybiasiolli.com

QR code for repository
QR code for slides