Files
zfs-backup/zfs_backup/time.py
T
2022-12-06 14:59:55 +01:00

40 lines
1.5 KiB
Python

from __future__ import annotations
from datetime import datetime
class Time:
@staticmethod
def _get_date_format() -> str:
"""date format as used by zpool status
%a: day of week, short version
%b: month, short version
%d: day of month, zero-padded
%H, %M, %S: hour, minute, second, zero-padded
%Y: year
"""
return "%a %b %d %H:%M:%S %Y"
@classmethod
def _extract_datetime_segment(cls, line_with_datetime: str) -> str:
"""From the line in `zpool status` that includes the date and time, extract only the part with date and time."""
return line_with_datetime.split(" errors on ")[-1]
@classmethod
def _get_line_with_datetime_from_status_output(cls, output: str) -> str:
"""From `zpool status`, extract the line that contains the date and time of the last scrub."""
lines = output.split("\n")
for line in lines:
if line.startswith(" scan: scrub repaired"):
return line.strip()
else:
raise ValueError("Could not find the correct line")
@classmethod
def get_last_scrub_from_status_output(cls, output: str) -> datetime:
"""Given the output of `zfs status`, return the `datetime` object of the last scrub."""
line_with_datetime = cls._get_line_with_datetime_from_status_output(output)
date_str = cls._extract_datetime_segment(line_with_datetime)
return datetime.strptime(date_str, cls._get_date_format())