Skip to content

Commit 2199c58

Browse files
committed
tomllib: Make seconds optional in Date-Time and Time (TOML 1.1)
1 parent 880092f commit 2199c58

File tree

5 files changed

+20
-9
lines changed

5 files changed

+20
-9
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"local-dt": {"type":"datetime-local","value":"1988-10-27t01:01:01"},
3+
"local-dt-no-seconds": {"type":"datetime-local","value":"2025-04-18t20:05:00"},
34
"zulu-dt": {"type":"datetime","value":"1988-10-27t01:01:01z"}
45
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
local-dt=1988-10-27t01:01:01
2+
local-dt-no-seconds=2025-04-18T20:05
23
zulu-dt=1988-10-27t01:01:01z
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
{"t":
2-
{"type":"time-local","value":"00:00:00.999999"}}
2+
{"type":"time-local","value":"00:00:00.999999"},
3+
"t2":
4+
{"type":"time-local","value":"00:00:00"}}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
t=00:00:00.99999999999999
1+
t=00:00:00.99999999999999
2+
t2=00:00

Lib/tomllib/_re.py

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

1515
from ._types import ParseFloat
1616

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]*)?"
17+
_TIME_RE_STR = 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+
"""
2125

2226
RE_NUMBER = re.compile(
2327
r"""
@@ -38,7 +42,7 @@
3842
""",
3943
flags=re.VERBOSE,
4044
)
41-
RE_LOCALTIME = re.compile(_TIME_RE_STR)
45+
RE_LOCALTIME = re.compile(_TIME_RE_STR, flags=re.VERBOSE)
4246
RE_DATETIME = re.compile(
4347
rf"""
4448
([0-9]{{4}})-(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01]) # date, e.g. 1988-10-27
@@ -74,7 +78,8 @@ def match_to_datetime(match: re.Match[str]) -> datetime | date:
7478
year, month, day = int(year_str), int(month_str), int(day_str)
7579
if hour_str is None:
7680
return date(year, month, day)
77-
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
7883
micros = int(micros_str.ljust(6, "0")) if micros_str else 0
7984
if offset_sign_str:
8085
tz: tzinfo | None = cached_tz(
@@ -103,8 +108,9 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:
103108

104109
def match_to_localtime(match: re.Match[str]) -> time:
105110
hour_str, minute_str, sec_str, micros_str = match.groups()
111+
sec = int(sec_str) if sec_str else 0
106112
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)
113+
return time(int(hour_str), int(minute_str), sec, micros)
108114

109115

110116
def match_to_number(match: re.Match[str], parse_float: ParseFloat) -> Any:

0 commit comments

Comments
 (0)