Python pytz - 将 UTC 和时区转换为本地时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25264811/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:58:52  来源:igfitidea点击:

pytz - Converting UTC and timezone to local time

pythontimezoneutcpytz

提问by Tzach

I have a datetimein utc time zone, for example:

我有一个datetimeUTC 时区,例如:

utc_time = datetime.datetime.utcnow()

And a pytz timezone object:

和一个 pytz 时区对象:

tz = timezone('America/St_Johns')

What is the proper way to convert utc_timeto the given timezone?

转换utc_time为给定时区的正确方法是什么?

采纳答案by Tzach

I think I got it:

我想我明白了:

pytz.utc.localize(utc_time, is_dst=None).astimezone(tz)

This line first converts the naive (time zone unaware) utc_timedatetimeobject to a datetimeobject that contains a timezone (UTC). Then it uses the astimezonefunction to adjust the time according to the requested time zone.

此行首先将原始(不知道时区)utc_timedatetime对象转换为datetime包含时区 (UTC)的对象。然后它使用该astimezone函数根据请求的时区调整时间。

回答by xbello

May I recommend to use arrow? If I understood the question:

我可以推荐使用arrow吗?如果我理解这个问题:

>>> import arrow
>>> utc = arrow.utcnow()
>>> utc
<Arrow [2014-08-12T13:01:28.071624+00:00]>    
>>> local = utc.to("America/St_Johns")
>>> local
<Arrow [2014-08-12T10:31:28.071624-02:30]>


You can also use

你也可以使用

tz.fromutc(utc_time)

回答by N Randhawa

Another very easy way:

另一个非常简单的方法:

Because utcnowmethod returns a naiveobject, so you have to convert the naiveobject into awareobject. Using replacemethod you can convert a naiveobject into awareobject. Then you can use the astimezonemethod to create new datetime object in a different time zone.

因为utcnow方法返回一个naive对象,所以你必须将naive对象转换成aware对象。使用replace方法,您可以将原始对象转换为感知对象。然后您可以使用该astimezone方法在不同的时区创建新的日期时间对象。

from datetime import datetime
import pytz    
utc_time = datetime.utcnow()
tz = pytz.timezone('America/St_Johns')

utc_time =utc_time.replace(tzinfo=pytz.UTC) #replace method      
st_john_time=utc_time.astimezone(tz)        #astimezone method
print(st_john_time)

回答by awaik

You can also use the sample below, I use it for similar task

您也可以使用下面的示例,我将它用于类似的任务

tz = pytz.timezone('America/St_Johns')
time_difference=tz.utcoffset(utc_time).total_seconds() #time difference between UTC and local timezones in 5:30:00 format
utc_time = date + timedelta(0,time_difference)

It works fast and you don't need to import additional libraries.

它运行速度很快,您无需导入额外的库。

回答by user8808265

It's the exact purpose of fromutcfunction:

这是fromutc函数的确切目的:

tz.fromutc(utc_time)

(astimezonefunction calls fromutcunder the hood, but tries to convert to UTC first, which is unneeded in your case)

astimezone函数fromutc在后台调用,但首先尝试转换为 UTC,这在您的情况下是不需要的)

回答by paolov

I agree with Tzach's answer. Just wanted to include that the is_dst parameter is not required:

我同意 Tzach 的回答。只是想包括不需要 is_dst 参数:

pytz.utc.localize(datetime.utcnow()).astimezone(tz)

That code converts the current UTC time to a timezone aware current datetime.

该代码将当前 UTC 时间转换为时区感知的当前日期时间。

Whereas the code below converts the current UTC time to a timezone aware datetime which is not necessarily current. The timezone is just appended into the UTC time value.

而下面的代码将当前的 UTC 时间转换为不一定是当前的时区感知日期时间。时区只是附加到 UTC 时间值中。

tz.localize(datetime.utcnow())