Skip to content

Commit

Permalink
Make seconds optional in Date-Time and Time
Browse files Browse the repository at this point in the history
  • Loading branch information
hukkin committed Sep 25, 2024
1 parent a613867 commit 1cde1da
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/tomli/_re.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand All @@ -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
Expand Down Expand Up @@ -71,7 +75,8 @@ def match_to_datetime(match: re.Match) -> datetime | date:
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(
Expand All @@ -97,8 +102,9 @@ def cached_tz(hour_str: str, minute_str: str, sign_str: str) -> timezone:

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:
Expand Down

0 comments on commit 1cde1da

Please sign in to comment.