1

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.

Apollo-Roboto
  • 623
  • 5
  • 21

2 Answers2

1

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
FObersteiner
  • 22,500
  • 8
  • 42
  • 72
0

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')
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
lepi
  • 31
  • 3