Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: print the time zone from strftime

Tags:

python

date

time

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?

like image 800
ishanka ganepola Avatar asked Jul 08 '15 17:07

ishanka ganepola


2 Answers

For me the easiest:

$ python3
>>> import datetime
>>> datetime.datetime.now().astimezone().strftime("%Y-%m-%dT%H:%M:%S %z")
>>> datetime.datetime.now().astimezone().strftime("%Y-%m-%dT%H:%M:%S %Z")
>>> exit()
like image 149
davgut Avatar answered Oct 31 '22 00:10

davgut


It is a documented behavior: datetime.now() returns a naive datetime object and %Z returns an empty string in such cases. You need an aware datetime object instead.

To print a local timezone abbreviation, you could use tzlocal module that can return your local timezone as a pytz tzinfo object that may contain a historical timezone info e.g., from the tz database:

#!/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, 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).

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.

like image 11
jfs Avatar answered Oct 30 '22 23:10

jfs