7

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?

0x4B1D
  • 923
  • 1
  • 9
  • 19
  • can you try adding `datefmt = '%Y-%m-%d %H:%M:%s'` to the `logging.basicConfig` call? – mgilson Aug 14 '12 at 19:02
  • I'm using Cygwin 1.7.16 and it works correctly. Date and the logging module both return the timestamp. – AlG Aug 14 '12 at 19:09
  • Looks like it's using UTC? Maybe you need to set the `TZ` environment variable. – Mark Ransom Aug 14 '12 at 19:11
  • Nowadays I tend to use native applications instead of Cygwin. Take a look at [Gow](https://github.com/bmatzelle/gow/wiki/) and [ConEmu](https://code.google.com/p/conemu-maximus5/). – Paulo Scardine Aug 14 '12 at 19:14
  • Thank you, I will try suggestions in the comments, and report back tomorrow. @Paulo I would prefer to solve the problem under cygwin, before looking at the alternatives. – 0x4B1D Aug 14 '12 at 22:02
  • @mgilson I get an exception with this format: ValueError: Invalid format string. – 0x4B1D Aug 15 '12 at 15:56
  • @MarkRansom $ echo $TZ -- seems to return a correct value. – 0x4B1D Aug 15 '12 at 15:58

2 Answers2

9

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")
  • 1
    Interesting, @Yun-Chen Sung. This does NOT work when I do it from the script. If I do unset and export TZ from bash before I run it, the correct time is displayed. Do you know why this code does not work in Cygwin? – 0x4B1D Aug 16 '12 at 18:14
  • 1
    @Nikita: If I do this before I import `time` `time.timezone` gives me the windows timezone. If I do this after I import `time` I get the wrong timezone. I assume `time` checks `$TZ` once on import. – Peter Graham Aug 23 '12 at 22:36
  • 1
    This solution did not work for me (unsetting TZ from inside Python was "too late" and I got the incorrect GMT behavior) but I found that running "unset TZ" in the bash shell PRIOR to calling Python worked. One way to automate the fix is to stick "unset TZ" in your .bash_profile. I also recommend dbl-checking at runtime that datetime.datetime.now() != datetime.datetime.utcnow() by an appropriate (hours) margin because this behavior seems to depend on the exact version of Cygwin. – Joseph Hastings Nov 06 '14 at 23:57
6

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

Joseph Hastings
  • 561
  • 2
  • 7
  • 14