From bd53ac05f63653fdebb25e5fdc92d747afb44b62 Mon Sep 17 00:00:00 2001 From: Carey Metcalfe Date: Sun, 8 Sep 2024 14:38:35 -0400 Subject: [PATCH] Fix track duration display --- supysonic/db.py | 8 +++++--- tests/base/test_db.py | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/supysonic/db.py b/supysonic/db.py index e7bfa58..d273b95 100644 --- a/supysonic/db.py +++ b/supysonic/db.py @@ -412,9 +412,11 @@ class Track(PathMixin, _Model): return mimetypes.guess_type(self.path, False)[0] or "application/octet-stream" def duration_str(self): - ret = f"{(self.duration % 3600) / 60:02}:{self.duration % 60:02}" - if self.duration >= 3600: - ret = f"{self.duration / 3600:02}:{ret}" + m, s = divmod(self.duration, 60) + h, m = divmod(m, 60) + ret = f"{m:02}:{s:02}" + if h: + ret = f"{h:02}:{ret}" return ret def suffix(self): diff --git a/tests/base/test_db.py b/tests/base/test_db.py index bde95d0..58890d6 100644 --- a/tests/base/test_db.py +++ b/tests/base/test_db.py @@ -59,7 +59,7 @@ class DbTestCase(unittest.TestCase): artist=artist, disc=1, number=1, - duration=3, + duration=3599, has_art=True, bitrate=320, path="tests/assets/formats/silence.ogg", @@ -74,7 +74,7 @@ class DbTestCase(unittest.TestCase): artist=artist, disc=1, number=2, - duration=5, + duration=3600, bitrate=96, path="tests/assets/23bytes", last_modification=1234, @@ -223,6 +223,9 @@ class DbTestCase(unittest.TestCase): def test_track(self): track1, track2 = self.create_some_tracks() + assert track1.duration_str() == "59:59" + assert track2.duration_str() == "01:00:00" + # Assuming SQLite doesn't enforce foreign key constraints MockUser = namedtuple("User", ["id"]) user = MockUser(uuid.uuid4())