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

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

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

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

        使用 Python 找出当前是否处于夏令时的时区

        Use Python to find out if a timezone currently in daylight savings time(使用 Python 找出当前是否处于夏令时的时区)
        <tfoot id='6luuM'></tfoot>

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

                  <tbody id='6luuM'></tbody>
                1. 本文介绍了使用 Python 找出当前是否处于夏令时的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我们有一个在 GMT 时间运行的服务器.我需要编写一个 Python 脚本来确定它当前(此时此刻)是否是加利福尼亚州洛杉矶的夏令时(DST).我怎样才能做到这一点?我看了一下 pytz 和 time ,但我无法弄清楚.我意识到我可以创建一些逻辑,例如将 LA 的当前时间与 GMT 时间进行比较,但如果我可以使用标准库代替它会更简洁.

                  We have a server that runs on GMT time. I need to write a Python script that determines if it's currently (at this very second) Daylight Savings Time (DST) in Los Angeles, CA. How can I accomplish this? I took a look at pytz and time, but I can't figure it out. I realize that I could create some logic such as comparing the current time in LA to GMT time, but it would be a lot cleaner if I could use a standard library instead.

                  谢谢

                  这是我设置时区的一些示例代码:

                  Here's some sample code of me setting up the timezone:

                  from pytz import timezone
                  import pytz
                  from datetime import datetime, timedelta
                  
                  tz = timezone('America/Los_Angeles')
                  // Instantiate a datetime object using tz? 
                  

                  这是一段可以工作的代码.它并不优雅,这就是为什么我要问是否有图书馆或为此而设计的东西.也许就像一个 is_dst() 函数.

                  Here's a snippet of code that will work. It's not elegant, which is why I'm asking if there's a library or something that is made for this. Maybe like a is_dst() function.

                  utc = timezone("UTC")
                  now = utc.localize(datetime.utcnow())
                  los_angeles_tz = timezone('America/Los_Angeles')
                  los_angeles_time = now.astimezone(los_angeles_tz)
                  
                  delta = los_angeles_time.utcoffset()
                  dstDelta = timedelta(hours=-8)
                  
                  is_dst = (delta == dstDelta)
                  

                  推荐答案

                  import pytz
                  from datetime import datetime, timedelta
                  
                  def is_dst(zonename):
                      tz = pytz.timezone(zonename)
                      now = pytz.utc.localize(datetime.utcnow())
                      return now.astimezone(tz).dst() != timedelta(0)
                  

                  用法:

                  >>> is_dst("America/Los_Angeles")
                  False
                  
                  >>> is_dst("America/Sao_Paulo")
                  True
                  

                  这篇关于使用 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% 就足够了)
                    <bdo id='QDLGk'></bdo><ul id='QDLGk'></ul>
                      <i id='QDLGk'><tr id='QDLGk'><dt id='QDLGk'><q id='QDLGk'><span id='QDLGk'><b id='QDLGk'><form id='QDLGk'><ins id='QDLGk'></ins><ul id='QDLGk'></ul><sub id='QDLGk'></sub></form><legend id='QDLGk'></legend><bdo id='QDLGk'><pre id='QDLGk'><center id='QDLGk'></center></pre></bdo></b><th id='QDLGk'></th></span></q></dt></tr></i><div id='QDLGk'><tfoot id='QDLGk'></tfoot><dl id='QDLGk'><fieldset id='QDLGk'></fieldset></dl></div>

                        <tbody id='QDLGk'></tbody>
                    • <tfoot id='QDLGk'></tfoot>
                    • <small id='QDLGk'></small><noframes id='QDLGk'>

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