Get local system timezone in python

It seems odd, but I can't 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 a timezone, but this is probably not the best way to do this ... Is there a command in pytz or pandas to get the system timezone? (preferably in python 2.7)

+12
source share
3 answers

I don't think this is possible with 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() 
+17
source

time.timezone should work.

Local (non-summer) time zone offset in seconds west of UTC (negative in most countries of Western Europe, positive in the USA, zero in the UK).

Division by 3600 will give you the offset in hours:

 import time print(time.timezone / 3600.0) 

It does not require any additional Python libraries.

+12
source

Although it does not use pytz / Pandas, other answers are not suitable either, so I decided to publish what I use in mac / linux:

 import subprocess timezone = subprocess.check_output("date +%Z") 

Advantages over other answers: takes into account daylight saving time, does not require installation of additional libraries.

+2
source

Source: https://habr.com/ru/post/1241702/


All Articles