brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: Python's zoneinfo gets Dutch summer and winter time right without pytz

Python
~/tips/tip-python-zoneinfo-europe-amsterdam

from zoneinfo import ZoneInfo

Converts times to Dutch local time with summer and winter time handled for you.

Living in Leiden means the clock jumps an hour twice a year, and any script that prints times has to know about it. Python has had the answer in the standard library since 3.9: the zoneinfo module, which reads the IANA time zone database. No pytz, and no hard-coded +01:00 that is wrong half the year.

from datetime import datetime, timezone
from zoneinfo import ZoneInfo

ams = ZoneInfo("Europe/Amsterdam")
utc = datetime(2026, 7, 1, 12, 0, tzinfo=timezone.utc)

print(utc.astimezone(ams))                       # 2026-07-01 14:00:00+02:00
print(datetime(2026, 12, 1, 12, 0, tzinfo=ams))  # 2026-12-01 12:00:00+01:00

The same zone gives +02:00 in July and +01:00 in December, with no work on your side. I’d store and log times in UTC and only call astimezone() at the moment a human has to read them. That one habit avoids most time bugs.

The night the clocks go back is the tricky part. On 25 October 2026 the clock in the Netherlands goes from 03:00 back to 02:00, so 02:30 happens twice. Python handles that with the fold attribute: fold=0 is the first 02:30, fold=1 the second one.

t = datetime(2026, 10, 25, 2, 30, tzinfo=ams)
print(t)                  # 2026-10-25 02:30:00+02:00
print(t.replace(fold=1))  # 2026-10-25 02:30:00+01:00

When you convert from UTC with astimezone(), Python sets the fold for you, which is one more reason to start from UTC.

The caveat is Windows. zoneinfo doesn’t ship the time zone data itself; it uses the system’s database, and Windows has no IANA database. There, and in some slim containers, ZoneInfo("Europe/Amsterdam") raises ZoneInfoNotFoundError until you install the first-party tzdata package from PyPI. If your script has to run anywhere, the Python docs recommend listing tzdata as a dependency.

Further reading: zoneinfo, IANA time zone support (Python docs)

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link