Date Calculator CLI Application in Python
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
graph LR
CLI[argparse CLI] --> Core[date logic module]
Core --> TZ[zoneinfo aware math]
Tests[pytest] --> Core
Environment Setup
- Python 3.11 in a venv with pytest.
- A project layout with
src/andtests/.
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-3103
Run the tests
$ pytest -q
.... 4 passed in 0.05s
Progress So Far
graph LR
A[01 Core logic] -->|done| B[02 CLI]
B -->|done| C[03 Tests]
style A fill:#1a4a1a,stroke:#00ff00,color:#fff
style B fill:#1a4a1a,stroke:#00ff00,color:#fff
style C fill:#1a4a1a,stroke:#00ff00,color:#fff
Testing & Validation
python -m datecalc diff 2026-06-17 2026-12-31 && pytest -qThe 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
- Package and publish to a private index.
- Add the CLI conventions from CLI Tools with argparse.
- Add CI with GitHub Actions.
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.