Skip to content

Commit

Permalink
[FEATURE] epoch() expression function (#4151)
Browse files Browse the repository at this point in the history
This commit adds an epoch() function to the expression engine which
returns the interval in milliseconds between the unix epoch and a
given date value.
  • Loading branch information
nirvn committed Feb 22, 2017
1 parent e1ede70 commit 07cbfdd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
7 changes: 7 additions & 0 deletions resources/function_help/json/epoch
@@ -0,0 +1,7 @@
{
"name": "epoch",
"type": "function",
"description": "Return the interval in milliseconds between the unix epoch and a given date value.",
"arguments": [ {"arg":"date","description":"a date or datetime value"} ],
"examples": [ { "expression":"epoch(to_date('2017-01-01'))", "returns":"1483203600000"} ]
}
13 changes: 13 additions & 0 deletions src/core/qgsexpression.cpp
Expand Up @@ -1721,6 +1721,18 @@ static QVariant fcnSeconds( const QVariantList& values, const QgsExpressionConte
}
}

static QVariant fcnEpoch( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
{
QDateTime dt = getDateTimeValue( values.at( 0 ), parent );
if ( dt.isValid() )
{
return QVariant( dt.toMSecsSinceEpoch() );
}
else
{
return QVariant();
}
}

#define ENSURE_GEOM_TYPE(f, g, geomtype) \
if ( !(f).hasGeometry() ) \
Expand Down Expand Up @@ -3903,6 +3915,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
<< new StaticFunction( QStringLiteral( "hour" ), 1, fcnHour, QStringLiteral( "Date and Time" ) )
<< new StaticFunction( QStringLiteral( "minute" ), 1, fcnMinute, QStringLiteral( "Date and Time" ) )
<< new StaticFunction( QStringLiteral( "second" ), 1, fcnSeconds, QStringLiteral( "Date and Time" ) )
<< new StaticFunction( QStringLiteral( "epoch" ), ParameterList() << Parameter( QStringLiteral( "date" ) ), fcnEpoch, QStringLiteral( "Date and Time" ) )
<< new StaticFunction( QStringLiteral( "day_of_week" ), 1, fcnDayOfWeek, QStringLiteral( "Date and Time" ) )
<< new StaticFunction( QStringLiteral( "lower" ), 1, fcnLower, QStringLiteral( "String" ) )
<< new StaticFunction( QStringLiteral( "upper" ), 1, fcnUpper, QStringLiteral( "String" ) )
Expand Down
8 changes: 4 additions & 4 deletions src/ui/qgspropertyassistantwidgetbase.ui
Expand Up @@ -86,10 +86,10 @@
<number>6</number>
</property>
<property name="minimum">
<double>-99999999.000000000000000</double>
<double>-99999999999999.000000000000000</double>
</property>
<property name="maximum">
<double>99999999.000000000000000</double>
<double>99999999999999.000000000000000</double>
</property>
</widget>
</item>
Expand All @@ -99,10 +99,10 @@
<number>6</number>
</property>
<property name="minimum">
<double>-99999999.000000000000000</double>
<double>-99999999999999.000000000000000</double>
</property>
<property name="maximum">
<double>99999999.000000000000000</double>
<double>99999999999999.000000000000000</double>
</property>
</widget>
</item>
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -968,6 +968,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "date - date" ) << "to_date('2013-03-04') - to_date('2013-03-01')" << false << QVariant( QgsInterval( 3*24*60*60 ) );
QTest::newRow( "datetime - datetime" ) << "to_datetime('2013-03-04 08:30:00') - to_datetime('2013-03-01 05:15:00')" << false << QVariant( QgsInterval( 3*24*60*60 + 3 * 60*60 + 15*60 ) );
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_date('2017-01-01'))" << false << QVariant(( qlonglong )1483203600000 );
QTest::newRow( "epoch invalid date" ) << "epoch('invalid')" << true << QVariant();

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

0 comments on commit 07cbfdd

Please sign in to comment.