25 lines
806 B
Python
25 lines
806 B
Python
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]
|
|
if 'month' in delta:
|
|
# 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)
|