MatchesDateTimeLiteral🔗

Type: Pattern verification
Check that text is a valid date-time literal of the query language.
A date-time literal is a valid calendar date, followed by an optional T separator, the time and an optional time zone, e.g. , 2024-02-29T12:00:00Z.
text🔗
Text to be checked
Return🔗
True if the text conforms to the pattern

Code

digit = "[0-9]"
month_with_31_days = f"(0[13578]|1[02])-(0[1-9]|[12]{digit}|3[01])"
month_with_30_days = f"(0[469]|11)-(0[1-9]|[12]{digit}|30)"
february = f"02-(0[1-9]|1{digit}|2[0-8])"
common_date = f"{digit}{{4}}-({month_with_31_days}|{month_with_30_days}|{february})"
leap_year = (
    f"({digit}{{2}}(0[48]|[2468][048]|[13579][26])"
    f"|([02468][048]|[13579][26])00)"
)
leap_day = f"{leap_year}-02-29"
time = (
    f"(([01]{digit}|2[0-3]):[0-5]{digit}(:[0-5]{digit})?(\\.{digit}+)?"
    f"|24:00(:00)?(\\.0+)?)"
)
timezone = f"(Z|[+-]((0{digit}|1[0-3]):[0-5]{digit}|14:00))"

pattern = f"^(({common_date})|({leap_day}))T?{time}{timezone}?$"

return match(pattern, text) is not None