Python 将时间从UTC更改为PDT

Python 将时间从UTC更改为PDT,python,Python,这就是我所拥有的: 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 或 因此,结果是: result = 2016-06-15 16:27:52 -0700 使用python包,可以使用以下简单脚本: import arrow fmt = "YYYY-MM-DD H

这就是我所拥有的:

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

因此,结果是:

result = 2016-06-15 16:27:52 -0700
使用python包,可以使用以下简单脚本:

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安装箭头安装箭头

编辑:如果不想使用
箭头
程序包:

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
软件包

谢谢,但我一直在寻找一个不导入标准库以外的任何内容的解决方案。@emprio如果没有任何包,这是不可能的。一些解决方案使用时间、日历、日期时间包
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)