IsPrefixValidDate🔗

Type: Transpilable verification function
Check that the date at the start of text is valid.
The text is expected to start with a date matching the pattern -?YYYY-MM-DD, where the year can have more than four digits. Whatever follows the date, such as the time or the time zone, is ignored.
text🔗
Text to be checked
Return🔗
True if the date at the start of text is valid

Code

month_start = text.find("-", 1) + 1

year = text[: month_start - 1]

# We do not accept year zero,
# see the note at: https://www.w3.org/TR/xmlschema-2/#dateTime
if year.lstrip("-").lstrip("0") == "":
    return False

month = int(text[month_start : month_start + 2])
day = int(text[month_start + 3 : month_start + 5])

return _is_day_of_month_valid(_is_leap_year(year), month, day)