learning-it by example

#140 Python for data 6.7 min

Python Dates and Time Zones: 5 Bugs Reproduced

Add one day to a London time across the clock change, and only 23 real hours pass.

1. In Python, what is the difference between an aware datetime and a naive datetime?
2. A London meeting at 09:00 is moved to New York. The code uses aware.replace(tzinfo=NEW_YORK) in one case and aware.astimezone(NEW_YORK) in the other. What did Python print for the two results?
3. Start at 09:00 in London on Saturday 28 March 2026, add one day, and subtract the two times in Python. How long does Python say passed, and how many real hours passed?
4. On 25 October 2026 in London, 01:30 happens twice. Which attribute picks which one, and what does subtracting them in the same zone give?
5. On 29 March 2026 the London clocks jump forward at 01:00. What did the exists check show for 01:30 and 02:30, and did zoneinfo raise an error for the missing time?

Practice only: nothing is saved and no score counts towards anything. Each answer comes with the reason.

  1. A day without a clock change

    Copy the code folder first. On Windows, pin tzdata as the video does. In run.py, in the dst mode, change the start date from datetime(2026, 3, 28, 9, 0, tzinfo=LONDON) to datetime(2026, 4, 10, 9, 0, tzinfo=LONDON). Predict the real hours between, then run python run.py dst. When I ran it, Python said 1 day, 0:00:00 and the real hours were 24.0.

    A hint

    Nothing changes the London clocks between 10 and 11 April.

  2. Remove fold

    In run.py, in the fold mode, change second = first.replace(fold=1) to second = first. Predict the UTC time on the fold=1 line and the real hours between. Then run python run.py fold. When I ran it, both lines showed UTC 00:30:00 and the real hours between were 0.0.

    A hint

    Without fold=1, Python picks the earlier of the two 01:30 times.

  3. Break the parsed Z string

    In run.py, in the naive mode, change the string '2026-03-30T08:00Z' to '2026-03-30T09:00Z'. London is on BST on 30 March, so predict what the 'parsed Z matches' line will print. Then run python run.py naive. When I ran it, it printed False.

    A hint

    BST is one hour ahead of UTC, so 09:00 in London is not 09:00 UTC.

  4. Another country's clock change

    In run.py, in the dst mode, change the start to datetime(2026, 3, 7, 9, 0, tzinfo=NEW_YORK). Predict the 'real hours between' line. Then run python run.py dst. When I ran it, the start was EST, the next day was EDT, and the real hours between were 23.0. The video tested only two zones, so treat other zones as something to check.

    A hint

    The name of the zone abbreviation changes on the +1 day line.

These tasks are for you to try on your own machine. Nothing is checked or saved.

What this does not show

Two zones and a few made-up 2026 times on one Windows run; it does not test other countries' rules, databases or calendars.

Up next in Python for data

More in Python for data

All Python for data videos

Related videos