Python traps · Chapter 11 of 16

Naive versus aware datetimes

Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Time
  • 3 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python traps are free. This one comes with the all labs plan.

Why you need this

"Now" and "local time" in Python mean the machine's zone: the TZ setting, or the system's. datetime.now() and fromtimestamp() without a zone give naive local times. Here is one script, run on a UTC machine and then with Mumbai's zone:

from datetime import datetime

check_in = datetime.fromtimestamp(1792782000)
print(check_in, check_in.date())
$ python3 day.py
2026-10-23 19:00:00 2026-10-23
$ TZ=Asia/Kolkata python3 day.py
2026-10-24 00:30:00 2026-10-24

The lab's machine is UTC, like DabbaDrop's server. Code that leans on the machine's zone is right on one machine and wrong on the other. Keep times aware, and convert to the zone the business runs in, ZoneInfo("Asia/Kolkata"), at the moment you need a date or a clock time.

What you build: 3 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    repro.py: half past midnight, counted as yesterday

    Write reproduce() in repro.py. Build the gateway's time for a payment at 7:00 pm UTC on 23 October 2026, an aware datetime in UTC (tzinfo=timezone.utc), which is 00:30 on 24 October in Mumbai. Pass it to business_day from sales.py and return what it gives.

    The check runs your reproduce() twice: beside the original sales.py it must return '2026-10-23' (the UTC day), and beside a fixed sales.py it must return '2026-10-24' (Mumbai's day). No real waiting, no input() and no datetime.now(): build the time yourself, and reproduce() must finish within 3 seconds.

  2. Task 2

    sales.py: Mumbai's day, whatever the server's clock

    Fix sales.py so it goes by Mumbai's clock (Asia/Kolkata). business_day(paid_at), for an aware datetime in any zone (UTC from the gateway, +05:30, or another), gives the date in Mumbai at that instant as "YYYY-MM-DD". is_open(now), for an aware datetime in any zone, is True from 7:00 am (open) up to 10:00 pm (closed) Mumbai time, and False otherwise.

    The answers must not depend on the machine's own time zone. The check imports a fresh copy of your sales.py and passes its own times in UTC, in +05:30 and in other zones, right at the edges (18:30 UTC is Mumbai's midnight), then runs your file again in a fresh python3 with the machine's zone set to Asia/Kolkata and expects the same answers. Adding five and a half hours is right only for times that are in UTC.

  3. Task 3

    test_sales.py: a test that would have caught it

    Write a pytest file, test_sales.py, that would have caught this bug before it reached Rafiq's numbers. The check runs your file three times: beside the original sales.py, where at least one test must fail; beside the fixed sales.py, where every test must pass; and beside a wrong fix that adds five and a half hours to every time, where at least one test must fail.

    So test a UTC time just after midnight in Mumbai (between 18:30 and 24:00 UTC), and a time already given in +05:30 late in the evening, whose date must not move. Use fixed datetimes, never datetime.now(). No real waiting: each test must finish within 3 seconds.

What you will learn

  • repro.py: half past midnight, counted as yesterday
  • sales.py: Mumbai's day, whatever the server's clock
  • test_sales.py: a test that would have caught it

More in Time

Build "Naive versus aware datetimes" yourself

DabbaDrop runs on Python, and Kunal, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python traps are free. This one comes with the all labs plan.