2024-03-07 14:09:42 -07:00
|
|
|
import re
|
|
|
|
|
|
|
|
import humanfriendly
|
|
|
|
|
|
|
|
SYSTEMCTL_TIMESPAN_RE = re.compile(r'([0-9]+((\s[a-z]*)|([a-z]+)))')
|
|
|
|
|
|
|
|
|
2024-10-10 19:47:22 -06:00
|
|
|
def parse_systemctl_time_delta(time_str: str) -> float:
|
2024-03-07 14:09:42 -07:00
|
|
|
"""
|
|
|
|
https://humanfriendly.readthedocs.io/en/latest/api.html?highlight=parse_timespan#humanfriendly.parse_timespan
|
|
|
|
"""
|
|
|
|
time_str = time_str.replace(' left', '').replace(' ago', '')
|
|
|
|
spans = []
|
|
|
|
for part in re.findall(SYSTEMCTL_TIMESPAN_RE, time_str):
|
|
|
|
delta = part[0]
|
2024-04-01 13:45:08 -06:00
|
|
|
if 'month' in delta:
|
2024-03-07 14:09:42 -07:00
|
|
|
# humanfriendly does not support "months" so we convert it to weeks
|
|
|
|
num = int(delta.split(' ')[0])
|
|
|
|
delta = f'{num * 4} weeks'
|
|
|
|
spans.append(humanfriendly.parse_timespan(delta))
|
|
|
|
return sum(spans)
|