1111
1212from ._types import ParseFloat
1313
14- # E.g.
15- # - 00:32:00.999999
16- # - 00:32:00
17- _TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
14+ _TIME_RE_STR = r"""
15+ ([01][0-9]|2[0-3]) # hours
16+ :([0-5][0-9]) # minutes
17+ (?:
18+ :([0-5][0-9]) # optional seconds
19+ (?:\.([0-9]{1,6})[0-9]*)? # optional fractions of a second
20+ )?
21+ """
1822
1923RE_NUMBER = re .compile (
2024 r"""
3539""" ,
3640 flags = re .VERBOSE ,
3741)
38- RE_LOCALTIME = re .compile (_TIME_RE_STR )
42+ RE_LOCALTIME = re .compile (_TIME_RE_STR , flags = re . VERBOSE )
3943RE_DATETIME = re .compile (
4044 rf"""
4145([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
@@ -71,7 +75,8 @@ def match_to_datetime(match: re.Match) -> datetime | date:
7175 year , month , day = int (year_str ), int (month_str ), int (day_str )
7276 if hour_str is None :
7377 return date (year , month , day )
74- hour , minute , sec = int (hour_str ), int (minute_str ), int (sec_str )
78+ hour , minute = int (hour_str ), int (minute_str )
79+ sec = int (sec_str ) if sec_str else 0
7580 micros = int (micros_str .ljust (6 , "0" )) if micros_str else 0
7681 if offset_sign_str :
7782 tz : tzinfo | None = cached_tz (
@@ -97,8 +102,9 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
97102
98103def match_to_localtime (match : re .Match ) -> time :
99104 hour_str , minute_str , sec_str , micros_str = match .groups ()
105+ sec = int (sec_str ) if sec_str else 0
100106 micros = int (micros_str .ljust (6 , "0" )) if micros_str else 0
101- return time (int (hour_str ), int (minute_str ), int ( sec_str ) , micros )
107+ return time (int (hour_str ), int (minute_str ), sec , micros )
102108
103109
104110def match_to_number (match : re .Match , parse_float : ParseFloat ) -> Any :
0 commit comments