reworked Duration

This commit is contained in:
Martin Wagner 2022-05-28 14:16:56 +02:00
parent 3e2f8c51d2
commit 7e8b169705
1 changed files with 16 additions and 19 deletions

View File

@ -28,7 +28,6 @@ import urllib.error
import threading import threading
import functools import functools
import itertools import itertools
import datetime
import collections import collections
import os import os
import sys import sys
@ -482,34 +481,32 @@ class MPRISInterface: # TODO emit Seeked if needed
###################### ######################
class Duration(): class Duration():
def __init__(self, value=None): def __init__(self, seconds=None):
if value is None: if seconds is None:
self._fallback=True self._fallback=True
self._value=0.0 self._seconds=0.0
else: else:
self._fallback=False self._fallback=False
self._value=float(value) self._seconds=float(seconds)
def __str__(self): def __str__(self):
if self._fallback: if self._fallback:
return "" return ""
else: else:
if self._value < 0: seconds=int(self._seconds)
sign="" days,seconds=divmod(seconds, 86400) # 86400 seconds make a day
value=-int(self._value) hours,seconds=divmod(seconds, 3600) # 3600 seconds make an hour
minutes,seconds=divmod(seconds, 60)
if days > 0:
days_string=ngettext("{days} day", "{days} days", days).format(days=days)
return f"{days_string}, {hours:02d}{minutes:02d}{seconds:02d}"
elif hours > 0:
return f"{hours}{minutes:02d}{seconds:02d}"
else: else:
sign="" return f"{minutes:02d}{seconds:02d}"
value=int(self._value)
delta=datetime.timedelta(seconds=value)
if delta.days > 0:
days=ngettext("{days} day", "{days} days", delta.days).format(days=delta.days)
time_string=f"{days}, {datetime.timedelta(seconds=delta.seconds)}"
else:
time_string=str(delta).lstrip("0").lstrip(":")
return sign+time_string.replace(":", "") # use 'ratio' as delimiter
def __float__(self): def __float__(self):
return self._value return self._seconds
class LastModified(): class LastModified():
def __init__(self, date): def __init__(self, date):
@ -1554,7 +1551,7 @@ class AlbumPopover(Gtk.Popover):
self._songs_list.clear() self._songs_list.clear()
tag_filter=("albumartist", albumartist, "album", album, "date", date) tag_filter=("albumartist", albumartist, "album", album, "date", date)
count=self._client.count(*tag_filter) count=self._client.count(*tag_filter)
duration=str(Duration(float(count["playtime"]))) duration=str(Duration(count["playtime"]))
length=int(count["songs"]) length=int(count["songs"])
text=ngettext("{number} song ({duration})", "{number} songs ({duration})", length).format(number=length, duration=duration) text=ngettext("{number} song ({duration})", "{number} songs ({duration})", length).format(number=length, duration=duration)
self._label.set_text(text) self._label.set_text(text)