<tfoot id='gyeL1'></tfoot>

      <i id='gyeL1'><tr id='gyeL1'><dt id='gyeL1'><q id='gyeL1'><span id='gyeL1'><b id='gyeL1'><form id='gyeL1'><ins id='gyeL1'></ins><ul id='gyeL1'></ul><sub id='gyeL1'></sub></form><legend id='gyeL1'></legend><bdo id='gyeL1'><pre id='gyeL1'><center id='gyeL1'></center></pre></bdo></b><th id='gyeL1'></th></span></q></dt></tr></i><div id='gyeL1'><tfoot id='gyeL1'></tfoot><dl id='gyeL1'><fieldset id='gyeL1'></fieldset></dl></div>
        <bdo id='gyeL1'></bdo><ul id='gyeL1'></ul>
    1. <legend id='gyeL1'><style id='gyeL1'><dir id='gyeL1'><q id='gyeL1'></q></dir></style></legend>

        <small id='gyeL1'></small><noframes id='gyeL1'>

      1. 从电子邮件中解析带有时区的日期?

        Parsing date with timezone from an email?(从电子邮件中解析带有时区的日期?)
            <tbody id='dBF2n'></tbody>
          • <tfoot id='dBF2n'></tfoot>

            <legend id='dBF2n'><style id='dBF2n'><dir id='dBF2n'><q id='dBF2n'></q></dir></style></legend>

            1. <small id='dBF2n'></small><noframes id='dBF2n'>

            2. <i id='dBF2n'><tr id='dBF2n'><dt id='dBF2n'><q id='dBF2n'><span id='dBF2n'><b id='dBF2n'><form id='dBF2n'><ins id='dBF2n'></ins><ul id='dBF2n'></ul><sub id='dBF2n'></sub></form><legend id='dBF2n'></legend><bdo id='dBF2n'><pre id='dBF2n'><center id='dBF2n'></center></pre></bdo></b><th id='dBF2n'></th></span></q></dt></tr></i><div id='dBF2n'><tfoot id='dBF2n'></tfoot><dl id='dBF2n'><fieldset id='dBF2n'></fieldset></dl></div>
                • <bdo id='dBF2n'></bdo><ul id='dBF2n'></ul>
                  本文介绍了从电子邮件中解析带有时区的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试从电子邮件中检索日期.一开始很简单:

                  I am trying to retrieve date from an email. At first it's easy:

                  message = email.parser.Parser().parse(file)
                  date = message['Date']
                  print date
                  

                  我收到:

                  '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')
                  

                  这会引发 ValueError,因为 %Z 不是 +0100 的格式.但是我在文档中找不到正确的时区格式,只有这个 %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 有一个针对 RFC 2822 格式的 parsedate() 函数,它就我知道没有被弃用.

                  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)
                  

                  但是请注意,parsedate 方法不考虑时区,并且 time.mktime 总是需要一个本地时间元组,如 这里.

                  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
                  

                  这篇关于从电子邮件中解析带有时区的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)
                  <tfoot id='3iHMM'></tfoot>
                    <i id='3iHMM'><tr id='3iHMM'><dt id='3iHMM'><q id='3iHMM'><span id='3iHMM'><b id='3iHMM'><form id='3iHMM'><ins id='3iHMM'></ins><ul id='3iHMM'></ul><sub id='3iHMM'></sub></form><legend id='3iHMM'></legend><bdo id='3iHMM'><pre id='3iHMM'><center id='3iHMM'></center></pre></bdo></b><th id='3iHMM'></th></span></q></dt></tr></i><div id='3iHMM'><tfoot id='3iHMM'></tfoot><dl id='3iHMM'><fieldset id='3iHMM'></fieldset></dl></div>
                      <tbody id='3iHMM'></tbody>
                      <bdo id='3iHMM'></bdo><ul id='3iHMM'></ul>

                            <small id='3iHMM'></small><noframes id='3iHMM'>

                            <legend id='3iHMM'><style id='3iHMM'><dir id='3iHMM'><q id='3iHMM'></q></dir></style></legend>