Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`. Raises ValueError if the match does not correspond to a valid date or datetime.
(match: re.Match[str])
| 57 | |
| 58 | |
| 59 | def match_to_datetime(match: re.Match[str]) -> datetime | date: |
| 60 | """Convert a `RE_DATETIME` match to `datetime.datetime` or `datetime.date`. |
| 61 | |
| 62 | Raises ValueError if the match does not correspond to a valid date |
| 63 | or datetime. |
| 64 | """ |
| 65 | ( |
| 66 | year_str, |
| 67 | month_str, |
| 68 | day_str, |
| 69 | hour_str, |
| 70 | minute_str, |
| 71 | sec_str, |
| 72 | micros_str, |
| 73 | zulu_time, |
| 74 | offset_sign_str, |
| 75 | offset_hour_str, |
| 76 | offset_minute_str, |
| 77 | ) = match.groups() |
| 78 | year, month, day = int(year_str), int(month_str), int(day_str) |
| 79 | if hour_str is None: |
| 80 | return date(year, month, day) |
| 81 | hour, minute = int(hour_str), int(minute_str) |
| 82 | sec = int(sec_str) if sec_str else 0 |
| 83 | micros = int(micros_str.ljust(6, "0")) if micros_str else 0 |
| 84 | if offset_sign_str: |
| 85 | tz: tzinfo | None = cached_tz( |
| 86 | offset_hour_str, offset_minute_str, offset_sign_str |
| 87 | ) |
| 88 | elif zulu_time: |
| 89 | tz = timezone.utc |
| 90 | else: # local date-time |
| 91 | tz = None |
| 92 | return datetime(year, month, day, hour, minute, sec, micros, tzinfo=tz) |
| 93 | |
| 94 | |
| 95 | # No need to limit cache size. This is only ever called on input |