pythontimezonepython-arrow

How can I get the timezone from an Arrow date object


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.


Solution

  • 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