Python strptime() 和时区? [英] Python strptime() and timezones?
问题描述
我有一个来自 Blackberry IPD 备份的 CSV 转储文件,使用 IPDDump 创建.这里的日期/时间字符串看起来像这样(其中 EST
是澳大利亚时区):
美国东部时间 2010 年 6 月 22 日星期二 07:46:22
我需要能够用 Python 解析这个日期.起初,我尝试使用 datettime 中的 strptime()
函数.
但是,由于某种原因,返回的 datetime
对象似乎没有任何 tzinfo
与之关联.
我确实在此页面上阅读了显然datetime.strptime
默默地丢弃了 tzinfo
,但是,我查看了文档,但找不到任何与此相关的内容 此处.
我已经能够使用第三方 Python 库 dateutil 解析日期,但是我仍然很好奇我是如何错误地使用内置 strptime()
的?有什么办法可以让 strptime()
与时区配合得很好?
返回一个date_string对应的日期时间,按照格式解析.这相当于 datetime(*(time.strptime(date_string, format)[0:6]))
.
看到[0:6]
了吗?这让你(年、月、日、时、分、秒)
.没有其他的.没有提到时区.
有趣的是,[Win XP SP2, Python 2.6, 2.7] 将您的示例传递给 time.strptime
不起作用,但如果您去掉%Z"和EST",它会起作用工作.也使用UTC"或GMT"代替EST"工作.PST"和MEZ"不起作用.令人费解.
值得注意的是,这已从 3.2 版开始更新,并且相同的文档现在还声明了以下内容:
<块引用>当 %z 指令提供给 strptime() 方法时,将生成一个感知日期时间对象.结果的 tzinfo 将设置为时区实例.
请注意,这不适用于 %Z,因此大小写很重要.请参见以下示例:
In [1]: from datetime import datetime在 [2] 中:start_time = datetime.strptime('2018-04-18-17-04-30-AEST','%Y-%m-%d-%H-%M-%S-%Z')在 [3] 中:print("TZ NAME: {tz}".format(tz=start_time.tzname()))TZ 名称:无在 [4] 中:start_time = datetime.strptime('2018-04-18-17-04-30-+1000','%Y-%m-%d-%H-%M-%S-%z')在 [5] 中:print("TZ NAME: {tz}".format(tz=start_time.tzname()))TZ 名称:UTC+10:00
I have a CSV dumpfile from a Blackberry IPD backup, created using IPDDump.
The date/time strings in here look something like this
(where EST
is an Australian time-zone):
Tue Jun 22 07:46:22 EST 2010
I need to be able to parse this date in Python. At first, I tried to use the strptime()
function from datettime.
>>> datetime.datetime.strptime('Tue Jun 22 12:10:20 2010 EST', '%a %b %d %H:%M:%S %Y %Z')
However, for some reason, the datetime
object that comes back doesn't seem to have any tzinfo
associated with it.
I did read on this page that apparently datetime.strptime
silently discards tzinfo
, however, I checked the documentation, and I can't find anything to that effect documented here.
I have been able to get the date parsed using a third-party Python library, dateutil, however I'm still curious as to how I was using the in-built strptime()
incorrectly? Is there any way to get strptime()
to play nicely with timezones?
The datetime
module documentation says:
Return a datetime corresponding to date_string, parsed according to format. This is equivalent to
datetime(*(time.strptime(date_string, format)[0:6]))
.
See that [0:6]
? That gets you (year, month, day, hour, minute, second)
. Nothing else. No mention of timezones.
Interestingly, [Win XP SP2, Python 2.6, 2.7] passing your example to time.strptime
doesn't work but if you strip off the " %Z" and the " EST" it does work. Also using "UTC" or "GMT" instead of "EST" works. "PST" and "MEZ" don't work. Puzzling.
It's worth noting this has been updated as of version 3.2 and the same documentation now also states the following:
When the %z directive is provided to the strptime() method, an aware datetime object will be produced. The tzinfo of the result will be set to a timezone instance.
Note that this doesn't work with %Z, so the case is important. See the following example:
In [1]: from datetime import datetime
In [2]: start_time = datetime.strptime('2018-04-18-17-04-30-AEST','%Y-%m-%d-%H-%M-%S-%Z')
In [3]: print("TZ NAME: {tz}".format(tz=start_time.tzname()))
TZ NAME: None
In [4]: start_time = datetime.strptime('2018-04-18-17-04-30-+1000','%Y-%m-%d-%H-%M-%S-%z')
In [5]: print("TZ NAME: {tz}".format(tz=start_time.tzname()))
TZ NAME: UTC+10:00
这篇关于Python strptime() 和时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!