update
This commit is contained in:
@@ -5,6 +5,29 @@ import re
|
||||
from calendar import monthrange
|
||||
|
||||
|
||||
_DAY = r"(\d{4})[-/年](\d{1,2})[-/月](\d{1,2})日?"
|
||||
_CLOSED_DATES = re.compile(rf"(?<!\d){_DAY}(?:\s*(?:至|到|[~~—–])\s*{_DAY})?(?!\d)")
|
||||
|
||||
|
||||
def explicit_date_bounds(text: str) -> tuple[str, str] | None:
|
||||
"""Parse a complete date or a closed range, including Chinese date notation."""
|
||||
match = _CLOSED_DATES.fullmatch(text.strip())
|
||||
if not match:
|
||||
return None
|
||||
digits = match.groups()
|
||||
try:
|
||||
first = date(*map(int, digits[:3]))
|
||||
last = date(*map(int, digits[3:])) if digits[3] else first
|
||||
except ValueError:
|
||||
return None
|
||||
return (first.isoformat(), last.isoformat()) if first <= last else None
|
||||
|
||||
|
||||
def single_explicit_date_span(text: str) -> str:
|
||||
spans = list(_CLOSED_DATES.finditer(text))
|
||||
return spans[0].group() if len(spans) == 1 else ""
|
||||
|
||||
|
||||
def explicit_relative_period(message: str):
|
||||
"""Canonical whole periods only; mixed ranges and specific weekdays stay explicit.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user