Skip to content

Commit

Permalink
Add tests for timezone (#363)
Browse files Browse the repository at this point in the history
After #163, this adds some test data to check if the datetime objects
return the correct timezone
  • Loading branch information
perlpunk authored Dec 16, 2019
1 parent 0cdef1d commit 74543d8
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/yaml/constructor.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 74543d8

Please sign in to comment.