Fix negative playtimes being considered positive
This commit is contained in:
parent
869e0e0fe0
commit
4e67735de8
|
@ -1,4 +1,5 @@
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
* Fix negative playtimes being considered positive
|
||||||
* Add %d for days to common.util.time.format_duration
|
* Add %d for days to common.util.time.format_duration
|
||||||
* Set up tests, add tests for common.util.time
|
* Set up tests, add tests for common.util.time
|
||||||
* Display total hours played on homepage
|
* Display total hours played on homepage
|
||||||
|
|
|
@ -23,6 +23,9 @@ def format_duration(
|
||||||
hour_seconds = 60 * minute_seconds
|
hour_seconds = 60 * minute_seconds
|
||||||
day_seconds = 24 * hour_seconds
|
day_seconds = 24 * hour_seconds
|
||||||
seconds_total = int(duration.total_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)
|
days, remainder = divmod(seconds_total, day_seconds)
|
||||||
hours, remainder = divmod(remainder, hour_seconds)
|
hours, remainder = divmod(remainder, hour_seconds)
|
||||||
minutes, seconds = divmod(remainder, minute_seconds)
|
minutes, seconds = divmod(remainder, minute_seconds)
|
||||||
|
|
|
@ -51,3 +51,8 @@ class FormatDurationTest(unittest.TestCase):
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
result, "50 days, 10 hours, 34 minutes, 24 seconds, 4358064 total seconds"
|
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")
|
||||||
|
|
Loading…
Reference in New Issue