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

Add tests for timezone #363

Merged
merged 1 commit into from Dec 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/yaml/constructor.py
Expand Up @@ -23,7 +23,7 @@ class timezone(datetime.tzinfo):
def __init__(self, offset):
self._offset = offset
seconds = abs(offset).total_seconds()
self._name = '%s%02d:%02d' % (
self._name = 'UTC%s%02d:%02d' % (
'-' if offset.days < 0 else '+',
seconds // 3600,
seconds % 3600 // 60
Expand Down
12 changes: 6 additions & 6 deletions tests/data/timestamp-bugs.code
@@ -1,8 +1,8 @@
[
datetime.datetime(2001, 12, 15, 3, 29, 43, 100000),
datetime.datetime(2001, 12, 14, 16, 29, 43, 100000),
datetime.datetime(2001, 12, 14, 21, 59, 43, 1010),
datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(60, "+1")),
datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(-90, "-1:30")),
datetime.datetime(2005, 7, 8, 17, 35, 4, 517600),
[datetime.datetime(2001, 12, 15, 3, 29, 43, 100000), 'UTC-05:30'],
[datetime.datetime(2001, 12, 14, 16, 29, 43, 100000), 'UTC+05:30'],
[datetime.datetime(2001, 12, 14, 21, 59, 43, 1010), None],
[datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(60, "+1")), 'UTC+01:00'],
[datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(-90, "-1:30")), 'UTC-01:30'],
[datetime.datetime(2005, 7, 8, 17, 35, 4, 517600), None],
]
18 changes: 12 additions & 6 deletions tests/data/timestamp-bugs.data
@@ -1,6 +1,12 @@
- 2001-12-14 21:59:43.10 -5:30
- 2001-12-14 21:59:43.10 +5:30
- 2001-12-14 21:59:43.00101
- 2001-12-14 21:59:43+1
- 2001-12-14 21:59:43-1:30
- 2005-07-08 17:35:04.517600
- !MyTime
- 2001-12-14 21:59:43.10 -5:30
- !MyTime
- 2001-12-14 21:59:43.10 +5:30
- !MyTime
- 2001-12-14 21:59:43.00101
- !MyTime
- 2001-12-14 21:59:43+1
- !MyTime
- 2001-12-14 21:59:43-1:30
- !MyTime
- 2005-07-08 17:35:04.517600
11 changes: 11 additions & 0 deletions tests/lib/test_constructor.py
Expand Up @@ -41,7 +41,18 @@ def construct1(constructor, node):
def represent1(representer, native):
return representer.represent_mapping("!tag1", native.__dict__)

def my_time_constructor(constructor, node):
seq = constructor.construct_sequence(node)
dt = seq[0]
tz = None
try:
tz = dt.tzinfo.tzname(dt)
except:
pass
return [dt, tz]

yaml.add_constructor("!tag1", construct1, Loader=MyLoader)
yaml.add_constructor("!MyTime", my_time_constructor, Loader=MyLoader)
yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper)

class MyTestClass2(MyTestClass1, yaml.YAMLObject):
Expand Down
11 changes: 11 additions & 0 deletions tests/lib3/test_constructor.py
Expand Up @@ -38,7 +38,18 @@ def construct1(constructor, node):
def represent1(representer, native):
return representer.represent_mapping("!tag1", native.__dict__)

def my_time_constructor(constructor, node):
seq = constructor.construct_sequence(node)
dt = seq[0]
tz = None
try:
tz = dt.tzinfo.tzname(dt)
except:
pass
return [dt, tz]

yaml.add_constructor("!tag1", construct1, Loader=MyLoader)
yaml.add_constructor("!MyTime", my_time_constructor, Loader=MyLoader)
yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper)

class MyTestClass2(MyTestClass1, yaml.YAMLObject):
Expand Down