On my Google App Engine application, i'm storing an auto-updated date/time in my model like that :
class MyModel(db.Model):
date = db.DateTimeProperty(auto_now_add=True)
But, that date/time is the local time on server, according to it's time zone.
So, when I would like to display it on my web page, how may I format it according the client time zone ?
Also, how may I know on which time zone that google server is ?
With respect to the second part of your question:
Python time() returns UTC regardless of what time zone the server is in. timezone() and tzname() will give you, respectively, the offset to local time on the server and the name of the timezone and the DST timezone as a tuple. GAE uses Python 2.5.x as of the time of this posting; see the documentation for Python time functions here.
For the first part of the question:
You can either format the date with code on the server, or with code on the client.
If you format on the server, you can
If you format on the client, you'll need to write a few lines of JavaScript. The procedure is something like "make a date from UTC using Date(utctime), then stuff it into the document." By default, JavaScript dates display as local time regardless of how they were initialized - awesome!
I recommend formatting on the client, because what's a webpage like without a bit of JavaScript? It's 2009! Marcelo has some examples.
Ok, so thanks to Thomas L Holaday, I have to sent that UTC date to the client, for example using JSON :
json = '{"serverUTCDate":new Date("%s")}' % date.ctime()
And then, on the client side, add/remove the number of seconds according to the user time zone like that :
var localDate = serverUTCDate.getTime() - (new Date()).getTimezoneOffset()*60000;
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