<bdo id='MgS5Q'></bdo><ul id='MgS5Q'></ul>

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

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

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

  • <tfoot id='MgS5Q'></tfoot>
      1. 如何在python中将时区添加到天真的日期时间实例中

        How to add timezone into a naive datetime instance in python(如何在python中将时区添加到天真的日期时间实例中)

            <bdo id='MDvhZ'></bdo><ul id='MDvhZ'></ul>

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

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

                  <tbody id='MDvhZ'></tbody>

                1. 本文介绍了如何在python中将时区添加到天真的日期时间实例中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个没有时区信息的 datetime.我现在正在获取时区信息并想将时区添加到现有的日期时间实例中,我该怎么做?

                  I've got a datetime which has no timezone information. I'm now getting the timezone info and would like to add the timezone into the existed datetime instance, how can I do?

                  d = datetime.datetime.now()
                  tz = pytz.timezone('Asia/Taipei')
                  

                  如何将时区信息 tz 添加到 datetime a

                  How to add the timezone info tz into datetime a

                  推荐答案

                  使用 tz.localize(d) 对实例进行本地化.来自文档:

                  Use tz.localize(d) to localize the instance. From the documentation:

                  首先是使用pytz库提供的localize()方法.这用于本地化一个简单的日期时间(没有时区信息的日期时间):

                  The first is to use the localize() method provided by the pytz library. This is used to localize a naive datetime (datetime with no timezone information):

                  >>> loc_dt = eastern.localize(datetime(2002, 10, 27, 6, 0, 0))
                  >>> print(loc_dt.strftime(fmt))
                  2002-10-27 06:00:00 EST-0500
                  

                  如果您使用tz.localize(),但使用datetime.replace(),那么historical 使用偏移量代替;tz.localize() 将选择对给定日期有效的正确偏移量.例如,美国东部时区 DST 的开始和结束日期随时间而变化.

                  If you don't use tz.localize(), but use datetime.replace(), chances are that a historical offset is used instead; tz.localize() will pick the right offset in effect for the given date. The US Eastern timezone DST start and end dates have changed over time, for example.

                  当您尝试本地化一个不明确的日期时间值时,因为它跨越从夏季到冬季时间的过渡期,反之亦然,时区将被查询以查看生成的日期时间对象是否应该具有 .dst() 返回 True 或 False.您可以使用 .localize()is_dst 关键字参数覆盖时区的默认值:

                  When you try to localize a datetime value that is ambiguous because it straddles the transition period from summer to winter time or vice-versa, the timezone will be consulted to see if the resulting datetime object should have .dst() return True or False. You can override the default for the timezone with the is_dst keyword argument for .localize():

                  dt = tz.localize(naive, is_dst=True)
                  

                  甚至通过设置 is_dst=None 完全关闭选择.在这种情况下,或者在极少数情况下, 没有为时区设置默认值,不明确的日期时间值将导致引发 AmbiguousTimeError 异常.is_dst 标志仅用于不明确的日期时间值,否则将被忽略.

                  or even switch off the choice altogether by setting is_dst=None. In that case, or in the rare cases there is no default set for a timezone, an ambiguous datetime value would lead to a AmbiguousTimeError exception being raised. The is_dst flag is only consulted for datetime values that are ambiguous and is ignored otherwise.

                  要返回另一种方式,将时区感知对象变回原始对象,请使用 .replace(tzinfo=None):

                  To go back the other way, turn a timezone-aware object back to a naive object, use .replace(tzinfo=None):

                  naivedt = awaredt.replace(tzinfo=None)
                  

                  这篇关于如何在python中将时区添加到天真的日期时间实例中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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% 就足够了)

                2. <legend id='A9BCw'><style id='A9BCw'><dir id='A9BCw'><q id='A9BCw'></q></dir></style></legend>
                  • <bdo id='A9BCw'></bdo><ul id='A9BCw'></ul>

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

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