Add tests for timezone (#363)
After #163, this adds some test data to check if the datetime objects
return the correct timezone
diff --git a/lib/yaml/constructor.py b/lib/yaml/constructor.py
index 568613b..8ce3972 100644
--- a/lib/yaml/constructor.py
+++ b/lib/yaml/constructor.py
@@ -23,7 +23,7 @@
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 b1d6e9c..e2c84cc 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 721d290..b243d0d 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 9e6c856..a22fd18 100644
--- a/tests/lib/test_constructor.py
+++ b/tests/lib/test_constructor.py
@@ -41,7 +41,18 @@
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 3d69649..877982d 100644
--- a/tests/lib3/test_constructor.py
+++ b/tests/lib3/test_constructor.py
@@ -38,7 +38,18 @@
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):