markdown: Added error handling for invalid time zones.

If an invalid timezone (such as +32h) was provided, the
timestamp.astimezone call would throw an exception, causing the
message send to fail. Replace that with a user-facing error.

Fixes #19658.
This commit is contained in:
Tomas Fonseca 2024-04-18 16:22:41 +01:00 committed by Tim Abbott
parent 2035305b89
commit 753b8e1f6f
2 changed files with 16 additions and 1 deletions

View File

@ -1417,7 +1417,15 @@ class Timestamp(markdown.inlinepatterns.Pattern):
# Use HTML5 <time> element for valid timestamps.
time_element = Element("time")
if timestamp.tzinfo:
timestamp = timestamp.astimezone(timezone.utc)
try:
timestamp = timestamp.astimezone(timezone.utc)
except ValueError:
error_element = Element("span")
error_element.set("class", "timestamp-error")
error_element.text = markdown.util.AtomicString(
f"Invalid time format: {time_input_string}"
)
return error_element
else:
timestamp = timestamp.replace(tzinfo=timezone.utc)
time_element.set("datetime", timestamp.isoformat().replace("+00:00", "Z"))

View File

@ -838,6 +838,13 @@
"marked_expected_output": "<p><span>**hello world**</span></p>",
"text_content": "Invalid time format: **hello world**"
},
{
"name": "timestamp_invalid_timezone",
"input": "<time:1969-12-31T00:00:00+32:00>",
"expected_output": "<p><span class=\"timestamp-error\">Invalid time format: 1969-12-31T00:00:00+32:00</span></p>",
"marked_expected_output": "<p><span>1969-12-31T00:00:00+32:00</span></p>",
"text_content": "Invalid time format: 1969-12-31T00:00:00+32:00"
},
{
"name": "timestamp_unix",
"input": "Let's meet at <time:1496701800>.",