Skip to content

Create timezone-aware datetimes when parsed as such #163

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

Merged
merged 8 commits into from
Dec 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions lib/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,29 @@
class ConstructorError(MarkedYAMLError):
pass


class timezone(datetime.tzinfo):
def __init__(self, offset):
self._offset = offset
seconds = abs(offset).total_seconds()
self._name = '%s%02d:%02d' % (
'-' if offset.days < 0 else '+',
seconds // 3600,
seconds % 3600 // 60
)

def tzname(self, dt=None):
return self._name

def utcoffset(self, dt=None):
return self._offset

def dst(self, dt=None):
return datetime.timedelta(0)

__repr__ = __str__ = tzname


class BaseConstructor(object):

yaml_constructors = {}
Expand Down Expand Up @@ -293,7 +316,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 All @@ -320,22 +343,23 @@ def construct_yaml_timestamp(self, node):
minute = int(values['minute'])
second = int(values['second'])
fraction = 0
tzinfo = None
if values['fraction']:
fraction = values['fraction'][:6]
while len(fraction) < 6:
fraction += '0'
fraction = int(fraction)
delta = 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)
elif values['tz']:
tzinfo = timezone(datetime.timedelta(0))
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
11 changes: 6 additions & 5 deletions lib3/yaml/constructor.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,22 +324,23 @@ def construct_yaml_timestamp(self, node):
minute = int(values['minute'])
second = int(values['second'])
fraction = 0
tzinfo = None
if values['fraction']:
fraction = values['fraction'][:6]
while len(fraction) < 6:
fraction += '0'
fraction = int(fraction)
delta = 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)
elif values['tz']:
tzinfo = datetime.timezone.utc
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