Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[expressions] Support date + time = datetime calculations
  • Loading branch information
nyalldawson committed May 22, 2016
1 parent 4ce16e1 commit fdb6488
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -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" );
Expand Down

0 comments on commit fdb6488

Please sign in to comment.