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]+)))')
|
|
|
|
|
|
|
|
|
|
|
|
def parse_systemctl_time_delta(time_str: str):
|
|
|
|
"""
|
|
|
|
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'
|
|
|
|
try:
|
|
|
|
spans.append(humanfriendly.parse_timespan(delta))
|
|
|
|
except humanfriendly.InvalidTimespan as e:
|
|
|
|
return e
|
|
|
|
return sum(spans)
|