datetime and Timezones
Scenario: Servers in three countries write timestamps that must compare correctly.
New words, in plain English
- datetime - a date and clock time
- Timezone-aware - carrying an offset from UTC
- UTC - the shared global time reference
- timedelta - a duration used in date arithmetic
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 usecheckto prove the real end state.