Repository navigation
Fix format_timespan() producing one minus sign per unit for negative detailed spans - #91
Open
ApparentlyTejas wants to merge 1 commit into
Open
ApparentlyTejas wants to merge 1 commit into
ApparentlyTejas wants to merge 1 commit into
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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).