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:
@ -44,7 +44,7 @@ def format_duration(
|
||||
# timestamps where end is before start
|
||||
if seconds_total < 0:
|
||||
seconds_total = 0
|
||||
days = hours = minutes = seconds = 0
|
||||
days = hours = hours_float = minutes = seconds = 0
|
||||
remainder = seconds = seconds_total
|
||||
if "%d" in format_string:
|
||||
days, remainder = divmod(seconds_total, day_seconds)
|
||||
@ -55,7 +55,7 @@ def format_duration(
|
||||
minutes, seconds = divmod(remainder, minute_seconds)
|
||||
literals = {
|
||||
"d": str(days),
|
||||
"H": str(hours),
|
||||
"H": str(hours) if "m" not in format_string else str(hours_float),
|
||||
"m": str(minutes),
|
||||
"s": str(seconds),
|
||||
"r": str(seconds_total),
|
||||
|
@ -83,6 +83,16 @@ class FormatDurationTest(unittest.TestCase):
|
||||
result = format_duration(delta, "%r 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):
|
||||
delta = timedelta(days=50, hours=10, minutes=34, seconds=24)
|
||||
result = format_duration(
|
||||
|
Reference in New Issue
Block a user