Skip to content
Permalink
Browse files

Use make_datetime instead of string based date expressions

  • Loading branch information
nyalldawson committed May 7, 2020
1 parent bcbfc5e commit 4f11d8b23f2a927c9296befe9b3e69710e8a46f9
@@ -153,11 +153,12 @@ void QgsVectorLayerTemporalProperties::setEndField( const QString &field )

QString dateTimeExpressionLiteral( const QDateTime &datetime )
{
// TODO - we should add a dedicated expression function for optimised datetime value creation
// which doesn't rely on string conversion!
// e.g. datetime(2012,5,4,12,15,0)

return QStringLiteral( "to_datetime('%1','yyyy-MM-ddTHH:mm:ss.zzz')" ).arg( datetime.toString( QStringLiteral( "yyyy-MM-ddTHH:mm:ss.zzz" ) ) );
return QStringLiteral( "make_datetime(%1,%2,%3,%4,%5,%6)" ).arg( datetime.date().year() )
.arg( datetime.date().month() )
.arg( datetime.date().day() )
.arg( datetime.time().hour() )
.arg( datetime.time().minute() )
.arg( datetime.time().second() + datetime.time().msec() / 1000.0 );
}

QString QgsVectorLayerTemporalProperties::createFilterString( QgsVectorLayer *, const QgsDateTimeRange &range ) const
@@ -78,13 +78,13 @@ def testSingleFieldMode(self):
self.assertFalse(props.createFilterString(layer, range))

props.setIsActive(True)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" >= to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "start_field" <= to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" >= make_datetime(2019,3,4,11,12,13) AND "start_field" <= make_datetime(2020,5,6,8,9,10)')

range = QgsDateTimeRange(QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)), QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10)), includeBeginning=False)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" > to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "start_field" <= to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" > make_datetime(2019,3,4,11,12,13) AND "start_field" <= make_datetime(2020,5,6,8,9,10)')

range = QgsDateTimeRange(QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)), QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10)), includeEnd=False)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" >= to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "start_field" < to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" >= make_datetime(2019,3,4,11,12,13) AND "start_field" < make_datetime(2020,5,6,8,9,10)')

def testDualFieldMode(self):
layer = QgsVectorLayer("Point?field=fldtxt:string&field=fldint:integer&field=start_field:datetime&field=end_field:datetime", "test", "memory")
@@ -101,13 +101,13 @@ def testDualFieldMode(self):
self.assertFalse(props.createFilterString(layer, range))

props.setIsActive(True)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" >= to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "end_field" <= to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" >= make_datetime(2019,3,4,11,12,13) AND "end_field" <= make_datetime(2020,5,6,8,9,10)')

range = QgsDateTimeRange(QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)), QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10)), includeBeginning=False)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" > to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "end_field" <= to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" > make_datetime(2019,3,4,11,12,13) AND "end_field" <= make_datetime(2020,5,6,8,9,10)')

range = QgsDateTimeRange(QDateTime(QDate(2019, 3, 4), QTime(11, 12, 13)), QDateTime(QDate(2020, 5, 6), QTime(8, 9, 10)), includeEnd=False)
self.assertEqual(props.createFilterString(layer, range), '''"start_field" >= to_datetime('2019-03-04T11:12:13.000','yyyy-MM-ddTHH:mm:ss.zzz') AND "end_field" < to_datetime('2020-05-06T08:09:10.000','yyyy-MM-ddTHH:mm:ss.zzz')''')
self.assertEqual(props.createFilterString(layer, range), '"start_field" >= make_datetime(2019,3,4,11,12,13) AND "end_field" < make_datetime(2020,5,6,8,9,10)')


if __name__ == '__main__':

0 comments on commit 4f11d8b

Please sign in to comment.
You can’t perform that action at this time.