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

      <tfoot id='W2Ybr'></tfoot>
    1. <small id='W2Ybr'></small><noframes id='W2Ybr'>

      如何将 datetime.time 从 UTC 转换为不同的时区?

      时间:2023-07-03

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

        <tfoot id='nf25x'></tfoot>

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

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

                本文介绍了如何将 datetime.time 从 UTC 转换为不同的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个保存时间的变量,它是 UTC 中的 datetime.time 类型,我希望它转换为其他时区.

                I have variable which holds time which is of type datetime.time in UTC, I wanted it to convert to some other timezone.

                我们可以转换 datetime.datetime 实例中的时区,如此链接所示 - 如何在 Python 中将本地时间转换为 UTC?.我无法弄清楚如何在 datetime.time 实例中转换时区.我不能使用 astimezone 因为 datetime.time 没有这个方法.

                we can convert timezones in datetime.datetime instance as shown in this SO link - How do I convert local time to UTC in Python?. I am not able to figure out how to convert timezones in datetime.time instances. I can't use astimezone because datetime.time doesn't have this method.

                例如:

                >>> t = d.datetime.now().time()
                >>> t
                datetime.time(12, 56, 44, 398402)
                >>> 
                

                我需要 UTC 格式的t".

                I need 't' in UTC format.

                推荐答案

                有四种情况:

                1. input datetime.time 设置了 tzinfo (例如 OP 提到 UTC)
                1. input datetime.time has tzinfo set (eg OP mentions UTC)
                1. 输出为非天真的时间
                2. 作为原始时间输出(tzinfo 未设置)

              1. 输入 datetime.time 未设置 tzinfo

                1. 输出为非天真的时间
                2. 作为原始时间输出(tzinfo 未设置)

              2. 正确答案需要使用 datetime.datetime.timetz() 函数,因为 datetime.time 不能通过调用 构建为非朴素时间戳>localize()astimezone() 直接.

                The correct answer needs to make use of datetime.datetime.timetz() function because datetime.time cannot be built as a non-naive timestamp by calling localize() or astimezone() directly.

                from datetime import datetime, time
                import pytz
                
                def timetz_to_tz(t, tz_out):
                    return datetime.combine(datetime.today(), t).astimezone(tz_out).timetz()
                
                def timetz_to_tz_naive(t, tz_out):
                    return datetime.combine(datetime.today(), t).astimezone(tz_out).time()
                
                def time_to_tz(t, tz_out):
                    return tz_out.localize(datetime.combine(datetime.today(), t)).timetz()
                
                def time_to_tz_naive(t, tz_in, tz_out):
                    return tz_in.localize(datetime.combine(datetime.today(), t)).astimezone(tz_out).time()
                

                基于 OP 要求的示例:

                Example based on OP requirement:

                t = time(12, 56, 44, 398402)
                time_to_tz(t, pytz.utc) # assigning tzinfo= directly would not work correctly with other timezones
                
                datetime.time(12, 56, 44, 398402, tzinfo=<UTC>)
                

                如果需要简单的时间戳:

                In case naive timestamp is wanted:

                time_to_tz_naive(t, pytz.utc, pytz.timezone('Europe/Berlin'))
                
                datetime.time(14, 56, 44, 398402)
                

                time() 实例已经 tzinfo 设置的情况更容易,因为 datetime.combine 从传递的参数中提取 tzinfo, 所以我们只需要转换成 tz_out.

                The cases where the time() instance has already tzinfo set are easier because datetime.combine picks up the tzinfo from the passed parameter, so we just need to convert to tz_out.

                这篇关于如何将 datetime.time 从 UTC 转换为不同的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:为什么 pytz localize() 不生成一个 tzinfo 与本地化它的 tz 对象匹配的 datetime 对象 下一篇:在 Windows 上获取本地时区名称(Python 3.9 zoneinfo)

                相关文章

              3. <small id='b60xQ'></small><noframes id='b60xQ'>

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

                2. <tfoot id='b60xQ'></tfoot>

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