Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse datetime timezone #251

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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