
A data job spans a weekend and its duration dashboard shows only a handful of hours. Reading the seconds component of a timedelta discards complete days. The full duration includes complete days and fractional hours. A component intended for human formatting is the wrong source for a total.
A fixture for the behaviour
The script runs timedelta.total_seconds against three independent inputs. Each call starts from the value shown, so changing one example leaves the other expectations available for comparison. The table records real return values from the example. Quotation marks around text in the table make its boundaries visible.
from datetime import timedelta
def evaluate(value):
return timedelta(hours=value).total_seconds()
for sample in [3, 27, 49.5]:
print(evaluate(sample))Inspect the boundary cases
| Input to evaluate | Observed result |
|---|---|
| 3 | 10800.0 |
| 27 | 97200.0 |
| 49.5 | 178200.0 |
For 3, the observed output is 10800.0. With 27, it becomes 97200.0. The useful comparison is at the application boundary. Run the pair against the input as it reaches the program after decoding or parsing, then inspect what the next component receives.
The value for 49.5 is 178200.0. Record the unit or indexing convention next to that expectation. A second implementation can return the same type and still disagree about where a boundary falls or which observations enter the calculation.
Check the calling code
A wall-clock timestamp subtraction across local clock changes can require conversion to UTC first. Use a monotonic clock for an in-process elapsed timer.
A practical test can begin with assert evaluate(3) == 10800.0. Add the other rows as separate expectations so one failure identifies the input that changed. For a service fed by stored data, repeat the check on one anonymized record from an earlier version of its file or message format.
One more fixture before integrating
Test a duration longer than twenty-four hours before building a human-readable hours-and-minutes string. A duration of twenty-seven hours should still be recoverable from the storage value even if the UI chooses to display one day and three hours.
The same Python function was also run with 0 / -1 / 4. These additional inputs change the boundary without changing the implementation. An exception name in the table records where the operation rejects an input; a returned empty or zero value records a successful call. Choose deliberately which of those outcomes the surrounding feature should expose.
| Additional fixture | Result or exception |
|---|---|
| 0 | 0.0 |
| -1 | -3600.0 |
| 4 | 14400.0 |
Python’s datetime reference · Python standard library
Sources and further reading
Cover photograph: Wikimedia Morocco Mini-Hackathon 2026 day 1 - Person giving thumbs up at desk with laptop and water bottle - 0017 by DERDAKI ILIAS. CC BY-SA 4.0. Cropped and typeset for this cover. The photograph illustrates the topic. This image adaptation uses the same share-alike license.
