问题描述
import pytz
from datetime import datetime
def ConvertTimezone(FromZone,timestring,ToZone):
print("Start of ConvertTimezone from " + str(FromZone) + " " + timestring + " to " + str(ToZone))
TFORMAT = "%Y %m %d %H:%M"
ftz = pytz.timezone(FromZone)
ttz = pytz.timezone(ToZone)
dt_str = datetime.strptime(timestring,TFORMAT)
dt_obj_ftz = ftz.localize(dt_str) #localising accounts for daylight savings
totime = dt_obj_ftz.astimezone(ttz)
return totime.strftime(TFORMAT)
print(ConvertTimezone("Etc/GMT+10","2020 08 30 12:00","Etc/GMT-12"))
我希望输出是
"2020 08 29 14:00" # the day before
但是我得到的是,好像它已经添加了时区差异(22小时)而不是减去了:
"2020 08 31 10:00" # the day after
应该使用户能够使用pytz.all_timezones中的任何时区。 我应该怎么做才能使它起作用?
解决方法
在这里找到结果: Printing datetime as pytz.timezone("Etc/GMT-5") yields incorrect result 显然,出于某些原因,pytz的Etc / GMT时区的优缺点被颠倒了。