【问题标题】:How to format +xxxx for strptime() [duplicate]如何为strptime()格式化+ xxxx [重复]
【发布时间】:2012-08-17 16:32:43
【问题描述】:

可能重复:
Python strptime() and timezones?

我有类似 [22/Aug/2012:13:08:27 +0400] 的字符串,以及如何使用 strptime 函数将其转换为时间元组?

time.strptime(t, '[%d/%b/%Y:%H:%M:%S +0400]')

有效,但显然我也需要格式化时间偏移。

当我尝试使用 %Z 格式化 +0400 时,我有:

ValueError: time data '[22/Aug/2012:13:08:27 +0400]' does not match format '[%d/%b/%Y:%H:%M:%S %Z]'

当我尝试 %z

ValueError: 'z' is a bad directive in format '[%d/%b/%Y:%H:%M:%S %z]'

【问题讨论】:

    标签: python time formatting


    【解决方案1】:

    确实,time.strptime() 不支持 %z 格式化程序。

    dateutil module 包含一个支持解析时区的解析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-25
      • 2020-01-06
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 2012-12-13
      • 2019-08-20
      • 2017-04-10
      相关资源
      最近更新 更多