mirror of https://github.com/apache/superset.git
fix: time parser truncate to first day of year/month (#14945)
This commit is contained in:
parent
f4de0dcb65
commit
723a67156c
|
@ -79,7 +79,8 @@ def parse_human_datetime(human_readable: str) -> datetime:
|
||||||
if re.search(x_periods, human_readable, re.IGNORECASE):
|
if re.search(x_periods, human_readable, re.IGNORECASE):
|
||||||
raise TimeRangeUnclearError(human_readable)
|
raise TimeRangeUnclearError(human_readable)
|
||||||
try:
|
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:
|
except (ValueError, OverflowError) as ex:
|
||||||
cal = parsedatetime.Calendar()
|
cal = parsedatetime.Calendar()
|
||||||
parsed_dttm, parsed_flags = cal.parseDT(human_readable)
|
parsed_dttm, parsed_flags = cal.parseDT(human_readable)
|
||||||
|
|
|
@ -35,6 +35,10 @@ from tests.base_tests import SupersetTestCase
|
||||||
def mock_parse_human_datetime(s):
|
def mock_parse_human_datetime(s):
|
||||||
if s == "now":
|
if s == "now":
|
||||||
return datetime(2016, 11, 7, 9, 30, 10)
|
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":
|
elif s == "today":
|
||||||
return datetime(2016, 11, 7)
|
return datetime(2016, 11, 7)
|
||||||
elif s == "yesterday":
|
elif s == "yesterday":
|
||||||
|
@ -153,6 +157,14 @@ class TestDateParser(SupersetTestCase):
|
||||||
expected = datetime(2016, 11, 7)
|
expected = datetime(2016, 11, 7)
|
||||||
self.assertEqual(result, expected)
|
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
|
# Parse compact arguments spelling
|
||||||
result = datetime_eval("dateadd(datetime('today'),1,year,)")
|
result = datetime_eval("dateadd(datetime('today'),1,year,)")
|
||||||
expected = datetime(2017, 11, 7)
|
expected = datetime(2017, 11, 7)
|
||||||
|
|
Loading…
Reference in New Issue