logging module for python reports incorrect timezone under cygwin

回眸只為那壹抹淺笑 提交于 2020-01-13 09:44:30

问题


I am running python script that uses logging module under cygwin on Windows 7. The date command reports correct time:

$ date
Tue, Aug 14, 2012  2:47:49 PM

However, the python script is five hours off:

2012-08-14 19:39:06,438: Done!

I don't do anything fancy when I configure logging for the script:

logging.basicConfig(format='%(asctime)-15s: %(message)s', level=logging.DEBUG)

Can someone tell me what is going on and how I can fix it?


回答1:


You need to unset the environment "TZ" in your python script prior to any importing the date/time modules. It is set by cygwin but not understood by Windows:

if os.getenv("TZ"):
    os.unsetenv("TZ")



回答2:


It seems that if TZ environment variable is set, Python in Cygwin will operate in GMT (UTC) timezone. This is true even when TZ is set to the same timezone as the Windows box!

As a workaround you can call unset TZ in the bash shell prior to calling Python and then Python will use the Windows timezone. For me, deleting the ENV variable inside Python did not work and it needed to happen prior to starting Python (even if deleting os.environ['TZ'] was as early as possible in the Python process before importing any time-related modules, possibly because one has to import os and maybe that triggers the Cygwin/Python "bug" whereby the timezone becomes UTC?).

One can automate the fix by adding unset TZ to the .bash_profile file (in /home/<user> which is a subdir of the location where Cygwin is installed).



来源:https://stackoverflow.com/questions/11958870/logging-module-for-python-reports-incorrect-timezone-under-cygwin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!