Python says these times are equal. They’re an hour apart.
Cedrik built Clockshift to inspect local schedules around clock changes. While checking a weekend example, I found a second surprise in Python: two timezone-aware values printed different UTC offsets and still compared equal. Belgrade turns its clocks back on 25 October 2026 , from 03:00 to 02:00. The local time 02:30 occurs twice. The published clock-change date supplies the calendar context;…
In a recent Dev.to article, Cedrik showcased an interesting behavior of Python's datetime module when dealing with timezone-aware values during clock changes. When comparing two timezone-aware datetime objects representing the same local time but with different UTC offsets due to a clock change, Python considers them equal. The example provided involved Belgrade's clock change on October 25, 2026, where the local time 02:30 occurs twice - once at UTC+2 and once at UTC+1.
Despite the different UTC offsets, Python's comparison operation returns True, and the difference between the two datetime objects is 0 seconds. However, when the datetime objects are converted to UTC, the comparison returns False, and the difference between them is 3600 seconds, highlighting the importance of using UTC values for elapsed time calculations or identity checks.
This behavior is documented in Python's datetime reference, and developers should be aware of it when working with timezone-aware datetime objects.
Brief written by urgent.news from Dev.to's own syndicated text. Machine-written — may contain errors; check the original before relying on it.