From 8d7aaf1d8452782abd3fc877b9569ac7fc58fae3 Mon Sep 17 00:00:00 2001 From: Luis Fernando Gomes Date: Mon, 11 Feb 2019 15:56:39 -0200 Subject: [PATCH] Return datetime timezoned --- lib/yaml/constructor.py | 28 ++++++++++++++++++++++------ lib3/yaml/constructor.py | 10 +++++----- 2 files changed, 27 insertions(+), 11 deletions(-) diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py index 635faac3..fa56b852 100644 --- a/lib/yaml/constructor.py +++ b/lib/yaml/constructor.py @@ -9,6 +9,22 @@ import binascii, re, sys, types + +class timezone(datetime.tzinfo): + def __init__(self, offset): + self.__offset = offset + self.__name = str(offset) + + def utcoffset(self, dt): + return self.__offset + + def tzname(self, dt): + return self.__name + + def dst(self, dt): + return datetime.timedelta(0) + + class ConstructorError(MarkedYAMLError): pass @@ -287,7 +303,7 @@ def construct_yaml_binary(self, node): return str(value).decode('base64') except (binascii.Error, UnicodeEncodeError), exc: raise ConstructorError(None, None, - "failed to decode base64 data: %s" % exc, node.start_mark) + "failed to decode base64 data: %s" % exc, node.start_mark) timestamp_regexp = re.compile( ur'''^(?P[0-9][0-9][0-9][0-9]) @@ -319,17 +335,17 @@ def construct_yaml_timestamp(self, node): while len(fraction) < 6: fraction += '0' fraction = int(fraction) - delta = None + + tzinfo = None if values['tz_sign']: tz_hour = int(values['tz_hour']) tz_minute = int(values['tz_minute'] or 0) delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) if values['tz_sign'] == '-': delta = -delta - data = datetime.datetime(year, month, day, hour, minute, second, fraction) - if delta: - data -= delta - return data + tzinfo = timezone(delta) + + return datetime.datetime(year, month, day, hour, minute, second, fraction, tzinfo=tzinfo) def construct_yaml_omap(self, node): # Note: we do not check for duplicate keys, because it's too diff --git a/lib3/yaml/constructor.py b/lib3/yaml/constructor.py index 981543ae..422990ce 100644 --- a/lib3/yaml/constructor.py +++ b/lib3/yaml/constructor.py @@ -323,17 +323,17 @@ def construct_yaml_timestamp(self, node): while len(fraction) < 6: fraction += '0' fraction = int(fraction) - delta = None + + tzinfo = None if values['tz_sign']: tz_hour = int(values['tz_hour']) tz_minute = int(values['tz_minute'] or 0) delta = datetime.timedelta(hours=tz_hour, minutes=tz_minute) if values['tz_sign'] == '-': delta = -delta - data = datetime.datetime(year, month, day, hour, minute, second, fraction) - if delta: - data -= delta - return data + tzinfo = datetime.timezone(delta) + + return datetime.datetime(year, month, day, hour, minute, second, fraction, tzinfo=tzinfo) def construct_yaml_omap(self, node): # Note: we do not check for duplicate keys, because it's too