datetime and Timezones

Scenario: Servers in three countries write timestamps that must compare correctly.

New words, in plain English

Prefer aware timestamps such as datetime.now(timezone.utc). Parse ISO text with fromisoformat, format with isoformat, and use timedelta for durations. Human calendar zones may require zoneinfo.

Analogy: A Python program is like a clear set of instructions for a careful helper: names label things, indentation groups steps, and errors explain where the helper became confused.

A small, real example

from datetime import datetime, timezone, timedelta
now = datetime.now(timezone.utc)
expires = now + timedelta(minutes=15)
print(expires.isoformat())
Tip: Do not compare naive and aware datetimes; choose a timezone policy early.
Goal: Practice this idea in the py-regex-datetime challenge at /labs/python. Fix the broken program, run it yourself, then use check to prove the real end state.