• <small id='0P7yU'></small><noframes id='0P7yU'>

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

        Python 时间老化,第 2 部分:时区

        时间:2023-07-03
          <bdo id='4uyrX'></bdo><ul id='4uyrX'></ul>
          <i id='4uyrX'><tr id='4uyrX'><dt id='4uyrX'><q id='4uyrX'><span id='4uyrX'><b id='4uyrX'><form id='4uyrX'><ins id='4uyrX'></ins><ul id='4uyrX'></ul><sub id='4uyrX'></sub></form><legend id='4uyrX'></legend><bdo id='4uyrX'><pre id='4uyrX'><center id='4uyrX'></center></pre></bdo></b><th id='4uyrX'></th></span></q></dt></tr></i><div id='4uyrX'><tfoot id='4uyrX'></tfoot><dl id='4uyrX'><fieldset id='4uyrX'></fieldset></dl></div>

              <tbody id='4uyrX'></tbody>

              <legend id='4uyrX'><style id='4uyrX'><dir id='4uyrX'><q id='4uyrX'></q></dir></style></legend>

              1. <small id='4uyrX'></small><noframes id='4uyrX'>

                <tfoot id='4uyrX'></tfoot>
                • 本文介绍了Python 时间老化,第 2 部分:时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  继我之前的问题之后,Python 老化时间,我现在遇到了一个关于时区的问题,事实证明它并不总是+0200".所以当 strptime 试图解析它时,它会抛出一个异常.

                  Following on from my previous question, Python time to age, I have now come across a problem regarding the timezone, and it turns out that it's not always going to be "+0200". So when strptime tries to parse it as such, it throws up an exception.

                  我曾想过用 [:-6] 或其他什么来切断 +0200,但有没有真正的方法可以用 strptime 做到这一点?

                  I thought about just chopping off the +0200 with [:-6] or whatever, but is there a real way to do this with strptime?

                  如果重要的话,我正在使用 Python 2.5.2.

                  I am using Python 2.5.2 if it matters.

                  >>> from datetime import datetime
                  >>> fmt = "%a, %d %b %Y %H:%M:%S +0200"
                  >>> datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0200", fmt)
                  datetime.datetime(2008, 7, 22, 8, 17, 41)
                  >>> datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0300", fmt)
                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python2.5/_strptime.py", line 330, in strptime
                      (data_string, format))
                  ValueError: time data did not match format:  data=Tue, 22 Jul 2008 08:17:41 +0300  fmt=%a, %d %b %Y %H:%M:%S +0200
                  

                  推荐答案

                  2.6 版中的新功能.

                  New in version 2.6.

                  对于一个简单的对象,%z 和 %Z格式代码替换为空字符串.

                  For a naive object, the %z and %Z format codes are replaced by empty strings.

                  看起来这仅在> = 2.6中实现,我认为您必须手动解析它.

                  It looks like this is implemented only in >= 2.6, and I think you have to manually parse it.

                  除了删除时区数据,我看不到其他解决方案:

                  I can't see another solution than to remove the time zone data:

                  from datetime import timedelta,datetime
                  try:
                      offset = int("Tue, 22 Jul 2008 08:17:41 +0300"[-5:])
                  except:
                      print "Error"
                  
                  delta = timedelta(hours = offset / 100)
                  
                  fmt = "%a, %d %b %Y %H:%M:%S"
                  time = datetime.strptime("Tue, 22 Jul 2008 08:17:41 +0200"[:-6], fmt)
                  time -= delta
                  

                  这篇关于Python 时间老化,第 2 部分:时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:python - 带有时区的日期时间到纪元 下一篇:Python在正确的时区获取当前时间

                  相关文章

                    <bdo id='BdSQa'></bdo><ul id='BdSQa'></ul>
                • <i id='BdSQa'><tr id='BdSQa'><dt id='BdSQa'><q id='BdSQa'><span id='BdSQa'><b id='BdSQa'><form id='BdSQa'><ins id='BdSQa'></ins><ul id='BdSQa'></ul><sub id='BdSQa'></sub></form><legend id='BdSQa'></legend><bdo id='BdSQa'><pre id='BdSQa'><center id='BdSQa'></center></pre></bdo></b><th id='BdSQa'></th></span></q></dt></tr></i><div id='BdSQa'><tfoot id='BdSQa'></tfoot><dl id='BdSQa'><fieldset id='BdSQa'></fieldset></dl></div>

                  1. <tfoot id='BdSQa'></tfoot><legend id='BdSQa'><style id='BdSQa'><dir id='BdSQa'><q id='BdSQa'></q></dir></style></legend>

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