简体   繁体   English

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

[英]How to convert datetime.time from UTC to different timezone?

I have variable which holds time which is of type datetime.time in UTC, I wanted it to convert to some other timezone. 我有变量,其中包含UTC时间类型为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? 我们可以在datetime.datetime实例中转换时区,如此SO链接所示 - 如何在Python中将本地时间转换为UTC? . I am not able to figure out how to convert timezones in datetime.time instances. 我无法弄清楚如何在datetime.time实例中转换时区。 I can't use astimezone because datetime.time doesn't have this method. 我不能使用astimezone,因为datetime.time没有这个方法。

For example: 例如:

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

I need 't' in UTC format. 我需要UTC格式的't'。

I would create a temp datetime object, convert the tz, and extract the time again. 我会创建一个临时日期时间对象,转换tz,然后再次提取时间。

import datetime
def time_to_utc(t):
    dt = datetime.datetime.combine(datetime.date.today(), t)
    utc_dt = datetime_to_utc(dt)
    return utc_dt.time()

t = datetime.datetime.now().time()
utc_t = time_to_utc(t)

where, datetime_to_utc is any of the suggestions in the linked question . 其中, datetime_to_utc链接问题中的任何建议。

There are four cases: 有四种情况:

  1. input datetime.time has tzinfo set (eg OP mentions UTC) 输入datetime.time设置了tzinfo (例如OP提到UTC)
    1. output as non-naive time 输出为非天真时间
    2. output as naive time ( tzinfo not set) 输出为天真时间( tzinfo未设置)
  2. input datetime.time has tzinfo not set 输入datetime.time没有设置tzinfo
    1. output as non-naive time 输出为非天真时间
    2. output as naive time ( tzinfo not set) 输出为天真时间( tzinfo未设置)

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. 正确的答案需要使用datetime.datetime.timetz()函数,因为通过直接调用localize()astimezone()无法将datetime.time构建为非天真的时间戳。

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()

Example based on OP requirement: 基于OP要求的示例:

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)

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 . time()实例已设置tzinfo情况更容易,因为datetime.combine从传递的参数中获取tzinfo ,因此我们只需要转换为tz_out

Easy way to convert from/to UTC timezone using pytz : 使用pytz从/到UTC时区转换的简便方法:

import datetime, pytz

def time_to_utc(naive, timezone="Europe/Istanbul"):
    local = pytz.timezone(timezone)
    local_dt = local.localize(naive, is_dst=None)
    utc_dt = local_dt.astimezone(pytz.utc)
    return utc_dt

def utc_to_time(naive, timezone="Europe/Istanbul"):
    return naive.replace(tzinfo=pytz.utc).astimezone(pytz.timezone(timezone))

# type(naive) """DateTime"""
# type(timezone) """String"""

You need pytz . 你需要pytz The use case you describe is illustrated in their primary examples. 您描述的用例在其主要示例中进行了说明。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM