Back to the blog

Tony CantaveHow I Handle Time Zones and Daylight Saving in Python Scripts

By Tony Cantave ·

Time looks simple until a script runs at the wrong hour twice a year. I learned this while building the screen management software for Local Flyer Ads, where content has to change at specific local times in Connecticut. The bugs never showed up in testing. They showed up the weekend the clocks changed.

Here is the small set of habits I now follow for any Python script that deals with dates and times.

Use zoneinfo, not fixed offsets

Since Python 3.9 the standard library includes zoneinfo, which knows the real rules for every time zone, including when daylight saving starts and ends.

from datetime import datetime
from zoneinfo import ZoneInfo

NY = ZoneInfo("America/New_York")
start = datetime(2026, 12, 1, 9, 0, tzinfo=NY)
print(start.strftime("%b %d %I:%M %p %Z"))

Connecticut runs on America/New_York time. The mistake I see most often is hard coding a five hour offset from UTC. That is right for about four months of the year and wrong for the rest, because summer time is only four hours behind UTC. A named zone handles the switch for you.

One note for Windows: the operating system does not ship the time zone database Python expects, so install it with pip install tzdata. On Linux and macOS it usually just works.

Naive versus aware datetimes

A datetime without tzinfo is called naive. It is just a calendar date and a clock reading with no idea where on Earth it applies. A datetime with tzinfo is aware.

My rule is simple: anything that crosses a boundary (a file, a database, an API, another machine) must be aware. Naive datetimes are fine for a quick calculation inside one function, and nowhere else. Mixing the two raises a TypeError when you compare them, which is actually helpful because it forces you to decide.

To get the current time correctly:

from datetime import datetime, timezone

now_utc = datetime.now(timezone.utc)
now_local = now_utc.astimezone(NY)

I avoid datetime.utcnow(). It returns a naive value that only looks like UTC, and it is deprecated in recent Python versions for exactly that reason.

Store in UTC, display in local time

Every timestamp I save goes in as UTC. Every timestamp I show a person gets converted to their zone right before display.

def to_storage(dt):
    return dt.astimezone(timezone.utc).isoformat()

def for_display(stored, zone=NY):
    return datetime.fromisoformat(stored).astimezone(zone)

UTC never jumps forward or back, so sorting, comparing and subtracting stored values is always correct. Local time is a presentation concern, like formatting a number with commas.

Trap one: the hour that does not exist

In 2026, daylight saving starts on Sunday, March 8. At 2:00 AM the clocks jump to 3:00 AM, so 2:30 AM never happens in Connecticut that day. Python will still let you build it without complaint.

ghost = datetime(2026, 3, 8, 2, 30, tzinfo=NY)
back = ghost.astimezone(timezone.utc).astimezone(NY)
print(ghost.hour, back.hour)

The round trip through UTC comes back as 3:30, not 2:30. I use that as a check: if converting to UTC and back changes the wall clock time, the original time did not exist, and the script needs a rule for what to do. For scheduled content I move it to the next valid minute and log that I did.

Trap two: the hour that happens twice

Daylight saving ends on Sunday, November 1, 2026. Clocks fall from 2:00 AM back to 1:00 AM, so 1:30 AM happens twice. Python uses the fold attribute to tell them apart.

first = datetime(2026, 11, 1, 1, 30, tzinfo=NY, fold=0)
second = datetime(2026, 11, 1, 1, 30, tzinfo=NY, fold=1)
print(first.tzname(), second.tzname())

The first prints EDT and the second prints EST. If a job is supposed to run once at 1:30, decide ahead of time which one you mean. I schedule by UTC under the hood so a job can never fire twice by accident.

Trap three: adding a day is not adding 24 hours

This one surprised me most. When two aware datetimes share the same tzinfo, Python does arithmetic on the wall clock, not on elapsed time.

from datetime import timedelta

sat = datetime(2026, 3, 7, 12, 0, tzinfo=NY)
sun = sat + timedelta(days=1)
print(sun.hour)
wall = (sun - sat).total_seconds() / 3600
real = (sun.astimezone(timezone.utc) - sat.astimezone(timezone.utc)).total_seconds() / 3600
print(wall, real)

Adding one day gives noon on Sunday, which is usually what a person wants for a daily schedule. But the wall clock difference says 24 hours while only 23 real hours passed. When I need elapsed time, for billing screen uptime or measuring how long a job ran, I convert both values to UTC before subtracting.

A short checklist

Before I trust any script that touches time, I check these:

  1. Every datetime that leaves a function is aware.
  2. Storage and comparisons use UTC.
  3. Display converts to a named zone, never a fixed offset.
  4. Schedules have a rule for the missing hour in March and the repeated hour in November.
  5. Elapsed time is measured in UTC.
  6. Tests include March 8 and November 1, not just ordinary days.

That last point is the one that would have saved me the most trouble. Date bugs hide on the two weekends a year when the clock moves, so those dates belong in the test suite permanently.