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

fix: parse datetime followed by a space #268

Merged
merged 1 commit into from
Jan 29, 2023
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

- Parse empty table name if it is quoted. ([#258](https://github.com/sdispater/tomlkit/issues/258))
- Fix a bug that remove last element of an Inline Table leaves a comma. ([#259](https://github.com/sdispater/tomlkit/issues/259))
- Parse datetime when it is followed by a space. ([#260](https://github.com/sdispater/tomlkit/issues/260))
- Fix the `unwrap()` method for `Container` children values which sometimes returns an internal object if the table is an out-of-order table. ([#264](https://github.com/sdispater/tomlkit/issues/264))

## [0.11.6] - 2022-10-27
Expand Down
11 changes: 11 additions & 0 deletions tests/test_items.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,17 @@ def test_dates_behave_like_dates():
assert doc.as_string() == "dt = 2018-07-23 # Comment"


def test_parse_datetime_followed_by_space():
# issue #260
doc = parse("dt = 2018-07-22 ")
assert doc["dt"] == date(2018, 7, 22)
assert doc.as_string() == "dt = 2018-07-22 "

doc = parse("dt = 2013-01-24 13:48:01.123456 ")
assert doc["dt"] == datetime(2013, 1, 24, 13, 48, 1, 123456)
assert doc.as_string() == "dt = 2013-01-24 13:48:01.123456 "


def test_times_behave_like_times():
i = item(time(12, 34, 56))

Expand Down
9 changes: 5 additions & 4 deletions tomlkit/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -497,11 +497,12 @@ def _parse_value(self) -> Item:
pass

time_raw = self.extract()
if not time_raw.strip():
trivia.comment_ws = time_raw
time_part = time_raw.rstrip()
trivia.comment_ws = time_raw[len(time_part) :]
if not time_part:
return date

dt = parse_rfc3339(raw + time_raw)
dt = parse_rfc3339(raw + time_part)
assert isinstance(dt, datetime.datetime)
return DateTime(
dt.year,
Expand All @@ -513,7 +514,7 @@ def _parse_value(self) -> Item:
dt.microsecond,
dt.tzinfo,
trivia,
raw + time_raw,
raw + time_part,
)
except ValueError:
raise self.parse_error(InvalidDateError)
Expand Down