Skip to main content

parse_datetime

@lru_cache(maxsize=8192)
def parse_datetime(time_string: str) -> datetime
Parsing datetime strings to timezone-aware datetime objects efficiently. If no timezone information is present in the string, UTC is assumed. Our labels can contain timestamps in different formats, but applying dateutil.parse straight away is expensive as it is very smart and tries to guess the time format. So instead we’re applying parsers with known formats, starting from the formats most likely to occur, and falling back to the most complicated logic only in case of all other attempts have failed. Arguments:
  • time_string - A string containing a datetime in various possible formats.
Returns: A timezone-aware datetime object (UTC if no timezone was specified).

format_datetime_to_long_string

def format_datetime_to_long_string(_datetime: datetime) -> str
Format a datetime object to a string in the DATETIME_LONG_STRING_FORMAT format.