| 1 |
# SPDX-License-Identifier: MIT |
| 2 |
# SPDX-FileCopyrightText: 2021 Taneli Hukkinen |
| 3 |
# Licensed to PSF under a Contributor Agreement. |
| 4 |
|
| 5 |
from __future__ import annotations |
| 6 |
|
| 7 |
from datetime import date, datetime, time, timedelta, timezone, tzinfo |
| 8 |
from functools import lru_cache |
| 9 |
import re |
| 10 |
|
| 11 |
TYPE_CHECKING = False |
| 12 |
if TYPE_CHECKING: |
| 13 |
from typing import Any |
| 14 |
|
| 15 |
from ._types import ParseFloat |
| 16 |
|
| 17 |
# E.g. |
| 18 |
# - 00:32:00.999999 |
| 19 |
# - 00:32:00 |
| 20 |
_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?" |
| 21 |
|
| 22 |
RE_NUMBER = re.compile( |
| 23 |
r""" |
| 24 |
0 |
| 25 |
(?: |
| 26 |
x[0-9A-Fa-f](?:_?[0-9A-Fa-f])* # hex |
| 27 |
| |
| 28 |
b[01](?:_?[01])* # bin |
| 29 |
| |
| 30 |
o[0-7](?:_?[0-7])* # oct |
| 31 |
) |
| 32 |
| |
| 33 |
[+-]?(?:0|[1-9](?:_?[0-9])*) # dec, integer part |
| 34 |
(?P<floatpart> |
| 35 |
(?:\.[0-9](?:_?[0-9])*)? # optional fractional part |
| 36 |
(?:[eE][+-]?[0-9](?:_?[0-9])*)? # optional exponent part |
| 37 |
) |
| 38 |
""", |
| 39 |
flags=re.VERBOSE, |
| 40 |
) |
| 41 |
RE_LOCALTIME = re.compile(_TIME_RE_STR) |
| 42 |
RE_DATETIME = re.compile( |
| 43 |
rf""" |
| 44 |
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27 |
| 45 |
(?: |
| 46 |
[Tt ] |
| 47 |
{_TIME_RE_STR} |
| 48 |
(?:([Zz])|([+-])([01][0-9]|2[0-3]):([0-5][0-9]))? # optional time offset |
| 49 |
)? |
| 50 |
""", |
| 51 |
flags=re.VERBOSE, |
| 52 |
) |
| 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 |
| 91 |
# that matched RE_DATETIME, so there is an implicit bound of |
| 92 |
# 24 (hours) * 60 (minutes) * 2 (offset direction) = 2880. |
| 93 |
@lru_cache(maxsize=None) |
| 94 |
def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone: |
| 95 |
sign = 1 if sign_str == "+" else -1 |
| 96 |
return timezone( |
| 97 |
timedelta( |
| 98 |
hours=sign * int(hour_str), |
| 99 |
minutes=sign * int(minute_str), |
| 100 |
) |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
def match_to_localtime(match: re.Match[str]) -> time: |
| 105 |
hour_str, minute_str, sec_str, micros_str = match.groups() |
| 106 |
micros = int(micros_str.ljust(6, "0")) if micros_str else 0 |
| 107 |
return time(int(hour_str), int(minute_str), int(sec_str), micros) |
| 108 |
|
| 109 |
|
| 110 |
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any: |
| 111 |
if match.group("floatpart"): |
| 112 |
return parse_float(match.group()) |
| 113 |
return int(match.group(), 0) |