Get time zone information of the system in Python?

PythonDatetime

Python Problem Overview


I want to get the default timezone (PST) of my system from Python. What's the best way to do that? I'd like to avoid forking another process.

Python Solutions


Solution 1 - Python

This should work:

import time
time.tzname

time.tzname returns 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.

Example return: ('MST', 'MDT')

Solution 2 - Python

Gives a UTC offset like in ThomasH's answer, but takes daylight savings into account.

>>> import time
>>> offset = time.timezone if (time.localtime().tm_isdst == 0) else time.altzone
>>> offset / 60 / 60 * -1
-9

The value of time.timezone or time.altzone is in seconds West of UTC (with areas East of UTC getting a negative value). This is the opposite to how we'd actually like it, hence the * -1.

time.localtime().tm_isdst will be zero if daylight savings is currently not in effect (although this may not be correct if an area has recently changed their daylight savings law).

EDIT: marr75 is correct, I've edited the answer accordingly.

Solution 3 - Python

Check out the Python Time Module.

from time import gmtime, strftime
print(strftime("%z", gmtime()))

Pacific Standard Time

Solution 4 - Python

I found this to work well:

import datetime
tz_string = datetime.datetime.now(datetime.timezone.utc).astimezone().tzname()

For me this was able to differentiate between daylight savings and not.

Reference with more detail: https://stackoverflow.com/a/39079819/4549682

Solution 5 - Python

The code snippets for calculating offset are incorrect, see http://bugs.python.org/issue7229.

The correct way to handle this is:

def local_time_offset(t=None):
    """Return offset of local zone from GMT, either at present or at time t."""
    # python2.3 localtime() can't take None
    if t is None:
        t = time.time()

    if time.localtime(t).tm_isdst and time.daylight:
        return -time.altzone
    else:
        return -time.timezone

This is in all likelihood, not the exact question that the OP asked, but there are two incorrect snippets on the page and time bugs suck to track down and fix.

Solution 6 - Python

For Python 3.6+ this can be easily achieved by following code:

import datetime
local_timezone = datetime.datetime.utcnow().astimezone().tzinfo

print(local_timezone)

But with Python < 3.6 calling astimezone() on naive datetime doesn't work. So we've to do it in a slightly different way.

So for Python 3.x,

import datetime
local_timezone = datetime.datetime.now(datetime.timezone.utc).astimezone().tzinfo

print(local_timezone)

Sample Output:
On Netherlands Server(Python 3.6.9): CEST
On Bangladesh Server(Python 3.8.2): +06

More details can be found on this thread.

Solution 7 - Python

To obtain timezone information in the form of a datetime.tzinfo object, use dateutil.tz.tzlocal():

from dateutil import tz
myTimeZone = tz.tzlocal()

This object can be used in the tz parameter of datetime.datetime.now():

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

or the tz parameter of datetime object via datetime.datetime.astimezone():

from datetime import datetime
from dateutil import tz
unlocalisedDatetime = datetime.now()
localisedDatetime = unlocalisedDatetime.astimezone(tz = tz.tzlocal())

Solution 8 - Python

Getting offset from UTC as timedelta:

from datetime import datetime, timezone

now = datetime.now()
now.replace(tzinfo=timezone.utc) - now.astimezone(timezone.utc)

Or like this (more obscure but also works):

datetime.now(timezone.utc).astimezone().tzinfo.utcoffset(None)

Both solutions give the same result. For example: datetime.timedelta(seconds=7200)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJosh GibsonView Question on Stackoverflow
Solution 1 - PythonJohannes WeissView Answer on Stackoverflow
Solution 2 - PythonDeanView Answer on Stackoverflow
Solution 3 - PythonahawkerView Answer on Stackoverflow
Solution 4 - PythonwordsforthewiseView Answer on Stackoverflow
Solution 5 - Pythonmarr75View Answer on Stackoverflow
Solution 6 - PythonImtiaz Shakil SiddiqueView Answer on Stackoverflow
Solution 7 - PythonDanielView Answer on Stackoverflow
Solution 8 - PythonGramView Answer on Stackoverflow