def set_fuso_horaro(self, novo_valor):
if novo_valor in pytz.country_timezones['br']:
self._fuso_horario = pytz.timezone(novo_valor)
#
# Nos valores abaixo, não entendi ainda até agora, mas para o resultado
# correto é preciso usar GMT+ (mais), não (menos) como seria de se
# esperar...
#
elif novo_valor == '-04:00' or novo_valor == '-0400':
self._fuso_horario = pytz.timezone('Etc/GMT+4')
elif novo_valor == '-03:00' or novo_valor == '-0300':
self._fuso_horario = pytz.timezone('Etc/GMT+3')
elif novo_valor == '-02:00' or novo_valor == '-0200':
self._fuso_horario = pytz.timezone('Etc/GMT+2')
elif novo_valor == '-01:00' or novo_valor == '-0100':
self._fuso_horario = pytz.timezone('Etc/GMT+1')
python类country_timezones()的实例源码
def get_local_datetime(self):
# convert global to local date (first add global timezone info, then convert to local)
local_datetime = self.model.curr_global_date
local_datetime = local_datetime.astimezone(timezone(country_timezones(self.country)[0]))
return local_datetime
def get_time_zone(country_code):
"""
Return time zone for country.
"""
try:
return country_timezones[country_code][0]
except KeyError:
return None
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 )