我有一个字符串时间来自第三方(我的python程序外部),我需要将这个时间与现在比较。那是多久以前的事了?在
我该怎么做?在
我查看了datetime
和time
库,以及pytz
,找不到一个明显的方法。它应该自动包含DST,因为第三方没有明确说明它的偏移量,只有时区(美国/东部)。在
我试过了,但失败了:
dt = datetime.datetime.strptime('June 10, 2016 12:00PM', '%B %d, %Y %I:%M%p')
dtEt = dt.replace(tzinfo=pytz.timezone('US/Eastern'))
now = datetime.datetime.now()
now - dtEt
TypeError:无法减去偏移量naive和offset aware datetime
问得好扎克!我自己也有这个问题。在
下面是一些代码:
要修复
TypeError
,请使用时区感知的datetime对象:is_dst=None
导致不明确/不存在时间的异常。您也可以使用is_dst=False
(默认)或is_dst=True
,请参见python converting string in localtime to UTC epoch timestamp。在相关问题 更多 >
编程相关推荐