Skip to content

Commit 7343f14

Browse files
committed
datetime type passed to designated Date type to be encoded as date.
same for datetime passed to Time.
1 parent dc275b1 commit 7343f14

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

spyne/protocol/_outbase.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
from spyne.model import ModelBase, XmlAttribute, SimpleModel, Null, \
4545
ByteArray, File, ComplexModelBase, AnyXml, AnyHtml, Unicode, Decimal, \
4646
Double, Integer, Time, DateTime, Uuid, Duration, Boolean, AnyDict, \
47-
AnyUri, PushBase
47+
AnyUri, PushBase, Date
4848

4949
from spyne.const.http import HTTP_400, HTTP_401, HTTP_404, HTTP_405, HTTP_413, \
5050
HTTP_500
@@ -107,6 +107,7 @@ def __init__(self, app=None, mime_type=None, ignore_uncap=False,
107107
Time: self.time_to_bytes,
108108
Uuid: self.uuid_to_bytes,
109109
Null: self.null_to_bytes,
110+
Date: self.date_to_bytes,
110111
Double: self.double_to_bytes,
111112
AnyXml: self.any_xml_to_bytes,
112113
Unicode: self.unicode_to_bytes,
@@ -125,6 +126,7 @@ def __init__(self, app=None, mime_type=None, ignore_uncap=False,
125126
ModelBase: self.model_base_to_unicode,
126127
File: self.file_to_unicode,
127128
Time: self.time_to_bytes,
129+
Date: self.date_to_bytes,
128130
Uuid: self.uuid_to_bytes,
129131
Null: self.null_to_bytes,
130132
Double: self.double_to_bytes,
@@ -359,6 +361,14 @@ def integer_to_bytes(self, cls, value, **_):
359361

360362
def time_to_bytes(self, cls, value, **_):
361363
"""Returns ISO formatted times."""
364+
if isinstance(value, datetime):
365+
value = value.time()
366+
return value.isoformat()
367+
368+
def date_to_bytes(self, cls, value, **_):
369+
"""Returns ISO formatted times."""
370+
if isinstance(value, datetime):
371+
value = value.date()
362372
return value.isoformat()
363373

364374
def datetime_to_bytes(self, cls, val, **_):

spyne/test/protocol/_test_dictdoc.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -637,9 +637,8 @@ def some_call(p):
637637
assert s == d
638638

639639
def test_date(self):
640-
d = date(2010, 9, 8).isoformat()
641640
vdt = datetime(2010, 9, 8)
642-
dt = vdt.isoformat()
641+
d = vdt.date().isoformat()
643642

644643
class SomeService(Service):
645644
@srpc(Date, _returns=Date)
@@ -656,17 +655,17 @@ def some_call_dt():
656655

657656
ctx = _dry_me([SomeService], {"some_call": [d]})
658657
s = self.loads(b''.join(ctx.out_string))
659-
d = {"some_callResponse": {"some_callResult": d}}
658+
rd = {"some_callResponse": {"some_callResult": d}}
660659
print(s)
661-
print(d)
662-
assert s == d
660+
print(rd)
661+
assert s == rd
663662

664663
ctx = _dry_me([SomeService], {"some_call_dt": []})
665664
s = self.loads(b''.join(ctx.out_string))
666-
d = {"some_call_dtResponse": {"some_call_dtResult": d}}
665+
rd = {"some_call_dtResponse": {"some_call_dtResult": d}}
667666
print(s)
668-
print(d)
669-
assert s == d
667+
print(rd)
668+
assert s == rd
670669

671670
def test_datetime(self):
672671
d = datetime(2010, 9, 8, 7, 6, 5).isoformat()

0 commit comments

Comments
 (0)