You will build a small but properly engineered Python CLI that computes date differences and future dates with timezone awareness. By the end you will have a tested tool with clean structure and packaging.

Learning Objectives

  • Structure a CLI app with argparse subcommands.
  • Do correct timezone-aware date math.
  • Cover the logic with pytest.
  • Time: ~6 hours · Difficulty: Intermediate · Prereqs: Python fundamentals.

Architecture Overview

Environment Setup

  • Python 3.11 in a venv with pytest.
  • A project layout with src/ and tests/.

Step-by-Step Execution

01
Implement the core date logic
from datetime import date, timedelta
def days_between(a: date, b: date) -> int:
    return abs((b - a).days)
02
Wire up the CLI
python -m datecalc diff 2026-06-17 2026-12-31
03
Run the tests
$ pytest -q
.... 4 passed in 0.05s

Progress So Far

Testing & Validation

python -m datecalc diff 2026-06-17 2026-12-31 && pytest -q

The CLI should print 197 days and the test suite should pass. If both hold, the app is correct and shippable.

Troubleshooting
  • Off-by-one across DST: use timezone-aware datetimes for time-of-day math.
  • Import errors: run as a module (python -m) so package imports resolve.
  • Bad input crashes: validate date strings and exit non-zero with a message.

Extension Ideas

Key Results

  • Shipped a working timezone-aware date CLI.
  • Covered the core logic with a passing pytest suite.
  • Separated logic, CLI, and tests cleanly.
  • Handled invalid input without crashing.