Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging module for python reports incorrect timezone under cygwin

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?

like image 837
0x4B1D Avatar asked Aug 14 '12 18:08

0x4B1D


2 Answers

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")
like image 89
Yun-Chen Sung Avatar answered Oct 02 '22 23:10

Yun-Chen Sung


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

like image 22
Joseph Hastings Avatar answered Oct 02 '22 23:10

Joseph Hastings