Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Pytz中使用缩写时区名称(PST,IST)_Python_Datetime_Pytz - Fatal编程技术网

Python 如何在Pytz中使用缩写时区名称(PST,IST)

Python 如何在Pytz中使用缩写时区名称(PST,IST),python,datetime,pytz,Python,Datetime,Pytz,我需要知道如何在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(tzin

我需要知道如何在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-这些缩写代表你的本地时区吗?否则,结果可能不明确。请参见2-您的
local2utc()
错误。不要使用
。将(tzinfo)
替换为具有非固定utc偏移量的时区,例如
美国/洛杉矶
。参见1-这些缩写是否代表您当地的时区?否则,结果可能不明确。请参见2-您的
local2utc()
错误。不要使用
。将(tzinfo)
替换为具有非固定utc偏移量的时区,例如
美国/洛杉矶
。看见