mirror of
https://github.com/codex-storage/deluge.git
synced 2025-02-02 14:44:21 +00:00
Prevent time formatting crash when seconds were floats
Update docstring and tests for ftime supporting floats Truncate rather than round floats in ftime
This commit is contained in:
parent
1e6c02ae83
commit
e8e649a030
@ -483,7 +483,7 @@ def ftime(secs):
|
||||
"""Formats a string to show time in a human readable form.
|
||||
|
||||
Args:
|
||||
secs (int): The number of seconds.
|
||||
secs (int or float): The number of seconds.
|
||||
|
||||
Returns:
|
||||
str: A formatted time string or empty string if value is 0.
|
||||
@ -497,20 +497,22 @@ def ftime(secs):
|
||||
|
||||
"""
|
||||
|
||||
# Handle floats by truncating to an int
|
||||
secs = int(secs)
|
||||
if secs <= 0:
|
||||
time_str = ''
|
||||
elif secs < 60:
|
||||
time_str = '{:d}s'.format(secs)
|
||||
time_str = '{}s'.format(secs)
|
||||
elif secs < 3600:
|
||||
time_str = '{:d}m {:d}s'.format(secs // 60, secs % 60)
|
||||
time_str = '{}m {}s'.format(secs // 60, secs % 60)
|
||||
elif secs < 86400:
|
||||
time_str = '{:d}h {:d}m'.format(secs // 3600, secs // 60 % 60)
|
||||
time_str = '{}h {}m'.format(secs // 3600, secs // 60 % 60)
|
||||
elif secs < 604800:
|
||||
time_str = '{:d}d {:d}h'.format(secs // 86400, secs // 3600 % 24)
|
||||
time_str = '{}d {}h'.format(secs // 86400, secs // 3600 % 24)
|
||||
elif secs < 31449600:
|
||||
time_str = '{:d}w {:d}d'.format(secs // 604800, secs // 86400 % 7)
|
||||
time_str = '{}w {}d'.format(secs // 604800, secs // 86400 % 7)
|
||||
else:
|
||||
time_str = '{:d}y {:d}w'.format(secs // 31449600, secs // 604800 % 52)
|
||||
time_str = '{}y {}w'.format(secs // 31449600, secs // 604800 % 52)
|
||||
|
||||
return time_str
|
||||
|
||||
|
@ -53,15 +53,17 @@ class CommonTestCase(unittest.TestCase):
|
||||
self.assertTrue(fpeer(10, -1) == '10')
|
||||
|
||||
def test_ftime(self):
|
||||
self.assertTrue(ftime(0) == '')
|
||||
self.assertTrue(ftime(5) == '5s')
|
||||
self.assertTrue(ftime(100) == '1m 40s')
|
||||
self.assertTrue(ftime(3789) == '1h 3m')
|
||||
self.assertTrue(ftime(23011) == '6h 23m')
|
||||
self.assertTrue(ftime(391187) == '4d 12h')
|
||||
self.assertTrue(ftime(604800) == '1w 0d')
|
||||
self.assertTrue(ftime(13893086) == '22w 6d')
|
||||
self.assertTrue(ftime(59740269) == '1y 46w')
|
||||
self.assertEqual(ftime(0), '')
|
||||
self.assertEqual(ftime(5), '5s')
|
||||
self.assertEqual(ftime(100), '1m 40s')
|
||||
self.assertEqual(ftime(3789), '1h 3m')
|
||||
self.assertEqual(ftime(23011), '6h 23m')
|
||||
self.assertEqual(ftime(391187), '4d 12h')
|
||||
self.assertEqual(ftime(604800), '1w 0d')
|
||||
self.assertEqual(ftime(13893086), '22w 6d')
|
||||
self.assertEqual(ftime(59740269), '1y 46w')
|
||||
self.assertEqual(ftime(61.25), '1m 1s')
|
||||
self.assertEqual(ftime(119.9), '1m 59s')
|
||||
|
||||
def test_fdate(self):
|
||||
self.assertTrue(fdate(-1) == '')
|
||||
|
Loading…
x
Reference in New Issue
Block a user