<legend id='ihA01'><style id='ihA01'><dir id='ihA01'><q id='ihA01'></q></dir></style></legend>
      <bdo id='ihA01'></bdo><ul id='ihA01'></ul>
    <tfoot id='ihA01'></tfoot>
    1. <small id='ihA01'></small><noframes id='ihA01'>

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

      python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?

      时间:2023-07-03

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

              <legend id='Msn6d'><style id='Msn6d'><dir id='Msn6d'><q id='Msn6d'></q></dir></style></legend>
            • <small id='Msn6d'></small><noframes id='Msn6d'>

                <tbody id='Msn6d'></tbody>
                <bdo id='Msn6d'></bdo><ul id='Msn6d'></ul>
              • 本文介绍了python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我必须将像 "2012-11-01T04:16:13-04:00" 这样的时区感知字符串转换为 Python datetime 对象.

                I have to convert a timezone-aware string like "2012-11-01T04:16:13-04:00" to a Python datetime object.

                我看到了具有解析功能的 dateutil 模块,但我真的不想使用它,因为它添加了依赖项.

                I saw the dateutil module which has a parse function, but I don't really want to use it as it adds a dependency.

                那我该怎么做呢?我尝试了类似以下的方法,但没有运气.

                So how can I do it? I have tried something like the following, but with no luck.

                datetime.datetime.strptime("2012-11-01T04:16:13-04:00", "%Y-%m-%dT%H:%M:%S%Z")
                

                推荐答案

                从 Python 3.7 开始,datetime.datetime.fromisoformat() 可以处理您的格式:

                As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:

                >>> import datetime
                >>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00')
                datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))
                

                在较旧的 Python 版本中,您无法做到这一点,除非没有大量艰苦的手动时区定义.

                In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.

                Python 不包含时区数据库,因为它会很快过时.相反,Python 依赖于可以具有更快发布周期的外部库来为您提供正确配置的时区.

                Python does not include a timezone database, because it would be outdated too quickly. Instead, Python relies on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.

                作为副作用,这意味着时区解析也需要是一个外部库.如果 dateutil 对你来说太重了,请使用 iso8601 相反,它会很好地解析您的特定格式:

                As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:

                >>> import iso8601
                >>> iso8601.parse_date('2012-11-01T04:16:13-04:00')
                datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>)
                

                iso8601 高达 4KB 小.比较 python-dateutil 的 148KB.

                iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.

                从 Python 3.2 开始,Python 可以处理简单的基于偏移的时区,并且 %z 将解析 -hhmm+hhmm 时间戳中的时区偏移量.这意味着对于 ISO 8601 时间戳,您必须删除时区中的 ::

                As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:

                >>> from datetime import datetime
                >>> iso_ts = '2012-11-01T04:16:13-04:00'
                >>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
                datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))
                

                Python 问题 15873 中正在跟踪缺乏正确的 ISO 8601 解析.

                The lack of proper ISO 8601 parsing is being tracked in Python issue 15873.

                这篇关于python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从电子邮件中解析带有时区的日期? 下一篇:pytz 本地化与日期时间替换

                相关文章

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

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

                    <bdo id='yHbQc'></bdo><ul id='yHbQc'></ul>
                2. <tfoot id='yHbQc'></tfoot>
                  1. <legend id='yHbQc'><style id='yHbQc'><dir id='yHbQc'><q id='yHbQc'></q></dir></style></legend>