【发布时间】:2014-08-17 22:08:24
【问题描述】:
我有一个字符串格式
frmt = "%m-%d-%Y %I:%M:%S %p"
当我将 now() 转换为这种格式并返回时,我会丢失 DST。
print datetime.strptime(datetime.now().strftime(frmt), frmt).replace(tzinfo=pytz.timezone("US/Eastern")).dst()
print datetime.now(tz=pytz.timezone("US/Eastern")).dst()
第一次打印返回 1:00:00,第二次打印返回 0:00:00。
使用 datetime.strptime 时有没有办法保留夏令时?
【问题讨论】:
-
我不确定你为什么在这里使用字符串,因为你的例子只是显示“现在”。如果这确实是一个要求,那么请显示以特定字符串开头,我会更新我的答案。
-
@MattJohnson 数据来自 API 作为字符串。我使用
now来解释这个问题。示例字符串为“06-27-2014 07:53:01 PM”。 tz 由配置设置确定。谢谢。
标签: python datetime pytz strptime