Optimisation de GTF <-> datetime en virant sgn.

This commit is contained in:
Pierre-Elliott Bécue 2015-02-23 15:22:36 +01:00
parent fb542de099
commit 9d89a0df5e

View file

@ -349,8 +349,7 @@ def datetime_from_generalized_time_format(gtf):
date, tz = gtf[0:14], gtf[14:] date, tz = gtf[0:14], gtf[14:]
else: else:
date = gtf.replace("Z", '') date = gtf.replace("Z", '')
tz = '0000' tz = '+0000'
tz = tz.replace('+', '')
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() tz_dict = build_tz_dict()
@ -360,10 +359,10 @@ def datetime_from_generalized_time_format(gtf):
def datetime_to_generalized_time_format(datetime_obj): def datetime_to_generalized_time_format(datetime_obj):
"""Transforms a datetime to a GTF""" """Transforms a datetime to a GTF"""
to_append = ""
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)
to_append = ""
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")
@ -383,9 +382,7 @@ def build_tz_dict():
d = datetime.datetime.now(mytz).utcoffset().total_seconds() d = datetime.datetime.now(mytz).utcoffset().total_seconds()
hours = int(d)/3600 hours = int(d)/3600
minutes = int(float(int(d) % 3600)/60) minutes = int(float(int(d) % 3600)/60)
fmt = "%s%02d%02d" % (sgn(hours), abs(hours), minutes) fmt = "%+03d%02d" % (hours, minutes)
if fmt not in tz_dict: if fmt not in tz_dict:
tz_dict[fmt] = mytz tz_dict[fmt] = mytz
return tz_dict return tz_dict
sgn = lambda x: '-' if x < 0 else ''