accept an iso string date
MODIFY
fastavro/_write.pyx
fastavro/_write.pyx
@@ -115,6 +115,8 @@ cpdef long64 prepare_timestamp_micros(object data, schema):
cpdef prepare_date(object data, schema):
if isinstance(data, datetime.date):
return data.toordinal() - const.DAYS_SHIFT
+ elif isinstance(data, str):
+ return datetime.datetime.strptime(data, "%Y-%m-%d").toordinal() - const.DAYS_SHIFT
else:
return data

MODIFY
fastavro/_write_py.py
fastavro/_write_py.py
@@ -71,6 +71,9 @@ def prepare_date(data, schema):
"""Converts datetime.date to int timestamp"""
if isinstance(data, datetime.date):
return data.toordinal() - DAYS_SHIFT
+ elif isinstance(data, str):
+ days = datetime.datetime.strptime(data, "%Y-%m-%d").toordinal()
+ return days - DAYS_SHIFT
else:
return data

MODIFY
tests/test_logical_types.py
tests/test_logical_types.py
@@ -353,3 +353,16 @@ def test_default_scale_value():
binary = serialize(schema, data1)
data2 = deserialize(schema, binary)
assert (data1 == data2)
+
+
+def test_date_as_string():
+ schema = {
+ "name": "test_date_as_string",
+ "type": "int",
+ "logicalType": "date",
+ }
+
+ data1 = "2019-05-06"
+ binary = serialize(schema, data1)
+ data2 = deserialize(schema, binary)
+ assert (datetime.date(2019, 5, 6) == data2)

accept an iso string date