How can I get the timezone from an Arrow date object

600 views Asked by At

I need to retrieve the timezone from an Arrow date object

Let's take this as an example:

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

How can I return this tzinfo exactly as it is in the code above?

I tried the following: arrow_date.format("ZZZ") but this returns an abbreviation that won't work in my situation.

2

There are 2 answers

0
FObersteiner On BEST ANSWER

You can use the _filename method of the tzinfo:

import arrow

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

see also How to find the timezone name from a tzfile in python.


For a standard lib datetime object with tzinfo from zoneinfo, you can simply use the __str__ method:

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
0
lepi On

Recently, I needed just the tzinfo object, and instead of using it from dateutil I used arrow.

Simple example:

utc = arrow.now('UTC').tzinfo
nyc = arrow.now('America/New_York').tzinfo

In [27]: nyc
Out[27]: tzfile('/usr/share/zoneinfo/America/New_York')