eight-hourr/backend/services/date_parser.py

26 lines
728 B
Python
Raw Normal View History

2026-07-17 06:55:36 +00:00
from __future__ import annotations
import re
from datetime import date
from pathlib import Path
def parse_dates_text(text: str) -> list[date]:
dates: list[date] = []
for line in text.splitlines():
line = line.strip()
if not line or line.startswith("#"):
continue
for token in re.split(r"[\s,;]+", line):
token = token.strip()
if token:
dates.append(date.fromisoformat(token))
return sorted(set(dates))
def parse_dates_file(path: str | Path) -> list[date]:
content = Path(path).read_text(encoding="utf-8")
dates = parse_dates_text(content)
if not dates:
raise ValueError(f"No dates found in {path}")
return dates