How to parse str(my_datetime) using strptime?

后端 未结 2 679
小蘑菇
小蘑菇 2020-12-20 21:59

This is the default string representation of a datetime:

>>> from datetime import datetime, timezone
>>> dt = datetime(2017, 1, 1, tzinfo=ti         


        
2条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-20 22:44

    Note that str(d) is documented as being equivalent to d.isoformat(' '). This starts with %Y-%m-%d %H:%M:%S (2017-01-01 00:00:00), but then:

    • Either has nothing or .%f for microseconds; and
    • Either has nothing or an offset with a colon, which doesn't match %z.

    .strptime doesn't have support for optional parts, therefore there isn't a single format parameter that can match all of these possible outputs, and doesn't support the colon in the offset, so some can't be handled at all.

提交回复
热议问题