|
14 | 14 |
|
15 | 15 | from ._types import ParseFloat |
16 | 16 |
|
17 | | -# E.g. |
18 | | -# - 00:32:00.999999 |
19 | | -# - 00:32:00 |
20 | | -_TIME_RE_STR: Final = ( |
21 | | - r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?" |
22 | | -) |
| 17 | +_TIME_RE_STR: Final = r""" |
| 18 | +([01][0-9]|2[0-3]) # hours |
| 19 | +:([0-5][0-9]) # minutes |
| 20 | +(?: |
| 21 | + :([0-5][0-9]) # optional seconds |
| 22 | + (?:\.([0-9]{1,6})[0-9]*)? # optional fractions of a second |
| 23 | +)? |
| 24 | +""" |
23 | 25 |
|
24 | 26 | RE_NUMBER: Final = re.compile( |
25 | 27 | r""" |
|
40 | 42 | """, |
41 | 43 | flags=re.VERBOSE, |
42 | 44 | ) |
43 | | -RE_LOCALTIME: Final = re.compile(_TIME_RE_STR) |
| 45 | +RE_LOCALTIME: Final = re.compile(_TIME_RE_STR, flags=re.VERBOSE) |
44 | 46 | RE_DATETIME: Final = re.compile( |
45 | 47 | rf""" |
46 | 48 | ([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27 |
@@ -76,7 +78,8 @@ def match_to_datetime(match: re.Match[str]) -> datetime | date: |
76 | 78 | year, month, day = int(year_str), int(month_str), int(day_str) |
77 | 79 | if hour_str is None: |
78 | 80 | return date(year, month, day) |
79 | | - hour, minute, sec = int(hour_str), int(minute_str), int(sec_str) |
| 81 | + hour, minute = int(hour_str), int(minute_str) |
| 82 | + sec = int(sec_str) if sec_str else 0 |
80 | 83 | micros = int(micros_str.ljust(6, "0")) if micros_str else 0 |
81 | 84 | if offset_sign_str: |
82 | 85 | tz: tzinfo | None = cached_tz( |
@@ -105,8 +108,9 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone: |
105 | 108 |
|
106 | 109 | def match_to_localtime(match: re.Match[str]) -> time: |
107 | 110 | hour_str, minute_str, sec_str, micros_str = match.groups() |
| 111 | + sec = int(sec_str) if sec_str else 0 |
108 | 112 | micros = int(micros_str.ljust(6, "0")) if micros_str else 0 |
109 | | - return time(int(hour_str), int(minute_str), int(sec_str), micros) |
| 113 | + return time(int(hour_str), int(minute_str), sec, micros) |
110 | 114 |
|
111 | 115 |
|
112 | 116 | def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any: |
|
0 commit comments