在python中获取系统本地时区 [英] Get system local timezone in python
本文介绍了在python中获取系统本地时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
看起来很奇怪,但我找不到在 Python 中使用 Pandas/pytz 找到本地时区的简单方法.
我能做到:
<预><代码>>>>pd.Timestamp('now', tz='utc').isoformat()出 [47]: '2016-01-28T09:36:35.604000+00:00'>>>pd.Timestamp('now').isoformat()出 [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 中)
解决方案
我不认为使用 pytz
或 pandas
是可能的,但你总是可以安装 python-dateutil 或 tzlocal:
from dateutil.tz import tzlocaldatetime.now(tzlocal())
或
from tzlocal import get_localzonelocal_tz = get_localzone()
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 )
解决方案
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()
这篇关于在python中获取系统本地时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文