Skip to content

Commit 4fcdcdb

Browse files
committed
Add tests for timezone (#363)
After #163, this adds some test data to check if the datetime objects return the correct timezone
1 parent 96d65f3 commit 4fcdcdb

File tree

5 files changed

+41
-13
lines changed

5 files changed

+41
-13
lines changed

lib/yaml/constructor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class timezone(datetime.tzinfo):
2323
def __init__(self, offset):
2424
self._offset = offset
2525
seconds = abs(offset).total_seconds()
26-
self._name = '%s%02d:%02d' % (
26+
self._name = 'UTC%s%02d:%02d' % (
2727
'-' if offset.days < 0 else '+',
2828
seconds // 3600,
2929
seconds % 3600 // 60

tests/data/timestamp-bugs.code

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[
2-
datetime.datetime(2001, 12, 15, 3, 29, 43, 100000),
3-
datetime.datetime(2001, 12, 14, 16, 29, 43, 100000),
4-
datetime.datetime(2001, 12, 14, 21, 59, 43, 1010),
5-
datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(60, "+1")),
6-
datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(-90, "-1:30")),
7-
datetime.datetime(2005, 7, 8, 17, 35, 4, 517600),
2+
[datetime.datetime(2001, 12, 15, 3, 29, 43, 100000), 'UTC-05:30'],
3+
[datetime.datetime(2001, 12, 14, 16, 29, 43, 100000), 'UTC+05:30'],
4+
[datetime.datetime(2001, 12, 14, 21, 59, 43, 1010), None],
5+
[datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(60, "+1")), 'UTC+01:00'],
6+
[datetime.datetime(2001, 12, 14, 21, 59, 43, 0, FixedOffset(-90, "-1:30")), 'UTC-01:30'],
7+
[datetime.datetime(2005, 7, 8, 17, 35, 4, 517600), None],
88
]

tests/data/timestamp-bugs.data

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
- 2001-12-14 21:59:43.10 -5:30
2-
- 2001-12-14 21:59:43.10 +5:30
3-
- 2001-12-14 21:59:43.00101
4-
- 2001-12-14 21:59:43+1
5-
- 2001-12-14 21:59:43-1:30
6-
- 2005-07-08 17:35:04.517600
1+
- !MyTime
2+
- 2001-12-14 21:59:43.10 -5:30
3+
- !MyTime
4+
- 2001-12-14 21:59:43.10 +5:30
5+
- !MyTime
6+
- 2001-12-14 21:59:43.00101
7+
- !MyTime
8+
- 2001-12-14 21:59:43+1
9+
- !MyTime
10+
- 2001-12-14 21:59:43-1:30
11+
- !MyTime
12+
- 2005-07-08 17:35:04.517600

tests/lib/test_constructor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ def construct1(constructor, node):
4141
def represent1(representer, native):
4242
return representer.represent_mapping("!tag1", native.__dict__)
4343

44+
def my_time_constructor(constructor, node):
45+
seq = constructor.construct_sequence(node)
46+
dt = seq[0]
47+
tz = None
48+
try:
49+
tz = dt.tzinfo.tzname(dt)
50+
except:
51+
pass
52+
return [dt, tz]
53+
4454
yaml.add_constructor("!tag1", construct1, Loader=MyLoader)
55+
yaml.add_constructor("!MyTime", my_time_constructor, Loader=MyLoader)
4556
yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper)
4657

4758
class MyTestClass2(MyTestClass1, yaml.YAMLObject):

tests/lib3/test_constructor.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,18 @@ def construct1(constructor, node):
3838
def represent1(representer, native):
3939
return representer.represent_mapping("!tag1", native.__dict__)
4040

41+
def my_time_constructor(constructor, node):
42+
seq = constructor.construct_sequence(node)
43+
dt = seq[0]
44+
tz = None
45+
try:
46+
tz = dt.tzinfo.tzname(dt)
47+
except:
48+
pass
49+
return [dt, tz]
50+
4151
yaml.add_constructor("!tag1", construct1, Loader=MyLoader)
52+
yaml.add_constructor("!MyTime", my_time_constructor, Loader=MyLoader)
4253
yaml.add_representer(MyTestClass1, represent1, Dumper=MyDumper)
4354

4455
class MyTestClass2(MyTestClass1, yaml.YAMLObject):

0 commit comments

Comments
 (0)