Fix edge case in format_duration

Fixes #65

```python
def test_specific_precise_if_unncessary(self):
        delta = timedelta(hours=2, minutes=40)
        result = format_duration(delta, "%02.0H:%02.0m")
        self.assertEqual(result, "02:40")
```
This test fails by returning "03:40" instead. The problem is in the way `format_duration` handles fractional hours.
To fix it, we need to switch between using hours and fractional hours
depending on if minutes are present in the formatted string.
This commit is contained in:
Lukáš Kucharczyk 2023-11-10 20:07:41 +01:00
parent 87f50cedd2
commit 09faa8e6fd
2 changed files with 12 additions and 2 deletions

View File

@ -44,7 +44,7 @@ def format_duration(
# timestamps where end is before start # timestamps where end is before start
if seconds_total < 0: if seconds_total < 0:
seconds_total = 0 seconds_total = 0
days = hours = minutes = seconds = 0 days = hours = hours_float = minutes = seconds = 0
remainder = seconds = seconds_total remainder = seconds = seconds_total
if "%d" in format_string: if "%d" in format_string:
days, remainder = divmod(seconds_total, day_seconds) days, remainder = divmod(seconds_total, day_seconds)
@ -55,7 +55,7 @@ def format_duration(
minutes, seconds = divmod(remainder, minute_seconds) minutes, seconds = divmod(remainder, minute_seconds)
literals = { literals = {
"d": str(days), "d": str(days),
"H": str(hours), "H": str(hours) if "m" not in format_string else str(hours_float),
"m": str(minutes), "m": str(minutes),
"s": str(seconds), "s": str(seconds),
"r": str(seconds_total), "r": str(seconds_total),

View File

@ -83,6 +83,16 @@ class FormatDurationTest(unittest.TestCase):
result = format_duration(delta, "%r seconds") result = format_duration(delta, "%r seconds")
self.assertEqual(result, "0 seconds") self.assertEqual(result, "0 seconds")
def test_specific(self):
delta = timedelta(hours=2, minutes=40)
result = format_duration(delta, "%H:%m")
self.assertEqual(result, "2:40")
def test_specific_precise_if_unncessary(self):
delta = timedelta(hours=2, minutes=40)
result = format_duration(delta, "%02.0H:%02.0m")
self.assertEqual(result, "02:40")
def test_all_at_once(self): def test_all_at_once(self):
delta = timedelta(days=50, hours=10, minutes=34, seconds=24) delta = timedelta(days=50, hours=10, minutes=34, seconds=24)
result = format_duration( result = format_duration(