如何在Pytz中使用缩写的时区名称(PST,IST) [英] How to use abbreviated timezone name(PST, IST) in Pytz
问题描述
我需要知道在pytz上使用缩写时区名称如PST,IST等的方式。
I need to know the way of using Abbreviated timezone name like PST, IST etc on pytz.
现在我可以使用时区名称在时区之间进行转换像美国/ Los_Angeles一样。
Now I can be able to convert between timezone using the timezone name like "America/Los_Angeles".
相反,我需要找到使用时区名称(如PST,IST等)的方式,
Instead I need to find the way of using timezone name like PST, IST etc.,
我现在用于转换的示例代码。
Sample code I have used now for conversion.
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)
有人让我知道实现相同的方式。
Someone let me know the way of achieving the same.
推荐答案
这本质上是不可能的,因为一些缩写可能意味着多个(不同的)时区:
This is by nature impossible, because some abbreviations can mean multiple (different) time zones:
http://www.timeanddate.com/time/zones/
如果你希望它专门为您的时区工作,使字典映射缩写为长名称。
If you want it to work specifically for your timezones, make a dictionary mapping abbreviation to long name.
这篇关于如何在Pytz中使用缩写的时区名称(PST,IST)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!