Skip to content

Commit

Permalink
Shapefile: fix writing an invalid "0000/00/00" date
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault authored and github-actions[bot] committed Nov 28, 2023
1 parent 813cca6 commit 72e2c6d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
25 changes: 25 additions & 0 deletions autotest/ogr/ogr_shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -5819,3 +5819,28 @@ def test_ogr_shape_write_arrow_IF_FID_NOT_PRESERVED_ERROR(tmp_vsimem):
lyr.WriteArrowBatch(
schema, array, ["FID=OGC_FID", "IF_FID_NOT_PRESERVED=ERROR"]
)


###############################################################################
# Test writing an invalid "0000/00/00" date


@gdaltest.enable_exceptions()
def test_ogr_shape_write_date_0000_00_00(tmp_vsimem):

filename = tmp_vsimem / "test_ogr_shape_write_date_0000_00_00.shp"
ds = gdal.GetDriverByName("ESRI Shapefile").Create(
filename, 0, 0, 0, gdal.GDT_Unknown
)
lyr = ds.CreateLayer("test")
lyr.CreateField(ogr.FieldDefn("date", ogr.OFTDate))
f = ogr.Feature(lyr.GetLayerDefn())
f["date"] = "0000/00/00"
lyr.CreateFeature(f)
f = None
ds.Close()

ds = ogr.Open(filename)
lyr = ds.GetLayer(0)
f = lyr.GetNextFeature()
assert f.IsFieldNull("date")
6 changes: 6 additions & 0 deletions ogr/ogrsf_frmts/shape/shape2ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,6 +1757,12 @@ OGRErr SHPWriteOGRFeature(SHPHandle hSHP, DBFHandle hDBF,
CE_Warning, CPLE_NotSupported,
"Year < 0 or > 9999 is not a valid date for shapefile");
}
else if (psField->Date.Year == 0 && psField->Date.Month == 0 &&
psField->Date.Day == 0)
{
DBFWriteNULLAttribute(
hDBF, static_cast<int>(poFeature->GetFID()), iField);
}
else
{
DBFWriteIntegerAttribute(
Expand Down

0 comments on commit 72e2c6d

Please sign in to comment.