Make seconds optional in Date-Time and Time
diff --git a/src/tomli/_re.py b/src/tomli/_re.py
index 994bb74..5a264ff 100644
--- a/src/tomli/_re.py
+++ b/src/tomli/_re.py
@@ -11,10 +11,14 @@
from ._types import ParseFloat
-# E.g.
-# - 00:32:00.999999
-# - 00:32:00
-_TIME_RE_STR = r"([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])(?:\.([0-9]{1,6})[0-9]*)?"
+_TIME_RE_STR = r"""
+([01][0-9]|2[0-3]) # hours
+:([0-5][0-9]) # minutes
+(?:
+ :([0-5][0-9]) # optional seconds
+ (?:\.([0-9]{1,6})[0-9]*)? # optional fractions of a second
+)?
+"""
RE_NUMBER = re.compile(
r"""
@@ -35,7 +39,7 @@
""",
flags=re.VERBOSE,
)
-RE_LOCALTIME = re.compile(_TIME_RE_STR)
+RE_LOCALTIME = re.compile(_TIME_RE_STR, flags=re.VERBOSE)
RE_DATETIME = re.compile(
rf"""
([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 @@
year, month, day = int(year_str), int(month_str), int(day_str)
if hour_str is None:
return date(year, month, day)
- hour, minute, sec = int(hour_str), int(minute_str), int(sec_str)
+ hour, minute = int(hour_str), int(minute_str)
+ sec = int(sec_str) if sec_str else 0
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
if offset_sign_str:
tz: tzinfo | None = cached_tz(
@@ -97,8 +102,9 @@
def match_to_localtime(match: re.Match) -> time:
hour_str, minute_str, sec_str, micros_str = match.groups()
+ sec = int(sec_str) if sec_str else 0
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
- return time(int(hour_str), int(minute_str), int(sec_str), micros)
+ return time(int(hour_str), int(minute_str), sec, micros)
def match_to_number(match: re.Match, parse_float: ParseFloat) -> Any: