validate_iso8601_string#

validate_iso8601_string(iso_string, has_ms=None, has_tz=None)#

Test if string is ISO 8601 timestring. Also verify if string includes milliseconds and timezone https://en.wikipedia.org/wiki/ISO_8601

Parameters:
iso_string: str

A formated timestring

has_ms: bool

Test if string includes milliseconds

has_tz: bool

Test if string includes timezone

Returns:
valid: Bool or str

Returns True if timestring is of shape %Y-%m-%dT%H:%M:%S.%ms. Else False

Examples

>>> validate_iso8601_string('2021-01-09T17:47:56.154564', has_ms=True, has_tz=False) # ISO8601 w/o timezone and with ms
True
>>> validate_iso8601_string('2021-01-09T17:47:56.154564', has_ms=False) # ISO8601 w/o timezone and with ms
False
>>> validate_iso8601_string('2021-01-09T17:47:56', has_ms=True) # No ms
False
>>> validate_iso8601_string('2021-01-09T17:47:56', has_ms=False) # No ms
True
>>> validate_iso8601_string('2021-01-09T17:47:56.2435+05:00', has_tz=True) # includes timezone
True
>>> validate_iso8601_string('2021-01-09T17:47:56.2435+05:00', has_tz=False) # includes timezone
False
>>> validate_iso8601_string('2021-01-09T17:47:56.154564 (45 12) (1)') # STARE timestring
False
>>> validate_iso8601_string('Wolfgang') # Not a timestamp
False