Fix negative playtimes being considered positive

This commit is contained in:
Lukáš Kucharczyk 2023-01-05 17:13:45 +01:00
parent 869e0e0fe0
commit 4e67735de8
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
3 changed files with 9 additions and 0 deletions

View File

@ -1,4 +1,5 @@
## Unreleased
* Fix negative playtimes being considered positive
* Add %d for days to common.util.time.format_duration
* Set up tests, add tests for common.util.time
* Display total hours played on homepage

View File

@ -23,6 +23,9 @@ def format_duration(
hour_seconds = 60 * minute_seconds
day_seconds = 24 * hour_seconds
seconds_total = int(duration.total_seconds())
# timestamps where end is before start
if seconds_total < 0:
seconds_total = 0
days, remainder = divmod(seconds_total, day_seconds)
hours, remainder = divmod(remainder, hour_seconds)
minutes, seconds = divmod(remainder, minute_seconds)

View File

@ -51,3 +51,8 @@ class FormatDurationTest(unittest.TestCase):
self.assertEqual(
result, "50 days, 10 hours, 34 minutes, 24 seconds, 4358064 total seconds"
)
def test_negative(self):
delta = timedelta(hours=-2)
result = format_duration(delta, "%H hours")
self.assertEqual(result, "0 hours")