Ouais, en fait, normaliser un temps dont l'offset est explicite ça sert à rien.

This commit is contained in:
Pierre-Elliott Bécue 2015-03-27 14:57:28 +01:00
parent d6f4e7aefd
commit d86bd4bdd6

View file

@ -352,8 +352,7 @@ def datetime_from_generalized_time_format(gtf):
tz = '+0000' tz = '+0000'
the_date = datetime.datetime.strptime(date, "%Y%m%d%H%M%S") the_date = datetime.datetime.strptime(date, "%Y%m%d%H%M%S")
if pytz is not None: if pytz is not None:
tz_dict = build_tz_dict() the_timezone = pytz.FixedOffset(int(tz[0:-2])*60 + int(tz[-2:]))
the_timezone = tz_dict.get(tz, pytz.utc)
the_date = the_timezone.localize(the_date) the_date = the_timezone.localize(the_date)
the_date = the_timezone.normalize(the_date) the_date = the_timezone.normalize(the_date)
return the_date return the_date
@ -364,27 +363,7 @@ def datetime_to_generalized_time_format(datetime_obj):
if datetime_obj.utcoffset() is None: if datetime_obj.utcoffset() is None:
if pytz is not None: if pytz is not None:
datetime_obj = pytz.utc.localize(datetime_obj) datetime_obj = pytz.utc.localize(datetime_obj)
datetime_obj = pytz.utc.normalize(datetime_obj)
else: else:
to_append = "Z" to_append = "Z"
mostly_gtf = datetime.datetime.strftime(datetime_obj, "%Y%m%d%H%M%S%z") mostly_gtf = datetime.datetime.strftime(datetime_obj, "%Y%m%d%H%M%S%z")
return mostly_gtf.replace('+0000', "Z") + to_append return mostly_gtf.replace('+0000', "Z") + to_append
def build_tz_dict():
"""Crappy way to define a dict containing all timezones that
pytz can handle.
It seems there is no way to get a tz object from the offset
with pytz.
"""
tz_dict = {}
for tz in pytz.common_timezones:
mytz = pytz.timezone(tz)
d = datetime.datetime.now(mytz).utcoffset().total_seconds()
hours = int(d)/3600
minutes = int(float(int(d) % 3600)/60)
fmt = "%+03d%02d" % (hours, minutes)
if fmt not in tz_dict:
tz_dict[fmt] = mytz
return tz_dict