Skip to content

Fix format_timespan() producing one minus sign per unit for negative detailed spans - #91

Open
ApparentlyTejas wants to merge 1 commit into
xolox:masterfrom
ApparentlyTejas:fix/negative-timespan-detailed
Open

ApparentlyTejas wants to merge 1 commit into
xolox:masterfrom
ApparentlyTejas:fix/negative-timespan-detailed

Conversation

@ApparentlyTejas

Copy link
Copy Markdown

format_timespan() produces malformed output for negative durations when detailed=True:

format_timespan(-3700, detailed=True)

'-1 hours, -1 minutes, -40 seconds and -0 nanoseconds'

expected something like: '-1 hour, 1 minute and 40 seconds'

Every unit gets its own minus sign, singular/plural is wrong (-1 hours instead of -1 hour), and a bogus -0 nanoseconds term leaks into the output.

Root cause: two bugs. First, the fast path (num_seconds < 60 and not detailed) was wrongly taken for any negative magnitude (e.g. -3700 < 60 is True), which also meant format_timespan(-3700, detailed=False) returned the unhelpful '-3700 seconds' instead of a proper breakdown. Second, with detailed=True the slow path ran Decimal division/modulo directly on the still-negative num_seconds, so each unit kept its own negative sign instead of the whole duration being decomposed as a magnitude with a single leading sign; a zero-valued unit could also render as the string '-0' (from round_number on Decimal('-0')), which the existing zero-filter only excluded by checking for '0', not '-0'.

Fix: changed the fast-path guard to abs(num_seconds) < 60, and in the slow path, decompose abs(num_seconds) into units, then prepend a single '-' to the final joined string if the original value was negative. This also incidentally fixes the -0 leakage since abs() ensures no unit count is ever negative.

Verified: format_timespan(-3700, detailed=True) -> '-1 hour, 1 minute and 40 seconds'; format_timespan(-90, detailed=True) -> '-1 minute and 30 seconds'; format_timespan(-3700, detailed=False) -> '-1 hour, 1 minute and 40 seconds' (previously '-3700 seconds'). Added 4 new assertions to the existing test_format_timespan test. Full suite passes with no regressions (pre-existing sandbox-only failures for pty/multiprocessing tests reproduce identically on unmodified master, confirmed via git stash).

…detailed spans

format_timespan(-3700, detailed=True) previously returned
'-1 hours, -1 minutes, -40 seconds and -0 nanoseconds' because the
detailed/slow code path ran Decimal division and modulo directly on
the still-negative num_seconds, so every unit kept its own sign, and
a spurious '-0' component leaked through since the zero-filtering
check only excluded the string '0', not '-0'.

Also fix the related issue where the fast path for small values
(abs < 60) used num_seconds < 60 instead of abs(num_seconds) < 60,
so large negative non-detailed spans like -3700 were never broken
into hours/minutes/seconds at all.

Now the sign is stripped before decomposing into units and a single
leading '-' is reapplied to the final formatted string.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant