Skip to content

Commit 2b5aafb

Browse files
authored
fix: parse datetime followed by a space (#268)
1 parent 588ecce commit 2b5aafb

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
- Parse empty table name if it is quoted. ([#258](https://github.com/sdispater/tomlkit/issues/258))
99
- Fix a bug that remove last element of an Inline Table leaves a comma. ([#259](https://github.com/sdispater/tomlkit/issues/259))
10+
- Parse datetime when it is followed by a space. ([#260](https://github.com/sdispater/tomlkit/issues/260))
1011
- 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))
1112

1213
## [0.11.6] - 2022-10-27

tests/test_items.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,17 @@ def test_dates_behave_like_dates():
677677
assert doc.as_string() == "dt = 2018-07-23 # Comment"
678678

679679

680+
def test_parse_datetime_followed_by_space():
681+
# issue #260
682+
doc = parse("dt = 2018-07-22 ")
683+
assert doc["dt"] == date(2018, 7, 22)
684+
assert doc.as_string() == "dt = 2018-07-22 "
685+
686+
doc = parse("dt = 2013-01-24 13:48:01.123456 ")
687+
assert doc["dt"] == datetime(2013, 1, 24, 13, 48, 1, 123456)
688+
assert doc.as_string() == "dt = 2013-01-24 13:48:01.123456 "
689+
690+
680691
def test_times_behave_like_times():
681692
i = item(time(12, 34, 56))
682693

tomlkit/parser.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,12 @@ def _parse_value(self) -> Item:
497497
pass
498498

499499
time_raw = self.extract()
500-
if not time_raw.strip():
501-
trivia.comment_ws = time_raw
500+
time_part = time_raw.rstrip()
501+
trivia.comment_ws = time_raw[len(time_part) :]
502+
if not time_part:
502503
return date
503504

504-
dt = parse_rfc3339(raw + time_raw)
505+
dt = parse_rfc3339(raw + time_part)
505506
assert isinstance(dt, datetime.datetime)
506507
return DateTime(
507508
dt.year,
@@ -513,7 +514,7 @@ def _parse_value(self) -> Item:
513514
dt.microsecond,
514515
dt.tzinfo,
515516
trivia,
516-
raw + time_raw,
517+
raw + time_part,
517518
)
518519
except ValueError:
519520
raise self.parse_error(InvalidDateError)

0 commit comments

Comments
 (0)