Python: print the time zone from strftime
Question: I want to print the time zone. I used `%Z` but it doesn't print:

    
    
    import datetime
    now = datetime.datetime.now()
    print now.strftime("%d-%m-%Y")
    print now.strftime("%d-%b-%Y")
    print now.strftime("%a,%d-%b-%Y %I:%M:%S %Z") # %Z doesn't work
    

Do I perhaps need to import `pytz`?


Answer: It is a documented behavior: `datetime.now()` returns a naive datetime object
and [`%Z` returns an empty string in such
cases](https://docs.python.org/2.7/library/datetime.html#strftime-and-
strptime-behavior). You need an aware datetime object instead.

To print a local timezone abbreviation, you could use [`tzlocal`
module](https://pypi.python.org/pypi/tzlocal) that can return your local
timezone as a [`pytz`](http://pytz.sourceforge.net) tzinfo object that may
contain a historical timezone info e.g., from [the tz
database](https://www.iana.org/time-zones/repository/tz-link.html):

    
    
    #!/usr/bin/env python
    from datetime import datetime
    import tzlocal # $ pip install tzlocal
    
    now = datetime.now(tzlocal.get_localzone())
    print(now.strftime('%Z'))
    # -> MSK
    print(now.tzname())
    # -> MSK
    

This code works for timezones with/without daylight saving time. It works
around and during DST transitions. It works if the local timezone had
different utc offset in the past even if the C library used by python has no
access to a historical timezone database on the given platform.

* * *

In Python 3.3+, when platform supports it, you could use [`.tm_zone`
attribute](https://docs.python.org/3/library/time.html#time.struct_time), to
get the tzname:

    
    
    >>> import time
    >>> time.localtime().tm_zone
    'MSK'
    

Or using `datetime` module:

    
    
    >>> from datetime import datetime, timezone
    >>> datetime.now(timezone.utc).astimezone().tzname()
    'MSK'
    

The code is portable but the result may be incorrect on some platforms
(without `.tm_zone` (`datetime` has to use `time.tzname` in this case) and
with ["interesting" timezones](http://bugs.python.org/issue1647654)).

On older Python versions, on a system with an "uninteresting" timezone, you
could use `time.tzname`:

    
    
    >>> import time
    >>> is_dst = time.daylight and time.localtime().tm_isdst > 0
    >>> time.tzname[is_dst]
    'MSK'
    

An example of an "interesting" timezone is Europe/Moscow timezone in 2010-2015
period.

Similar issues are discussed in [Getting computer's UTC offset in
Python](http://stackoverflow.com/a/3168394/4279).

