boatcoder
boatcoder

Reputation: 18107

Why are the timestamps incorrect when debugging App Engine Python code

Under my debugger:

logging.info("TZ = %s -- It is now: %s", os.environ['TZ'], time.ctime())
TZ = UTC -- It is now: Mon Oct 17 12:10:44 2011

Under App Engine Launcher:

logging.info("TZ = %s -- It is now: %s", os.environ['TZ'], time.ctime())
TZ = UTC -- It is now: Mon Oct 17 17:09:24 2011

So what do I have setup wrong? This affects not just time.ctime(), but all the data dropped into the debug database. I'd like the debugger to run in the same "timeframe" as the app engine launcher because of timestamps in the database, and the debugger is slower than the launcher, so I don't want to use it all the time.

Upvotes: 5

Views: 395

Answers (2)

John Ehresman
John Ehresman

Reputation: 106

The issue is that the C runtime functions on Windows with Python 2.5 seem to cache the value of the TZ environment variable the first time either time or ctime is called. The following will display the time in local time and not in UTC:

import time, os
time.time()
os.environ['TZ'] = 'UTC'
print time.ctime()

Wing's debugger calls time() before running any AppEngine code so the initial value of TZ was captured. The workaround is to set TZ=UTC in the environment variables via the project properties dialog.

Upvotes: 5

Cody Hess
Cody Hess

Reputation: 1796

Now that I understand your problem better (you want your debugger to run on UTC, like GAE's default), this gets exciting and tricky. logging.Formatter.formatTime defaults to formatting dates according to time.localtime(). To change this, set the converter attribute to time.gmtime(). This is barely documented, but I found a blog about Logging with UTC Timestamps from Python that may be what you're looking for.

Tell me if this is not helpful; it may be that I still misunderstand what you're asking.


Old Answer: ctime will always convert to local time. The local time on the server is different from the local time for your home computer's debugger. You can set os os.environ time zone variable to whatever you want; time.ctime() won't care.

Try formatting time.gmtime() and you'll be using Greenwich Mean Time for both.

gmt = time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())
logging.info("It is now: %s", gmt)
-- It is now: Mon 17 Oct 2011 19:11:10

Upvotes: 0

Related Questions