diff options
Diffstat (limited to 'include/py/homekit/util.py')
-rw-r--r-- | include/py/homekit/util.py | 21 |
1 files changed, 20 insertions, 1 deletions
diff --git a/include/py/homekit/util.py b/include/py/homekit/util.py index 3c73440..f267488 100644 --- a/include/py/homekit/util.py +++ b/include/py/homekit/util.py @@ -12,7 +12,7 @@ import re import os from enum import Enum -from datetime import datetime +from datetime import datetime, timedelta from typing import Optional, List from zlib import adler32 @@ -255,6 +255,25 @@ def filesize_fmt(num, suffix="B") -> str: return f"{num:.1f} Yi{suffix}" +def seconds_to_human_readable_string(seconds: int) -> str: + duration = timedelta(seconds=seconds) + days, remainder = divmod(duration.total_seconds(), 86400) + hours, remainder = divmod(remainder, 3600) + minutes, seconds = divmod(remainder, 60) + + parts = [] + if days > 0: + parts.append(f"{int(days)} day{'s' if days > 1 else ''}") + if hours > 0: + parts.append(f"{int(hours)} hour{'s' if hours > 1 else ''}") + if minutes > 0: + parts.append(f"{int(minutes)} minute{'s' if minutes > 1 else ''}") + if seconds > 0: + parts.append(f"{int(seconds)} second{'s' if seconds > 1 else ''}") + + return ' '.join(parts) + + class HashableEnum(Enum): def hash(self) -> int: return adler32(self.name.encode()) |