gfw.common.datetime.datetime_from_string#

datetime_from_string(s, date_format='%Y-%m-%d', time_format='%H_%M_%SZ', allow_no_time=True, tz=datetime.timezone.utc)[source]#

Extracts a zone-aware datetime from a string.

Parameters:
  • s (str) – The string containing the datetime to extract.

  • date_format (str) – The strftime/strptime format of the date part. Defaults to %Y-%m-%d.

  • time_format (str) – The strftime/strptime format of the time part. Defaults to %H_%M_%SZ.

  • allow_no_time (bool) – If True, allows input strings with no time information.

  • tz (timezone) – The timezone to apply if the parsed datetime has no tzinfo. Defaults to UTC.

Raises:

ValueError

  • When date is not found in the input string. - When time is not found in the input string and allow_no_time is False.

Returns:

A timezone-aware datetime object.

Return type:

datetime