1

When I try to use time.localtime() in Python, I get a different time than the one returned by the "date" command in the shell. I am running this on a Windows box in Cygwin (using MinTTY)

$ which date
/usr/bin/date
$ date --version
date (GNU coreutils) 8.24
Packaged by Cygwin (8.24-3)
Copyright (C) 2015 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by David MacKenzie.
$ which python
/cygdrive/c/Python27/python
$ python --version
Python 2.7.12
$ date +"%Y-%m-%d %H:%M:%S"
2016-11-19 23:06:42
$ python -i
Python 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
2016-11-20 07:06:46

Anybody know how to fix this discrepancy?

ag415
  • 392
  • 2
  • 15
  • It looks like a time zone problem. – Laurent LAPORTE Nov 20 '16 at 07:28
  • Which of them is the right time? What timezone are you in / is set on your computer? – Klaus D. Nov 20 '16 at 07:28
  • @KlausD. According to the title of the OP, Python is wrong. – Laurent LAPORTE Nov 20 '16 at 07:32
  • @KlausD. date command returns correct time. time.localtime() in python returns wrong time – ag415 Nov 20 '16 at 07:35
  • @LaurentLAPORTE If the output matches the time of posting, I guess Python returns GMT. But ag415 should clarify that. – Klaus D. Nov 20 '16 at 07:35
  • @LaurentLAPORTE i tried googling this problem and saw some people mentioning issues with the time zone, but couldnt find a clear solution to the problem. time.timezone in python is giving me "0" – ag415 Nov 20 '16 at 07:37
  • You're running Windows Python, not Cygwin Python, so are you asking why Cygwin is using a different timezone from the host Windows system? – Eryk Sun Nov 20 '16 at 08:16
  • @eryksun I see now. When I run it straight from cmd.exe it works correctly. I guess it was Cygwin messing with the timezone. Is there a way to remove this discrepancy? – ag415 Nov 20 '16 at 08:28
  • 1
    It may be due to the [`TZ` environment variable](http://stackoverflow.com/a/11655109/205580) inherited from the Cygwin environment. Try clearing it in Python, e.g. `del os.environ['TZ']`. Also check if the problem exists if you instead run `cygstart python`. – Eryk Sun Nov 20 '16 at 08:45
  • @eryksun deleting the 'TZ' environment variable prior to importing the time module fixes the issue when running an interactive session via cygstart python. However this solution does not work when executing the python script directly from the cygwin terminal using "./scriptname". Interestingly enough, if i unset TZ in the shell prior to running the python script, it works, but its far from an elegant fix. Ideally, I'd like to be able to unset the variable inside the script and then restore it after it exits so no other programs that might depend on it break. – ag415 Nov 20 '16 at 12:56
  • actually nevermind, i got it working. Apparently the other modules i was importing prior to unsetting TZ were triggering the bug. I moved the statement all the way to the top of the script just after importing OS module and that fixes the issue. Thanks for the help @eryksun – ag415 Nov 20 '16 at 13:19

0 Answers0