Skip to content

Commit

Permalink
Return datetime timezoned
Browse files Browse the repository at this point in the history
  • Loading branch information
luiscoms committed Feb 11, 2019
1 parent a9c28e0 commit 8d7aaf1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
28 changes: 22 additions & 6 deletions lib/yaml/constructor.py
Expand Up @@ -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

Expand Down Expand Up @@ -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<year>[0-9][0-9][0-9][0-9])
Expand Down Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions lib3/yaml/constructor.py
Expand Up @@ -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
Expand Down

0 comments on commit 8d7aaf1

Please sign in to comment.