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

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

  • <tfoot id='daIIR'></tfoot>

      1. <i id='daIIR'><tr id='daIIR'><dt id='daIIR'><q id='daIIR'><span id='daIIR'><b id='daIIR'><form id='daIIR'><ins id='daIIR'></ins><ul id='daIIR'></ul><sub id='daIIR'></sub></form><legend id='daIIR'></legend><bdo id='daIIR'><pre id='daIIR'><center id='daIIR'></center></pre></bdo></b><th id='daIIR'></th></span></q></dt></tr></i><div id='daIIR'><tfoot id='daIIR'></tfoot><dl id='daIIR'><fieldset id='daIIR'></fieldset></dl></div>
        <legend id='daIIR'><style id='daIIR'><dir id='daIIR'><q id='daIIR'></q></dir></style></legend>
      2. 在 Windows 上获取本地时区名称(Python 3.9 zoneinfo)

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

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

                  <bdo id='FYu7C'></bdo><ul id='FYu7C'></ul>
                    <tbody id='FYu7C'></tbody>
                  <tfoot id='FYu7C'></tfoot>

                  本文介绍了在 Windows 上获取本地时区名称(Python 3.9 zoneinfo)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  查看 zoneinfo Python 3.9 中的模块,我想知道它是否还提供了一个方便的选项来检索 Windows 上的本地时区(操作系统设置).

                  Checking out the zoneinfo module in Python 3.9, I was wondering if it also offers a convenient option to retrieve the local time zone (OS setting) on Windows.

                  在 GNU/Linux 上,你可以这样做

                  On GNU/Linux, you can do

                  from datetime import datetime
                  from zoneinfo import ZoneInfo
                  
                  naive = datetime(2020, 6, 11, 12)
                  aware = naive.replace(tzinfo=ZoneInfo('localtime'))
                  

                  但在 Windows 上,会抛出

                  but on Windows, that throws

                  ZoneInfoNotFoundError: '没有找到关键本地时间的时区'

                  ZoneInfoNotFoundError: 'No time zone found with key localtime'

                  所以我还需要使用第三方库吗?例如

                  so would I still have to use a third-party library? e.g.

                  import time
                  import dateutil
                  
                  tzloc = dateutil.tz.gettz(time.tzname[time.daylight])
                  aware = naive.replace(tzinfo=tzloc)
                  

                  由于 time.tzname[time.daylight] 返回一个本地化名称(在我的例子中是德语,例如Mitteleuropische Sommerzeit"),这也不起作用:

                  Since time.tzname[time.daylight] returns a localized name (German in my case, e.g. 'Mitteleuropische Sommerzeit'), this doesn't work either:

                  aware = naive.replace(tzinfo=ZoneInfo(tzloc))
                  

                  有什么想法吗?

                  附言在 Python 上试试这个 <3.9,使用backports(另见this answer):

                  p.s. to try this on Python < 3.9, use backports (see also this answer):

                  pip install backports.zoneinfo
                  pip install tzdata # needed on Windows
                  

                  推荐答案

                  使用系统本地时区不需要使用zoneinfo.您可以在调用 None(或省略)时区rel="noreferrer">datetime.astimezone.

                  You don't need to use zoneinfo to use the system local time zone. You can simply pass None (or omit) the time zone when calling datetime.astimezone.

                  来自文档:

                  如果调用时不带参数(或使用 tz=None),则假定系统本地时区.转换后的日期时间实例的 .tzinfo 属性将设置为时区实例,其区域名称和偏移量从操作系统获取.

                  If called without arguments (or with tz=None) the system local timezone is assumed. The .tzinfo attribute of the converted datetime instance will be set to an instance of timezone with the zone name and offset obtained from the OS.

                  因此:

                  from datetime import datetime
                  
                  naive = datetime(2020, 6, 11, 12)
                  aware = naive.astimezone()
                  

                  这篇关于在 Windows 上获取本地时区名称(Python 3.9 zoneinfo)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何将 datetime.time 从 UTC 转换为不同的时区? 下一篇:Python:给定当前 UTC 时间,您如何确定特定时区一天的开始和结束时间?

                  相关文章

                  • <bdo id='CIbYm'></bdo><ul id='CIbYm'></ul>
                  <tfoot id='CIbYm'></tfoot>

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

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

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