在 python 中获取系统本地时区

Get system local timezone in python

看起来很奇怪,但我找不到使用 Python 中的 Pandas/pytz 找到本地时区的简单方法。

我能做到:

>>> 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')

这会给我时区,但这可能不是最好的方法... pytz或pandas中有获取系统时区的命令吗? (最好在 python 2.7 )

我认为使用 pytzpandas 是不可能的,但您始终可以安装 python-dateutil or tzlocal:

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

from tzlocal import get_localzone
local_tz = get_localzone()

time.timezone 应该可以。

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).

除以 3600 将得到以小时为单位的偏移量:

import time

print(time.timezone / 3600.0)

这不需要任何额外的 Python 库。

虽然它不使用 pytz/Pandas,但其他答案也不使用,所以我认为我应该 post 我在 mac/linux 上使用的内容:

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

相对于其他答案的优势:尊重夏令时,不需要安装额外的库。

我发现在很多情况下这是可行的:(自 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 timezone
type(tznow.tzinfo)
<class 'datetime.timezone'>

我觉得这很方便,因为它不依赖于外部包。它似乎只在 Python3 中有效(但在 Python2 中无效)

OS 级别的相当多的语言环境时间相关设置被 time 模块覆盖

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.18
local_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)

另一个技巧是将datetime.now().astimezone()用作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)