从电子邮件解析带时区的日期? [英] Parsing date with timezone from an email?
问题描述
我正在尝试从电子邮件中检索日期.一开始很简单:
message = email.parser.Parser().parse(file)日期 = 消息['日期']打印日期
我收到:
'Mon, 16 Nov 2009 13:32:02 +0100'
但我需要一个不错的日期时间对象,所以我使用:
datetime.strptime('Mon, 16 Nov 2009 13:32:02 +0100', '%a, %d %b %Y %H:%M:%S %Z')
这会引发 ValueError,因为 %Z 不是 +0100
的格式.但是我在文档中找不到正确的时区格式,只有这个 %Z
用于区域.有人可以帮我吗?
email.utils
有一个 parsedate()
函数用于 RFC 2822 格式,就我知道没有被弃用.
但是请注意,parsedate
方法不考虑时区,并且 time.mktime
总是需要一个本地时间元组,如这里.
因此您仍然需要解析时区并考虑本地时差:
<预><代码>>>>REMOTE_TIME_ZONE_OFFSET = +9 * 60 * 60>>>(time.mktime(email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0900')) +... time.timezone - REMOTE_TIME_ZONE_OFFSET)1258410122.0I am trying to retrieve date from an email. At first it's easy:
message = email.parser.Parser().parse(file)
date = message['Date']
print date
and I receive:
'Mon, 16 Nov 2009 13:32:02 +0100'
But I need a nice datetime object, so I use:
datetime.strptime('Mon, 16 Nov 2009 13:32:02 +0100', '%a, %d %b %Y %H:%M:%S %Z')
which raises ValueError, since %Z isn't format for +0100
. But I can't find proper format for timezone in the documentation, there is only this %Z
for zone. Can someone help me on that?
email.utils
has a parsedate()
function for the RFC 2822 format, which as far as I know is not deprecated.
>>> import email.utils
>>> import time
>>> import datetime
>>> email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0100')
(2009, 11, 16, 13, 32, 2, 0, 1, -1)
>>> time.mktime((2009, 11, 16, 13, 32, 2, 0, 1, -1))
1258378322.0
>>> datetime.datetime.fromtimestamp(1258378322.0)
datetime.datetime(2009, 11, 16, 13, 32, 2)
Please note, however, that the parsedate
method does not take into account the time zone and time.mktime
always expects a local time tuple as mentioned here.
>>> (time.mktime(email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0900')) ==
... time.mktime(email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0100'))
True
So you'll still need to parse out the time zone and take into account the local time difference, too:
>>> REMOTE_TIME_ZONE_OFFSET = +9 * 60 * 60
>>> (time.mktime(email.utils.parsedate('Mon, 16 Nov 2009 13:32:02 +0900')) +
... time.timezone - REMOTE_TIME_ZONE_OFFSET)
1258410122.0
这篇关于从电子邮件解析带时区的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!