How can I get the timezone from an Arrow date object

Question:

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.

Asked By: Apollo-Roboto

||

Answers:

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
Answered By: FObersteiner

Recently i neded ijust tzinfo object instead of using it from dateutil will be using 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')
Answered By: lepi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.