这是我的:
pat = '%Y-%m-%d %H:%M:%S +0000'
my_time = time.strptime(task_time, pattern)
但是,如何更改时区:
my_time:
2016-06-15 23:27:52 +0000
到不同的时区:
PDT
或
-0700
所以结果是:
result = 2016-06-15 16:27:52 -0700
请您参考如下方法:
使用 python 包 arrow可以使用这个简单的脚本:
import arrow
fmt = "YYYY-MM-DD HH:mm:ss Z"
time = arrow.get("2016-06-15 23:27:52 +0000", fmt)
time = time.to("US/Pacific")
print(time.format(fmt))
2016-06-15 16:27:52 -0700
使用 pip install arrow 安装 arrow
编辑:如果您不想使用 arrow 包:
import time
import calendar
fmt = "%Y-%m-%d %H:%M:%S "
t = calendar.timegm(time.strptime("2016-06-15 23:27:52 +0000", fmt + "+0000"))
t -= 8 * 60 * 60
s = time.strftime(fmt + "-0700", time.gmtime(t))
print(s)
注意,这是可怕的代码,如果你在生产中使用它,你肯定会被解雇,所以只需安装 arrow 包!
