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?
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")
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With