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

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

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

      <legend id='mxoVM'><style id='mxoVM'><dir id='mxoVM'><q id='mxoVM'></q></dir></style></legend>
        <tfoot id='mxoVM'></tfoot>
      1. Python时区转换

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

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

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

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

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

                  问题描述

                  我正在寻找一种快速输入时间的方法,然后 python 将其转换为其他时区(可能多达 10 个不同的时区)

                  I am looking for a quick way to type in a time and then python convert it into other timezones ( maybe up to 10 different timezones )

                  对不起.我根本不熟悉python中的时间,如果有人能把我引向正确的方向,我将不胜感激.

                  Sorry. I am not familar with time in python at all, if someone could put me in the right direction I would really appreciate it.

                  推荐答案

                  我发现最好的方法是将感兴趣的时刻"转换为支持 utc-timezone 的 datetime 对象(在 python 中,时区组件是datetime 对象不需要).

                  I have found that the best approach is to convert the "moment" of interest to a utc-timezone-aware datetime object (in python, the timezone component is not required for datetime objects).

                  然后您可以使用 astimezone 转换为感兴趣的时区(参考).

                  Then you can use astimezone to convert to the timezone of interest (reference).

                  from datetime import datetime
                  import pytz
                  
                  utcmoment_naive = datetime.utcnow()
                  utcmoment = utcmoment_naive.replace(tzinfo=pytz.utc)
                  
                  # print "utcmoment_naive: {0}".format(utcmoment_naive) # python 2
                  print("utcmoment_naive: {0}".format(utcmoment_naive))
                  print("utcmoment:       {0}".format(utcmoment))
                  
                  localFormat = "%Y-%m-%d %H:%M:%S"
                  
                  timezones = ['America/Los_Angeles', 'Europe/Madrid', 'America/Puerto_Rico']
                  
                  for tz in timezones:
                      localDatetime = utcmoment.astimezone(pytz.timezone(tz))
                      print(localDatetime.strftime(localFormat))
                  
                  # utcmoment_naive: 2017-05-11 17:43:30.802644
                  # utcmoment:       2017-05-11 17:43:30.802644+00:00
                  # 2017-05-11 10:43:30
                  # 2017-05-11 19:43:30
                  # 2017-05-11 13:43:30
                  

                  因此,在本地时区感兴趣的时刻(存在的时间),您可以像这样将其转换为UTC(参考).

                  So, with the moment of interest in the local timezone (a time that exists), you convert it to utc like this (reference).

                  localmoment_naive = datetime.strptime('2013-09-06 14:05:10', localFormat)
                  
                  localtimezone = pytz.timezone('Australia/Adelaide')
                  
                  try:
                      localmoment = localtimezone.localize(localmoment_naive, is_dst=None)
                      print("Time exists")
                  
                      utcmoment = localmoment.astimezone(pytz.utc)
                  
                  except pytz.exceptions.NonExistentTimeError as e:
                      print("NonExistentTimeError")
                  

                  这篇关于Python时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:在 Python 中使用时区缩写名称解析日期/时间字符串? 下一篇:如何获得“午夜"的 UTC 时间?对于给定的时区?

                  相关文章

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

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

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