• <tfoot id='ZCRTK'></tfoot>
  • <small id='ZCRTK'></small><noframes id='ZCRTK'>

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

      <legend id='ZCRTK'><style id='ZCRTK'><dir id='ZCRTK'><q id='ZCRTK'></q></dir></style></legend>
          <bdo id='ZCRTK'></bdo><ul id='ZCRTK'></ul>

        Python在正确的时区获取当前时间

        Python get current time in right timezone(Python在正确的时区获取当前时间)
      1. <legend id='gYM0t'><style id='gYM0t'><dir id='gYM0t'><q id='gYM0t'></q></dir></style></legend>
        <tfoot id='gYM0t'></tfoot>

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

                  <tbody id='gYM0t'></tbody>

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

                1. 本文介绍了Python在正确的时区获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我现在用

                  import datetime
                  print(datetime.datetime.now().strftime("%X"))
                  

                  将当前时间显示为字符串.
                  问题是,我的电脑运行在 Europe/Berlin 时区,这里不考虑 +2 到 UTC 的偏移量.而不是 19:22:26 它应该显示 21:22:26也不同于我在这里找到的其他答案,我不通过调用来存储它

                  to display the current time as a string.
                  Problem is, my computer is running in Europe/Berlin time zone, and the offset of +2 to UTC is not accounted here. Instead of 19:22:26 it should display 21:22:26 Also different to the other answers I found here, I do not store it by calling

                  datetime.datetime(2014, 7, 10, 18, 44, 59, 193982, tzinfo=<UTC>)
                  

                  但是

                  datetime.datetime.now()
                  

                  所以我尝试了以下方法(但失败了):

                  so I tried (and failed) the following:

                  >>> from pytz import timezone
                  >>> datetime.datetime.now().astimezone(timezone('Europe/Berlin'))
                   ValueError: astimezone() cannot be applied to a naive datetime
                  


                  无法作为答案发布,因为此问题已标记为已关闭

                  我遇到此问题的服务器不再存在.不管怎样,这里还有一些值得检查的地方:

                  The server I had this issue with doesn't exists any longer. Anyway, here are some other things worth checking:

                  • 您的服务器/系统的时区设置是否正确?
                    • VM 或 docker 容器可能与主机不同步,这值得检查.

                    推荐答案

                    将本地时区的当前时间作为一个简单的 datetime 对象:

                    To get the current time in the local timezone as a naive datetime object:

                    from datetime import datetime
                    naive_dt = datetime.now()
                    

                    如果它没有返回预期的时间,则表示您的计算机配置错误.你应该先修复它(它与 Python 无关).

                    If it doesn't return the expected time then it means that your computer is misconfigured. You should fix it first (it is unrelated to Python).

                    以 UTC 格式获取当前时间作为原始日期时间对象:

                    To get the current time in UTC as a naive datetime object:

                    naive_utc_dt = datetime.utcnow()
                    

                    在 Python 3.3+ 中将当前时间作为可感知的日期时间对象获取:

                    To get the current time as an aware datetime object in Python 3.3+:

                    from datetime import datetime, timezone
                    
                    utc_dt = datetime.now(timezone.utc) # UTC time
                    dt = utc_dt.astimezone() # local time
                    

                    从 tz 数据库中获取给定时区的当前时间:

                    To get the current time in the given time zone from the tz database:

                    import pytz
                    
                    tz = pytz.timezone('Europe/Berlin')
                    berlin_now = datetime.now(tz)
                    

                    它在 DST 转换期间工作.如果时区在过去有不同的 UTC 偏移量,它就可以工作,即,即使时区在不同时间对应于多个 tzinfo 对象,它也可以工作.

                    It works during DST transitions. It works if the timezone had different UTC offset in the past i.e., it works even if the timezone corresponds to multiple tzinfo objects at different times.

                    这篇关于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% 就足够了)

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

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

                            <tfoot id='TTtst'></tfoot>

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