如何从 Arrow 日期对象获取时区

时间:2021-07-01 21:18:29

标签: python timezone python-arrow

我需要从 Arrow 日期对象中检索时区

我们以此为例:

import arrow
arrow_date = arrow.get("2000-01-01", tzinfo="America/Toronto")

我怎样才能完全按照上面的代码返回这个 tzinfo?

我尝试了以下操作:arrow_date.format("ZZZ") 但这返回的缩写在我的情况下不起作用。

1 个答案:

答案 0 :(得分:0)

您可以使用 tzinfo 的 _filename 方法:

import arrow

arrow_date = arrow.get("2000-01-01", tzinfo="America/Toronto")
print(arrow_date.tzinfo._filename)
# Canada/Eastern

另见How to find the timezone name from a tzfile in python


对于带有来自 zoneinfo 的 tzinfo 的标准 lib datetime 对象,您可以简单地使用 __str__ 方法:

from datetime import datetime
from zoneinfo import ZoneInfo # Python 3.9

dtobj = datetime.fromisoformat("2000-01-01").replace(tzinfo=ZoneInfo("America/Toronto"))
tzinfo = str(dtobj.tzinfo)
print(tzinfo)
# America/Toronto
相关问题