androidengineers.Book a session

Python 07 · Files and interchange formats

Dates, time zones, and configuration values

articleSelf-paced

Learn the concept

An event timestamp needs a clear time-zone interpretation. Naive datetimes contain no timezone information; aware datetimes do. Use explicit timezone-aware values for stored events and convert for display. Duration measurement should use a monotonic clock rather than subtracting wall-clock values that can change.

Configuration often arrives as environment-variable strings. Parse and validate it once near startup. The string "false" is nonempty and therefore truthy; convert Boolean configuration explicitly. Missing required configuration should cause a clear startup error.

Avoid placing secrets in source files or examples that will be committed. Log the name of a missing setting rather than printing its secret value. Treat configuration as part of the application contract, with defaults only where they are safe.

Run and inspect

from datetime import datetime, timezone

now = datetime.now(timezone.utc)
assert now.utcoffset().total_seconds() == 0
def parse_flag(text):
    if text not in {"true", "false"}:
        raise ValueError("Expected true or false")
    return text == "true"
assert parse_flag("false") is False

Your exercise

Parse an aware ISO timestamp and a Boolean configuration setting. Reject an ambiguous timestamp unless your application defines a timezone policy.

Check your understanding

You distinguish event timestamps from elapsed time and never rely on bool("false") to parse configuration.

YOUR LEARNING JOURNEY

0 of 89 available lessons completed

Progress saved in this browser. No account needed.
Dates, time zones, and configuration values | AI Engineer | Android Engineers