Python: 来自 helm 的带时区的时间格式

Python: timeformat with timezone from helm

我正在尝试解析从 helm helm list 命令生成的时间戳。 现在时间戳格式是这样的: 2021-08-24 13:19:18.683572 +0800 +0800 如何使用 python?

解析它

提前致谢

使用字符串修剪时:

    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