Skip to content

Commit

Permalink
Rename to 'DateTimeFromEpoch' and add 2 extra tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rduivenvoorde committed Nov 1, 2019
1 parent 2ce7217 commit f67fe84
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions resources/function_help/json/from_epoch
@@ -1,7 +1,7 @@
{
"name": "from_epoch",
"name": "datetime_from_epoch",
"type": "function",
"description": "Returns a datetime whose date and time are the number of milliseconds, msecs, that have passed since 1970-01-01T00:00:00.000, Coordinated Universal Time (Qt.UTC), and converted to Qt.LocalTime.",
"arguments": [ {"arg":"int","description":"number (milliseconds)"} ],
"examples": [ { "expression":"from epoch(1483225200000)", "returns":"2017-01-01T00:00:00"} ]
"examples": [ { "expression":"datetime_from_epoch(1483225200000)", "returns":"2017-01-01T00:00:00"} ]
}
4 changes: 2 additions & 2 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -1969,7 +1969,7 @@ static QVariant fcnEpoch( const QVariantList &values, const QgsExpressionContext
}
}

static QVariant fcnFromEpoch( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
static QVariant fcnDateTimeFromEpoch( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
long long millisecs_since_epoch = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
// no sense to check for strange values, as Qt behaviour is undefined anyway (see docs)
Expand Down Expand Up @@ -5278,7 +5278,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "minute" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "datetime" ) ), fcnMinute, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "second" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "datetime" ) ), fcnSeconds, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "epoch" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "date" ) ), fcnEpoch, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "from_epoch" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "long" ) ), fcnFromEpoch, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "datetime_from_epoch" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "long" ) ), fcnDateTimeFromEpoch, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "day_of_week" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "date" ) ), fcnDayOfWeek, QStringLiteral( "Date and Time" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "lower" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ), fcnLower, QStringLiteral( "String" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "upper" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "string" ) ), fcnUpper, QStringLiteral( "String" ) )
Expand Down
4 changes: 3 additions & 1 deletion tests/src/core/testqgsexpression.cpp
Expand Up @@ -1284,7 +1284,9 @@ class TestQgsExpression: public QObject
QTest::newRow( "time - time" ) << "to_time('08:30:00') - to_time('05:15:00')" << false << QVariant( QgsInterval( 3 * 60 * 60 + 15 * 60 ) );
QTest::newRow( "epoch" ) << "epoch(to_datetime('2017-01-01T00:00:01+00:00'))" << false << QVariant( 1483228801000LL );
QTest::newRow( "epoch invalid date" ) << "epoch('invalid')" << true << QVariant();
QTest::newRow( "from_epoch" ) << "from_epoch(1483228801000)" << false << QVariant( QDateTime( QDate( 2017, 1, 1 ), QTime( 0, 0, 1 ), Qt::UTC ) );
QTest::newRow( "datetime_from_epoch" ) << "datetime_from_epoch(1483228801000)" << false << QVariant( QDateTime( QDate( 2017, 1, 1 ), QTime( 0, 0, 1 ), Qt::UTC ) );
QTest::newRow( "datetime_from_epoch_null" ) << "datetime_from_epoch(NULL)" << true << QVariant();
QTest::newRow( "datetime_from_epoch_string" ) << "datetime_from_epoch('1483228801000')" << true << QVariant();
QTest::newRow( "date from format" ) << "to_date('June 29, 2019','MMMM d, yyyy')" << false << QVariant( QDate( 2019, 6, 29 ) );
QTest::newRow( "date from format, wrong string" ) << "to_date('wrong.string.here','yyyy.MM.dd')" << true << QVariant();
QTest::newRow( "date from format, wrong format" ) << "to_date('2019-01-01','wrong')" << true << QVariant();
Expand Down

0 comments on commit f67fe84

Please sign in to comment.