Added the DATE_SPEC_RE constant to simplify the regular expression matching in the _parse_period_spec function.

This commit is contained in:
依瑪貓 2023-03-08 21:42:47 +08:00
parent ba3d8c6d4e
commit d64f354ee0

View File

@ -554,6 +554,10 @@ class YearPeriod(Period):
self.is_a_year = True self.is_a_year = True
DATE_SPEC_RE: str = r"(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?"
"""The regular expression of a date specification."""
def _parse_period_spec(text: str) \ def _parse_period_spec(text: str) \
-> tuple[datetime.date | None, datetime.date | None]: -> tuple[datetime.date | None, datetime.date | None]:
"""Parses the period specification. """Parses the period specification.
@ -565,17 +569,17 @@ def _parse_period_spec(text: str) \
""" """
if text == "-": if text == "-":
return None, None return None, None
m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) m = re.match(f"^{DATE_SPEC_RE}$", text)
if m is not None: if m is not None:
return __get_start(m[1], m[2], m[3]), \ return __get_start(m[1], m[2], m[3]), \
__get_end(m[1], m[2], m[3]) __get_end(m[1], m[2], m[3])
m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?-$", text) m = re.match(f"^{DATE_SPEC_RE}-$", text)
if m is not None: if m is not None:
return __get_start(m[1], m[2], m[3]), None return __get_start(m[1], m[2], m[3]), None
m = re.match(r"-(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) m = re.match(f"-{DATE_SPEC_RE}$", text)
if m is not None: if m is not None:
return None, __get_end(m[1], m[2], m[3]) return None, __get_end(m[1], m[2], m[3])
m = re.match(r"^(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?-(\d{4})(?:-(\d{2})(?:-(\d{2}))?)?$", text) m = re.match(f"^{DATE_SPEC_RE}-{DATE_SPEC_RE}$", text)
if m is not None: if m is not None:
return __get_start(m[1], m[2], m[3]), \ return __get_start(m[1], m[2], m[3]), \
__get_end(m[4], m[5], m[6]) __get_end(m[4], m[5], m[6])