Python 如何将 datetime.time 从 UTC 转换为不同的时区?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16603645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert datetime.time from UTC to different timezone?
提问by rajpy
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?. 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.
我们可以在 datetime.datetime 实例中转换时区,如此链接所示 -如何在 Python 中将本地时间转换为 UTC?. 我无法弄清楚如何在 datetime.time 实例中转换时区。我不能使用 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'。
采纳答案by Matt Johnson-Pint
回答by shx2
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_utcis any of the suggestions in the linked question.
其中,datetime_to_utc是链接问题中的任何建议。
回答by Tarek Kalaji
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"""
回答by Dorian B.
There are four cases:
有四种情况:
- input
datetime.timehastzinfoset (eg OP mentions UTC)- output as non-naive time
- output as naive time (
tzinfonot set)
- input
datetime.timehastzinfonot set- output as non-naive time
- output as naive time (
tzinfonot set)
- 输入
datetime.time已tzinfo设置(例如 OP 提到 UTC)- 输出为非初始时间
- 输出为初始时间(
tzinfo未设置)
- 输入
datetime.time还tzinfo没有设置- 输出为非初始时间
- 输出为初始时间(
tzinfo未设置)
The correct answer needs to make use of datetime.datetime.timetz()function because datetime.timecannot be built as a non-naive timestamp by calling localize()or astimezone()directly.
正确答案需要使用datetime.datetime.timetz()函数,因为datetime.time不能通过调用localize()或astimezone()直接构建为非原始时间戳。
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 tzinfoset are easier because datetime.combinepicks up the tzinfofrom the passed parameter, so we just need to convert to tz_out.
其中时间()实例已经案件tzinfo设置更容易,因为datetime.combine拾取tzinfo从传递的参数,所以我们只需要转换为tz_out。

