【发布时间】:2020-12-14 02:35:25
【问题描述】:
我正在尝试使用此函数转换时间:
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 中的任何时区。 我应该怎么做才能让它工作?
【问题讨论】:
-
注意,您应该尽可能选择基于地区的时区(例如
Australia/Melbourne)。Etc/GMT±X区域通常用于海上船舶。
标签: python-3.x timezone dst pytz gmt