def guessTimeZone(self):
"""
Guess the timezone the user is supposed to be in.
If it cant be guessed, a safe default (UTC) is used
"""
timeZone = "UTC" # Default fallback
try:
# Check the local cache first
if "timeZone" in request.current.requestData().keys():
return( request.current.requestData()["timeZone"] )
headers = request.current.get().request.headers
if "X-Appengine-Country" in headers.keys():
country = headers["X-Appengine-Country"]
else: # Maybe local development Server - no way to guess it here
return( timeZone )
tzList = pytz.country_timezones[ country ]
except: # Non-User generated request (deferred call; task queue etc), or no pytz
return( timeZone )
if len( tzList ) == 1: # Fine - the country has exactly one timezone
timeZone = tzList[ 0 ]
elif country.lower()=="us": # Fallback for the US
timeZone = "EST"
elif country.lower() == "de": # For some freaking reason Germany is listed with two timezones
timeZone = "Europe/Berlin"
else: # The user is in a Country which has more than one timezone
# Fixme: Is there any equivalent of EST for australia?
pass
request.current.requestData()["timeZone"] = timeZone #Cache the result
return( timeZone )
评论列表
文章目录