Add missing timeformat file
This commit is contained in:
25
src/emulsion/timeformat.py
Normal file
25
src/emulsion/timeformat.py
Normal 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])
|
||||
Reference in New Issue
Block a user