I have a bunch of input values in this format:
2014-05-14 17:42:18
And I would like to store them in a field in QGIS. I am using the Python API for GDAL/OGR. I notice that QGIS supports a field type "QDate", but it seems that it can only handle dates with no time, as such:
2014-05-14
I'm just wondering if there is a way to get around this or am I stuck storing the timestamp as a string?
I guess the python OGR API has a way to define a datetime field and store data in it (its defined in the OFRFieldType
enum. You have to use the OFTDateTime
(or OFTDate
or OFTTime
) type defined by OGR.
So you can do something like :
date_field = ogr.FieldDefn("date", ogr.OFTDateTime)
your_layer.CreateField(date_field)
then you can set the value for a feature by passing your date as a string:
feature = ogr.Feature(your_layer.GetLayerDefn())
feature.SetField("date", "2014-05-14 17:42:18")
The result may depend on your output data structure (if it defines/supports a date time type) and on the desktop GIS software you are using to display them.
EDIT : But I tested to write a shapefile like this, then open it in QGIS; the date field was properly recognized as a QDate
field, and as you say the time is not showing so I looked in the .dbf
file and the time part doesn't seems to have been written.