52def match_to_datetime(match: re.Match) -> datetime | date:
53 """Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`.
54
55 Raises ValueError if the match does not correspond to a valid date
56 or datetime.
57 """
58 (
59 year_str,
60 month_str,
61 day_str,
62 hour_str,
63 minute_str,
64 sec_str,
65 micros_str,
66 zulu_time,
67 offset_sign_str,
68 offset_hour_str,
69 offset_minute_str,
71 year, month, day = int(year_str), int(month_str), int(day_str)
72 if hour_str is None:
73 return date(year, month, day)
74 hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
76 if offset_sign_str:
77 tz: tzinfo | None = cached_tz(
78 offset_hour_str, offset_minute_str, offset_sign_str
79 )
80 elif zulu_time:
82 else:
83 tz = None
84 return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz)
85
86
87@lru_cache(maxsize=None)