Add missing timeformat file

This commit is contained in:
Alexander Wainwright
2026-02-01 21:03:36 +10:00
parent 67e073714e
commit 22c7b3cc8a

View File

@@ -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])