Skip to content Skip to sidebar Skip to footer

Get System Local Timezone In Python

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

Solution 1:

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

from dateutil.tzimport tzlocal
datetime.now(tzlocal())

or

from tzlocal importget_localzonelocal_tz= get_localzone()

Solution 2:

time.timezone should work.

The offset of the local (non-DST) timezone, in seconds west of UTC (negative in most of Western Europe, positive in the US, zero in the UK).

Dividing by 3600 will give you the offset in hours:

import timeprint(time.timezone / 3600.0)

This does not require any additional Python libraries.

Solution 3:

I have found that in many cases this works: (Since Python 3.6)

from datetime import datetime

# use this extension and it adds the timezone
tznow = datetime.now().astimezone()
print(tznow.isoformat())
2020-11-05T06:56:38.514560-08:00# It shows that it does have a valid timezonetype(tznow.tzinfo)
<class'datetime.timezone'>

I find this handy as it does not depend on external packages. It appears to work only in Python3 (but not in Python2)

Solution 4:

Quite a few locale time related settings from OS level is covered by time module

import time

# Since Python 3.3
local_time = time.localtime()         # returns a `time.struct_time`
tzname_local = local_time.tm_zone     # 'EST'
dst = local_time.tm_isdst             # _from docs_: may be set to 1 when daylight savings time is in effect, # and 0 when it is not. A value of -1 indicates that this is not known, # and will usually result in the correct state being filled in.

tm_gmtoff and tm_zone attributes are available on platforms with C library supporting the corresponding fields in struct tm. see: https://docs.python.org/3/library/time.html#time.struct_time

# At least from Python 2.7.18local_tzname = time.tzname            # 'EST'

A tuple of two strings: the first is the name of the local non-DST timezone, the second is the name of the local DST timezone. If no DST timezone is defined, the second string should not be used. see: https://docs.python.org/2.7/library/time.html#time.tzname)

Another trick is to use datetime.now().astimezone() as found here and the reason why it fails on python 2.x

from datetime import datetime

# Python 3 will return a datetime with local timezone,
local_now = datetime.now().astimezone()    

# Doesn't work on python 2.x # datetime.now().astimezone()                -> TypeError: Required argument 'tz' (pos 1) not found# datetime.now().astimezone(dateutil.tz.UTC) -> ValueError: astimezone() cannot be applied to a naive datetime

local_tz = local_now.tzinfo  # datetime.timezone
local_tzname = local_tz.tzname(local_now)
print(local_tzname) 

Solution 5:

While it doesn't use pytz/Pandas, the other answers don't either, so I figured I should post what I'm using on mac/linux:

importsubprocesstimezone= subprocess.check_output("date +%Z")

Benefits over the other answers: respects daylight savings time, doesn't require additional libraries to be installed.

Post a Comment for "Get System Local Timezone In Python"