Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make seconds optional in Date-Time and Time #203

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/tomli/_re.py
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