Skip to content

Commit 07cbfdd

Browse files
authored
[FEATURE] epoch() expression function (#4151)
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.
1 parent e1ede70 commit 07cbfdd

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

resources/function_help/json/epoch

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "epoch",
3+
"type": "function",
4+
"description": "Return the interval in milliseconds between the unix epoch and a given date value.",
5+
"arguments": [ {"arg":"date","description":"a date or datetime value"} ],
6+
"examples": [ { "expression":"epoch(to_date('2017-01-01'))", "returns":"1483203600000"} ]
7+
}

src/core/qgsexpression.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1721,6 +1721,18 @@ static QVariant fcnSeconds( const QVariantList& values, const QgsExpressionConte
17211721
}
17221722
}
17231723

1724+
static QVariant fcnEpoch( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
1725+
{
1726+
QDateTime dt = getDateTimeValue( values.at( 0 ), parent );
1727+
if ( dt.isValid() )
1728+
{
1729+
return QVariant( dt.toMSecsSinceEpoch() );
1730+
}
1731+
else
1732+
{
1733+
return QVariant();
1734+
}
1735+
}
17241736

17251737
#define ENSURE_GEOM_TYPE(f, g, geomtype) \
17261738
if ( !(f).hasGeometry() ) \
@@ -3903,6 +3915,7 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
39033915
<< new StaticFunction( QStringLiteral( "hour" ), 1, fcnHour, QStringLiteral( "Date and Time" ) )
39043916
<< new StaticFunction( QStringLiteral( "minute" ), 1, fcnMinute, QStringLiteral( "Date and Time" ) )
39053917
<< new StaticFunction( QStringLiteral( "second" ), 1, fcnSeconds, QStringLiteral( "Date and Time" ) )
3918+
<< new StaticFunction( QStringLiteral( "epoch" ), ParameterList() << Parameter( QStringLiteral( "date" ) ), fcnEpoch, QStringLiteral( "Date and Time" ) )
39063919
<< new StaticFunction( QStringLiteral( "day_of_week" ), 1, fcnDayOfWeek, QStringLiteral( "Date and Time" ) )
39073920
<< new StaticFunction( QStringLiteral( "lower" ), 1, fcnLower, QStringLiteral( "String" ) )
39083921
<< new StaticFunction( QStringLiteral( "upper" ), 1, fcnUpper, QStringLiteral( "String" ) )

src/ui/qgspropertyassistantwidgetbase.ui

+4-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@
8686
<number>6</number>
8787
</property>
8888
<property name="minimum">
89-
<double>-99999999.000000000000000</double>
89+
<double>-99999999999999.000000000000000</double>
9090
</property>
9191
<property name="maximum">
92-
<double>99999999.000000000000000</double>
92+
<double>99999999999999.000000000000000</double>
9393
</property>
9494
</widget>
9595
</item>
@@ -99,10 +99,10 @@
9999
<number>6</number>
100100
</property>
101101
<property name="minimum">
102-
<double>-99999999.000000000000000</double>
102+
<double>-99999999999999.000000000000000</double>
103103
</property>
104104
<property name="maximum">
105-
<double>99999999.000000000000000</double>
105+
<double>99999999999999.000000000000000</double>
106106
</property>
107107
</widget>
108108
</item>

tests/src/core/testqgsexpression.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,8 @@ class TestQgsExpression: public QObject
968968
QTest::newRow( "date - date" ) << "to_date('2013-03-04') - to_date('2013-03-01')" << false << QVariant( QgsInterval( 3*24*60*60 ) );
969969
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 ) );
970970
QTest::newRow( "time - time" ) << "to_time('08:30:00') - to_time('05:15:00')" << false << QVariant( QgsInterval( 3 * 60*60 + 15*60 ) );
971+
QTest::newRow( "epoch" ) << "epoch(to_date('2017-01-01'))" << false << QVariant(( qlonglong )1483203600000 );
972+
QTest::newRow( "epoch invalid date" ) << "epoch('invalid')" << true << QVariant();
971973

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

0 commit comments

Comments
 (0)