Skip to content

Commit

Permalink
fix: time parser truncate to first day of year/month (#14945)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoyongjie authored Jun 2, 2021
1 parent f4de0dc commit 723a671
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion superset/utils/date_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def parse_human_datetime(human_readable: str) -> datetime:
if re.search(x_periods, human_readable, re.IGNORECASE):
raise TimeRangeUnclearError(human_readable)
try:
dttm = parse(human_readable)
default = datetime(year=datetime.now().year, month=1, day=1)
dttm = parse(human_readable, default=default)
except (ValueError, OverflowError) as ex:
cal = parsedatetime.Calendar()
parsed_dttm, parsed_flags = cal.parseDT(human_readable)
Expand Down
12 changes: 12 additions & 0 deletions tests/utils/date_parser_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
def mock_parse_human_datetime(s):
if s == "now":
return datetime(2016, 11, 7, 9, 30, 10)
elif s == "2018":
return datetime(2018, 1, 1)
elif s == "2018-9":
return datetime(2018, 9, 1)
elif s == "today":
return datetime(2016, 11, 7)
elif s == "yesterday":
Expand Down Expand Up @@ -153,6 +157,14 @@ def test_datetime_eval(self):
expected = datetime(2016, 11, 7)
self.assertEqual(result, expected)

result = datetime_eval("datetime('2018')")
expected = datetime(2018, 1, 1)
self.assertEqual(result, expected)

result = datetime_eval("datetime('2018-9')")
expected = datetime(2018, 9, 1)
self.assertEqual(result, expected)

# Parse compact arguments spelling
result = datetime_eval("dateadd(datetime('today'),1,year,)")
expected = datetime(2017, 11, 7)
Expand Down

0 comments on commit 723a671

Please sign in to comment.