Skip to content

Commit

Permalink
Round partial seconds
Browse files Browse the repository at this point in the history
Previously, partial seconds could sometimes be rounded up by the
formatter resulting in "XX:60" being displayed briefly. If we always
round first, we can avoid this.
  • Loading branch information
DJtheRedstoner authored and avylove committed Sep 3, 2023
1 parent 99f500d commit 0476ab9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
8 changes: 4 additions & 4 deletions enlighten/_util.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

# -*- coding: utf-8 -*-
# Copyright 2017 - 2022 Avram Lubkin, All Rights Reserved
# Copyright 2017 - 2023 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -76,9 +76,9 @@ def format_time(seconds):
"""

# Always do minutes and seconds in mm:ss format
minutes = seconds // 60
hours = minutes // 60
rtn = u'%02.0f:%02.0f' % (minutes % 60, seconds % 60)
minutes, seconds = divmod(round(seconds), 60)
hours, minutes = divmod(minutes, 60)
rtn = u'%02d:%02d' % (minutes, seconds)

# Add hours if there are any
if hours:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2017 - 2022 Avram Lubkin, All Rights Reserved
# Copyright 2017 - 2023 Avram Lubkin, All Rights Reserved

# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -29,10 +29,13 @@ def test_seconds(self):
self.assertEqual(format_time(0), '00:00')
self.assertEqual(format_time(6), '00:06')
self.assertEqual(format_time(42), '00:42')
self.assertEqual(format_time(48.2), '00:48')
self.assertEqual(format_time(52.6), '00:53')

def test_minutes(self):
"""Verify minutes formatting"""

self.assertEqual(format_time(59.9), '01:00')
self.assertEqual(format_time(60), '01:00')
self.assertEqual(format_time(128), '02:08')
self.assertEqual(format_time(1684), '28:04')
Expand Down

0 comments on commit 0476ab9

Please sign in to comment.