【发布时间】:2016-09-03 17:52:07
【问题描述】:
我需要知道在 pytz 上使用缩写时区名称(如 PST、IST 等)的方式。
现在我可以使用“America/Los_Angeles”之类的时区名称在时区之间进行转换。
相反,我需要找到使用时区名称的方法,例如 PST、IST 等,
我现在用于转换的示例代码。
def local2utc(self, dt):
from_zone = tz.gettz('America/Los_Angeles')
to_zone = tz.gettz('UTC')
local = dt.replace(tzinfo=from_zone)
print("converted time")
print(local.astimezone(to_zone).replace(tzinfo = None))
return local.astimezone(to_zone).replace(tzinfo = None)
有人告诉我实现相同的方法。
【问题讨论】:
-
1- 这些缩写是否代表您当地的时区?否则结果可能不明确。见Parsing date/time string with timezone abbreviated name in Python? 2- 你的
local2utc()是错误的。不要将.replace(tzinfo)与具有非固定UTC 偏移量的时区一起使用,例如America/Los_Angeles。见Datetime Timezone conversion using pytz