mirror of
https://github.com/sui-feng-cb/AzurLaneAutoScript1.git
synced 2026-07-13 10:58:02 +08:00
35 lines
1.0 KiB
Python
35 lines
1.0 KiB
Python
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import re
|
||
|
|
from datetime import datetime
|
||
|
|
from typing import Optional
|
||
|
|
|
||
|
|
from module.logger import logger
|
||
|
|
|
||
|
|
|
||
|
|
DOWNTIME_FORMAT_RE = re.compile(
|
||
|
|
r'(\d{4})-(\d{1,2})-(\d{1,2}) '
|
||
|
|
r'(\d{1,2}):(\d{2})~(\d{1,2}):(\d{2})',
|
||
|
|
)
|
||
|
|
|
||
|
|
def parse_downtime(downtime: str) -> Optional[datetime]:
|
||
|
|
"""
|
||
|
|
Parse downtime window string into end datetime.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
downtime (str): Window string in format 'YYYY-MM-DD HH:MM~HH:MM'.
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Optional[datetime]: End datetime parsed from the window,
|
||
|
|
or None if no pattern matched.
|
||
|
|
"""
|
||
|
|
match = DOWNTIME_FORMAT_RE.search(downtime)
|
||
|
|
if not match:
|
||
|
|
logger.warning(f'Failed to parse downtime window: {downtime}')
|
||
|
|
return None
|
||
|
|
year, month, day = int(match.group(1)), int(match.group(2)), int(match.group(3))
|
||
|
|
end_hour, end_minute = int(match.group(6)), int(match.group(7))
|
||
|
|
end_dt = datetime(year, month, day, end_hour, end_minute)
|
||
|
|
logger.info(f'Parsed downtime window: end={end_dt:%Y-%m-%d %H:%M}')
|
||
|
|
return end_dt
|