Skip to content

Commit a3f03f0

Browse files
committed
vector file writer: fix saving of datetime values (fixes #14190)
1 parent 9b5c683 commit a3f03f0

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

src/core/qgsvectorfilewriter.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ void QgsVectorFileWriter::init( QString vectorFileName, QString fileEncoding, co
392392
if ( mOgrDriverName == "ESRI Shapefile" )
393393
{
394394
ogrType = OFTString;
395-
ogrWidth = 12;
395+
ogrWidth = 12; // %02d:%02d:%06.3f
396396
}
397397
else
398398
{
@@ -401,7 +401,15 @@ void QgsVectorFileWriter::init( QString vectorFileName, QString fileEncoding, co
401401
break;
402402

403403
case QVariant::DateTime:
404-
ogrType = OFTDateTime;
404+
if ( mOgrDriverName == "ESRI Shapefile" )
405+
{
406+
ogrType = OFTString;
407+
ogrWidth = 24; // "%04d/%02d/%02d %02d:%02d:%06.3f"
408+
}
409+
else
410+
{
411+
ogrType = OFTDateTime;
412+
}
405413
break;
406414

407415
default:
@@ -1794,14 +1802,21 @@ OGRFeatureH QgsVectorFileWriter::createFeature( QgsFeature& feature )
17941802
0, 0, 0, 0 );
17951803
break;
17961804
case QVariant::DateTime:
1797-
OGR_F_SetFieldDateTime( poFeature, ogrField,
1798-
attrValue.toDateTime().date().year(),
1799-
attrValue.toDateTime().date().month(),
1800-
attrValue.toDateTime().date().day(),
1801-
attrValue.toDateTime().time().hour(),
1802-
attrValue.toDateTime().time().minute(),
1803-
attrValue.toDateTime().time().second(),
1804-
0 );
1805+
if ( mOgrDriverName == "ESRI Shapefile" )
1806+
{
1807+
OGR_F_SetFieldString( poFeature, ogrField, mCodec->fromUnicode( attrValue.toDateTime().toString( "yyyy/MM/dd hh:mm:ss.zzz" ) ).data() );
1808+
}
1809+
else
1810+
{
1811+
OGR_F_SetFieldDateTime( poFeature, ogrField,
1812+
attrValue.toDateTime().date().year(),
1813+
attrValue.toDateTime().date().month(),
1814+
attrValue.toDateTime().date().day(),
1815+
attrValue.toDateTime().time().hour(),
1816+
attrValue.toDateTime().time().minute(),
1817+
attrValue.toDateTime().time().second(),
1818+
0 );
1819+
}
18051820
break;
18061821
case QVariant::Time:
18071822
if ( mOgrDriverName == "ESRI Shapefile" )

0 commit comments

Comments
 (0)