Skip to content

Commit

Permalink
Fix default encoding for dates without explicit flag in c module
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekenre committed Oct 28, 2019
1 parent 59f3946 commit 9927bb5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cbor2/encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ def encode_datetime(self, value):
self.encode_semantic(CBORTag(0, datestring))

def encode_date(self, value):
value = datetime.combine(value, time()).replace(tzinfo=timezone.utc)
value = datetime.combine(value, time()).replace(tzinfo=self._timezone)
self.encode_datetime(value)

def encode_decimal(self, value):
Expand Down
4 changes: 1 addition & 3 deletions source/encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ CBOREncoder_init(CBOREncoderObject *self, PyObject *args, PyObject *kwargs)
PyObject *tmp, *fp = NULL, *default_handler = NULL, *tz = NULL,
*date_as_datetime = NULL;

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|pOpOBO", keywords,
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|pOpOpp", keywords,
&fp, &self->timestamp_format, &tz, &self->value_sharing,
&default_handler, &self->enc_style, &date_as_datetime))
return -1;
Expand Down Expand Up @@ -1805,8 +1805,6 @@ encode(CBOREncoderObject *self, PyObject *value)
return CBOREncoder_encode_map(self, value);
else if (PyDateTime_CheckExact(value))
return CBOREncoder_encode_datetime(self, value);
else if (PyDate_CheckExact(value))
return CBOREncoder_encode_date(self, value);
else if (PyAnySet_CheckExact(value))
return CBOREncoder_encode_set(self, value);
// fall-thru
Expand Down
8 changes: 5 additions & 3 deletions tests/test_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,10 +234,12 @@ def test_datetime(impl, value, as_timestamp, expected):
expected = unhexlify(expected)
assert impl.dumps(value, datetime_as_timestamp=as_timestamp, timezone=timezone.utc) == expected


def test_date(impl):
@pytest.mark.parametrize('tz', [None, timezone.utc], ids=['no timezone', 'utc'])
def test_date_fails(impl, tz):
encoder = impl.CBOREncoder(BytesIO(b''), timezone=tz, date_as_datetime=False)
assert date not in encoder._encoders
with pytest.raises(impl.CBOREncodeError):
impl.dumps(date(2013, 3, 21))
encoder.encode(date(2013, 3, 21))


def test_date_as_datetime(impl):
Expand Down

0 comments on commit 9927bb5

Please sign in to comment.