[version_table] Improve date parsing (#527)

So far the script only allowed datetimes. Now full/partial dates are allowed too.
This commit is contained in:
Marc Wrobel
2025-11-08 13:18:14 +01:00
committed by GitHub
parent 03905bcf79
commit 1fb86114be
3 changed files with 13 additions and 7 deletions

View File

@@ -93,6 +93,15 @@ def parse_datetime(text: str, formats: list[str] = frozenset([
raise ValueError(msg)
def parse__datetime_or_date_or_month_year_date(text: str) -> datetime.datetime:
"""Parse a given text representing a datetime, date or a partial date using the default list of formats.
"""
try:
return parse_datetime(text)
except ValueError:
return parse_date_or_month_year_date(text)
def date(year: int, month: int, day: int) -> datetime.datetime:
"""Create a datetime object with the given year, month and day, at midnight."""
return datetime.datetime(year, month, day, tzinfo=datetime.timezone.utc)

View File

@@ -138,16 +138,13 @@ class Field:
str_value = self.template.render(**match.groupdict()) if self.template else raw_value
if self.type == "date":
try:
return dates.parse_date(str_value)
except ValueError:
return dates.parse_month_year_date(str_value)
return dates.parse_date_or_month_year_date(str_value)
elif self.type == "range":
if self.type == "range":
items = RANGE_LIST_SEPARATOR_PATTERN.split(str_value)
return f"{items[0]} - {items[-1]}" if len(items) > 1 else str_value
elif self.type == "identifier":
if self.type == "identifier":
return endoflife.to_identifier(str_value)
return str_value

View File

@@ -59,7 +59,7 @@ with ProductData(config.product) as product_data:
continue
version_name = config.render(version_match)
version_date = dates.parse_datetime(cells[version_date_index])
version_date = dates.parse__datetime_or_date_or_month_year_date(cells[version_date_index])
product_data.declare_version(version_name, version_date)
except ValueError as e: