diff --git a/picard/script/functions.py b/picard/script/functions.py index 9a0eae1f3..874f07a26 100644 --- a/picard/script/functions.py +++ b/picard/script/functions.py @@ -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 '' diff --git a/test/test_script.py b/test/test_script.py index a35743b52..f7b167d5e 100644 --- a/test/test_script.py +++ b/test/test_script.py @@ -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, "