diff --git a/src/emulsion/timeformat.py b/src/emulsion/timeformat.py new file mode 100644 index 0000000..2ec8ec2 --- /dev/null +++ b/src/emulsion/timeformat.py @@ -0,0 +1,25 @@ +# Source - https://stackoverflow.com/a/24542445 +# Posted by Mr. B, modified by community. See post 'Timeline' for change history +# Retrieved 2026-01-31, License - CC BY-SA 4.0 + +intervals = ( + ('yr', 31536000), # 60 * 60 * 24 * 365 + ('mo', 2592000), # 60 * 60 * 24 * 30 + ('wks', 604800), # 60 * 60 * 24 * 7 + ('d', 86400), # 60 * 60 * 24 + ('h', 3600), # 60 * 60 + ('m', 60), + ('s', 1), +) + +def format_time(seconds: float, granularity: int = 2) -> str: + result = [] + + for name, count in intervals: + value = seconds // count + if value: + seconds -= value * count + if value == 1: + name = name.rstrip('s') + result.append(f"{value}{name}") + return ' '.join(result[:granularity])