0%

The Standard Library Exists · practice

Measure Time with datetime

The first briefing job sounds simple: find how many days separate two dates. The data arrives as text such as "2026-08-03". Subtracting those would not mean calendar arithmetic to Python.

The datetime documentation gives the route:

  • date.fromisoformat accepts an ISO date string and returns a date ;

  • subtracting one date from another returns a timedelta;

  • a timedelta exposes the whole-day distance through .days.

Try the contract in a small example:

Try it

The result is 8, not 9: this is distance between dates, not an inclusive count of calendar labels. Reversing the two dates produces -8. We will keep that signed behavior because it preserves useful information instead of hiding an accidentally reversed input.

Let the API keep its failure contract

date.fromisoformat("next Tuesday") raises . That is useful at this layer. days_between does not know whether a future interface should ask again, reject a data row, or show an error. Catching the error here would force that policy into a small conversion .

The later error-handling chapter will decide what an interface should do. For now, let the documented exception reach the caller.

What does date.fromisoformat("2026-08-11") return?

Implement days_between in the workspace. Try it on a few date pairs of your own before you submit, including two that are the same day, and a pair given in reverse order.

Task

Implement days_between(start_text, end_text) in activity_report.py.

  • Parse both with datetime.date.fromisoformat.

  • Return (end_date - start_date).days.

  • Keep the signed result when the end is earlier than the start.

  • Let invalid ISO date text raise .

You may import the datetime or import date from it. Renaming either import is also fine; the public behavior and the documented API are the contract.