Allow entering variables as arguments to $dateformat()

This commit is contained in:
Bob Swift
2021-07-23 10:03:30 -06:00
parent 816fcd8022
commit 4956789de4
2 changed files with 10 additions and 24 deletions

View File

@@ -1446,7 +1446,8 @@ def func_day(parser, date_to_parse, date_order='ymd'):
Returns the input date in the specified `format`, which is based on the standard
Python `strftime` [format codes](https://strftime.org/). If no `format` is
specified the date will be returned in the form `2020-02-05`.
specified the date will be returned in the form `2020-02-05`. If the date or
format are invalid an empty string will be returned.
The default order for the input date is "ymd". This can be changed by specifying
either "dmy" or "mdy".
@@ -1464,10 +1465,8 @@ def func_dateformat(parser, date_to_parse, date_format=None, date_order='ymd'):
try:
date_object = datetime.date(int(yr), int(mo), int(da))
except ValueError:
stackitem = parser._function_stack.get()
raise ScriptRuntimeError(stackitem, "Invalid date")
return ''
try:
return date_object.strftime(date_format)
except ValueError:
stackitem = parser._function_stack.get()
raise ScriptRuntimeError(stackitem, "Unsupported format code")
return ''

View File

@@ -1692,7 +1692,6 @@ class ScriptParserTest(PicardTestCase):
# Test missing elements
self.assertScriptResultEquals("$month(,)", "", context)
self.assertScriptResultEquals("$month(-21-2021,mdy)", "", context)
# self.assertScriptResultEquals("$month(21--2021,dmy)", "", context)
# Tests with invalid number of arguments
areg = r"^\d+:\d+:\$month: Wrong number of arguments for \$month: Expected between 1 and 2, "
@@ -1721,7 +1720,6 @@ class ScriptParserTest(PicardTestCase):
# Test missing elements
self.assertScriptResultEquals("$day(,)", "", context)
self.assertScriptResultEquals("$day(-07-2021,dmy)", "", context)
# self.assertScriptResultEquals("$day(07--2021,mdy)", "", context)
# Tests with invalid number of arguments
areg = r"^\d+:\d+:\$day: Wrong number of arguments for \$day: Expected between 1 and 2, "
@@ -1747,25 +1745,14 @@ class ScriptParserTest(PicardTestCase):
# Test with invalid overrides
self.assertScriptResultEquals("$dateformat(2021-07-21,,myd)", "2021-07-21", context)
areg = r"^\d+:\d+:\$dateformat: Invalid date"
# with self.assertRaisesRegex(ScriptError, areg):
# self.parser.eval("$dateformat(2021-07-21,,,.)")
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(2021-07-21,,dmy)")
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(2021-07-21,,mdy)")
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(2021-July-21)")
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(2021)")
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(2021-07)")
# with self.assertRaisesRegex(ScriptError, areg):
# self.parser.eval("$dateformat(2021--21)")
self.assertScriptResultEquals("$dateformat(2021-07-21,,dmy)", "", context)
self.assertScriptResultEquals("$dateformat(2021-07-21,,mdy)", "", context)
self.assertScriptResultEquals("$dateformat(2021-July-21)", "", context)
self.assertScriptResultEquals("$dateformat(2021)", "", context)
self.assertScriptResultEquals("$dateformat(2021-07)", "", context)
# Test missing elements
with self.assertRaisesRegex(ScriptError, areg):
self.parser.eval("$dateformat(,)")
self.assertScriptResultEquals("$dateformat(,)", "", context)
# Tests with invalid number of arguments
areg = r"^\d+:\d+:\$dateformat: Wrong number of arguments for \$dateformat: Expected between 1 and 3, "