Get system local timezone in python

ntg picture ntg · Jan 28, 2016 · Viewed 32.8k times · Source

Seems strange, but I cannot find an easy way to find the local timezone using Pandas/pytz in Python.

I can do:

>>> pd.Timestamp('now', tz='utc').isoformat()
Out[47]: '2016-01-28T09:36:35.604000+00:00'
>>> pd.Timestamp('now').isoformat()
Out[48]: '2016-01-28T10:36:41.830000'
>>> pd.Timestamp('now').tz_localize('utc') - pd.Timestamp('now', tz='utc')
Out[49]: Timedelta('0 days 01:00:00')

Which will give me the timezone, but this is probably not the best way to do it... Is there a command in pytz or pandas to get the system time zone? (preferably in python 2.7 )

Answer

Selcuk picture Selcuk · Jan 28, 2016

I don't think this is possible using pytz or pandas, but you can always install python-dateutil or tzlocal:

from dateutil.tz import tzlocal
datetime.now(tzlocal())

or

from tzlocal import get_localzone
local_tz = get_localzone()