How to get the current UK time in Google App Engine

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

But if I run datetime.now() on the Google App Engine (including the Dev server), it does not account for daylight saving time (UK daylight saving time) and returns the wrong time (13:47 instead of 14:47). If I check time.daylight in GAE, it is set to 0 .

How can I get the right local time? Do I need to change the time zone in my application?

+4
source share
3 answers

The time zone of the Google App Engine is always set to UTC.

You can set your local or desired time zone using a library such as pytz.

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

Google Code - gae-pytz

+4
source

AppEngine is a distributed system whose servers can be located in different data centers around the world (afaik, currently the EU and the USA). Please note that the python code you use is not running on the server in the browser, so you will never get client time. To get time for clients, you will need to use JavaScript, and then send it to the server.

The time zone of the GAE server is always set to UTC . Since UTC is in daylight saving time for one hour from UK time, you get an hourly difference.

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

+4
source

I managed to get this working:

 import pytz import datetime tz = pytz.timezone('Europe/London') print datetime.datetime.now(tz) 

It looks like the Google App Engine is already importing a few modules by default, including pytz and datetime , so it may not be necessary to import them explicitly.

+3
source

Source: https://habr.com/ru/post/1493923/


All Articles