Skip to content

Commit

Permalink
fix: parse datetime followed by a space (python-poetry#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored and capuanob committed Jan 31, 2023
1 parent 8770be8 commit 60c3610
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
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

0 comments on commit 60c3610

Please sign in to comment.