From f53208125febb65ac1f4d3695fbe6a15093bb73c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tina=20M=C3=BCller?= Date: Sun, 15 Dec 2019 17:51:13 +0100 Subject: [PATCH] Add tests for timezone After #163, this adds some test data to check if the datetime objects return the correct timezone --- lib/yaml/constructor.py | 2 +- tests/data/timestamp-bugs.code | 12 ++++++------ tests/data/timestamp-bugs.data | 18 ++++++++++++------ tests/lib/test_constructor.py | 11 +++++++++++ tests/lib3/test_constructor.py | 11 +++++++++++ 5 files changed, 41 insertions(+), 13 deletions(-) diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py index 568613b7..8ce39722 100644 --- a/lib/yaml/constructor.py +++ b/lib/yaml/constructor.py @@ -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 diff --git a/tests/data/timestamp-bugs.code b/tests/data/timestamp-bugs.code index b1d6e9c4..e2c84cc3 100644 --- a/tests/data/timestamp-bugs.code +++ b/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], ] diff --git a/tests/data/timestamp-bugs.data b/tests/data/timestamp-bugs.data index 721d2908..b243d0d2 100644 --- a/tests/data/timestamp-bugs.data +++ b/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 diff --git a/tests/lib/test_constructor.py b/tests/lib/test_constructor.py index 9e6c8564..a22fd189 100644 --- a/tests/lib/test_constructor.py +++ b/tests/lib/test_constructor.py @@ -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): diff --git a/tests/lib3/test_constructor.py b/tests/lib3/test_constructor.py index 3d696498..877982db 100644 --- a/tests/lib3/test_constructor.py +++ b/tests/lib3/test_constructor.py @@ -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):