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