Skip to content
Permalink
Browse files

[expressions] Support date + time = datetime calculations

  • Loading branch information
nyalldawson committed May 22, 2016
1 parent 4ce16e1 commit fdb6488b975d67d479b14509f6f9871e3524475b
Showing with 17 additions and 0 deletions.
  1. +15 −0 src/core/qgsexpression.cpp
  2. +2 −0 tests/src/core/testqgsexpression.cpp
@@ -4051,6 +4051,21 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression *parent, const Q
}
return QVariant( computeDateTimeFromInterval( dL, &iL ) );
}
else if (( vL.type() == QVariant::Date && vR.type() == QVariant::Time ) ||
( vR.type() == QVariant::Date && vL.type() == QVariant::Time ) )
{
QDate date = getDateValue( vL.type() == QVariant::Date ? vL : vR, parent );
ENSURE_NO_EVAL_ERROR;
QTime time = getTimeValue( vR.type() == QVariant::Time ? vR : vL, parent );
ENSURE_NO_EVAL_ERROR;
if ( mOp == boMinus || mOp == boDiv || mOp == boMul || mOp == boMod )
{
parent->setEvalErrorString( tr( "Can't preform -, /, *, or % on Date and Time" ) );
return QVariant();
}
QDateTime dt = QDateTime( date, time );
return QVariant( dt );
}
else
{
// general floating point arithmetic
@@ -832,6 +832,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "age time" ) << "second(age(to_time('08:30:22'),to_time('07:12:10')))" << false << QVariant( 4692.0 );
QTest::newRow( "age date" ) << "day(age(to_date('2004-03-22'),to_date('2004-03-12')))" << false << QVariant( 10.0 );
QTest::newRow( "age datetime" ) << "hour(age(to_datetime('2004-03-22 08:30:22'),to_datetime('2004-03-12 07:30:22')))" << false << QVariant( 241.0 );
QTest::newRow( "date + time" ) << "to_date('2013-03-04') + to_time('13:14:15')" << false << QVariant( QDateTime( QDate( 2013, 3, 4 ), QTime( 13, 14, 15 ) ) );
QTest::newRow( "time + date" ) << "to_time('13:14:15') + to_date('2013-03-04')" << false << QVariant( QDateTime( QDate( 2013, 3, 4 ), QTime( 13, 14, 15 ) ) );

// Color functions
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "254,190,116,255" );

0 comments on commit fdb6488

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