Python: timeformat with timezone from helm

I’m trying to parse a timestamp that was generated from helm helm list command. Now the timestamp format is something like this: 2021-08-24 13:19:18.683572 +0800 +0800 How can I parse it using python?

Thanks in advance

Answer

When with string trimming:

    def _convert_time_to_iso_format(date_helm_format: str) -> datetime:
        """The helm time output format: 2021-08-24 13:19:18.683572 +0300 +0300
        Need to convert to iso format: 2021-08-24 13:19:18.683

        Args:
            date_helm_format (str): 2021-08-24 13:19:18.683572 +0300 +0300
        """
        (date, hour, tz, _) = date_helm_format.split(" ")
        hour = hour[0:-3] # remove nano-seconds, Python don't know how to handle it
        updated_date = date + " " + hour + " " + tz
        updated_date = datetime.strptime(updated_date, "%Y-%m-%d %H:%M:%S.%f %z")
        updated_date = updated_date - updated_date.utcoffset()
        return updated_date