How to get current UK time in Google App Engine

為{幸葍}努か 提交于 2020-01-10 04:05:47

问题


Under python on my machine I can run datetime.now() to get the local time. If I inspect time.daylight flag it is set to 1 because we are currently in July (hence daylight saving).

But if I run datetime.now() on Google App Engine (including the dev server) it doesn't account for daylight saving (british summer time) and returns the wrong time (13:47 instead of 14:47). If I inspect time.daylight in GAE it is set to 0.

How can I get the correct local time? Do I need to change the timezone on my app?


回答1:


Google App Engine's "time zone" is always set to UTC.

You can adjust to your local or desired time zone using a library like pytz.

Check out the following project on Google Code for an App Engine optimized version of pytz.

Google Code - gae-pytz




回答2:


AppEngine is a distributed system, whose servers can be located in different datacenters around the globe (afaik, currently EU and USA). Note that the python code you are running runs on the server not in the browser, so you'll never get the client's time. To get clients' time you'll need to use JavaScript and then send it to server.

GAE servers' timezone is always set to UTC. Since UTC is in summer time one hour off from UK time, you are getting an hour difference.

See this blog series for time handling on GAE: http://www.learningtechnicalstuff.com/2010/01/supporting-timezones-in-google-app.html




回答3:


I've been able to get this working:

import pytz
import datetime

tz = pytz.timezone('Europe/London')

print datetime.datetime.now(tz)

It appears Google App Engine already imports a few modules by default, including pytz and datetime, so perhaps there is no need to explicitly import them.



来源:https://stackoverflow.com/questions/17908994/how-to-get-current-uk-time-in-google-app-engine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!