是否有一种简单的方法可以将ISO 8601持续时间转换为timedelta?
问题描述:
给出ISO 8601持续时间字符串,如何将其转换为 datetime.timedelta
?
Given a ISO 8601 duration string, how do I convert it into a datetime.timedelta
?
这不起作用:
timedelta("PT1H5M26S", "T%H%M%S")
timedelta对象表示持续时间,即两个日期或时间之间的时差( https://docs.python.org/3/library/datetime.html#datetime.timedelta )。 ISO 8601持续时间在此处进行了描述: https://en.wikipedia.org/wiki/ISO_8601#Durations
A timedelta object represents a duration, the difference between two dates or times (https://docs.python.org/3/library/datetime.html#datetime.timedelta). ISO 8601 durations are described here: https://en.wikipedia.org/wiki/ISO_8601#Durations
答
我发现 isodate 库完全可以执行我想要的操作
I found isodate library to do exactly what I want
isodate.parse_duration('PT1H5M26S')