Skip to content

Commit 1cde1da

Browse files
committed
Make seconds optional in Date-Time and Time
1 parent a613867 commit 1cde1da

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/tomli/_re.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@
1111

1212
from ._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

1923
RE_NUMBER = re.compile(
2024
r"""
@@ -35,7 +39,7 @@
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)
3943
RE_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

98103
def 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

104110
def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any:

0 commit comments

Comments
 (0)