<tfoot id='7L8YR'></tfoot>

      <small id='7L8YR'></small><noframes id='7L8YR'>

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

        • <bdo id='7L8YR'></bdo><ul id='7L8YR'></ul>
        <legend id='7L8YR'><style id='7L8YR'><dir id='7L8YR'><q id='7L8YR'></q></dir></style></legend>

        pytz 和 astimezone() 不能应用于天真的日期时间

        时间:2023-07-03

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

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

            <bdo id='3x3U4'></bdo><ul id='3x3U4'></ul>

              <legend id='3x3U4'><style id='3x3U4'><dir id='3x3U4'><q id='3x3U4'></q></dir></style></legend>
                  <tbody id='3x3U4'></tbody>

                  本文介绍了pytz 和 astimezone() 不能应用于天真的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个日期,我需要让它知道时区.

                  I have a date and I need to make it time zone aware.

                  local_tz = timezone('Asia/Tokyo')
                  start_date = '2012-09-27'
                  start_date = datetime.strptime(start_date, "%Y-%m-%d")   
                  start_date = start_date.astimezone(local_tz)
                  
                  
                  now_utc = datetime.now(timezone('UTC'))
                  local_now = now_utc.astimezone(local_tz)
                  

                  我需要看看这是不是真的:

                  I need to find if this is true:

                  print start_date>local_now
                  

                  但我收到此错误.

                     start_date = start_date.astimezone(local_tz)
                     ValueError: astimezone() cannot be applied to a naive datetime
                  

                  我将 UTC 转换为东京没有问题.我需要在东京制作 start_date 时区感知广告.

                  I convert utc to tokyo with no issue. I need to make start_date timezone aware ad well in tokyo.

                  谢谢

                  推荐答案

                  对于 pytz 时区,使用他们的 .localize() 方法来转换一个幼稚的 datetime 对象与时区合二为一:

                  For pytz timezones, use their .localize() method to turn a naive datetime object into one with a timezone:

                  start_date = local_tz.localize(start_date)
                  

                  对于没有 DST 转换的时区,.replace()将时区附加到天真的 datetime 对象的方法 通常也可以工作:

                  For timezones without a DST transition, the .replace() method to attach a timezone to a naive datetime object should normally also work:

                  start_date = start_date.replace(tzinfo=local_tz)
                  

                  有关详细信息,请参阅 pytz 文档的本地化时间和日期算术.

                  See the localized times and date arithmetic of the pytz documentation for more details.

                  这篇关于pytz 和 astimezone() 不能应用于天真的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:`datetime.now(pytz_timezone)` 什么时候失败? 下一篇:让 Python 响应 Windows 时区的变化

                  相关文章

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

                    1. <tfoot id='r0i7p'></tfoot>

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

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