mirror of
https://github.com/qgis/QGIS.git
synced 2025-12-04 00:06:46 -05:00
[FEATURE][expression] Optional format parameter for to_date, to_datetime, to_time functions
This commit is contained in:
parent
3a19a5f2a9
commit
b58a2a6c21
@ -1,8 +1,13 @@
|
||||
{
|
||||
"name": "to_date",
|
||||
"type": "function",
|
||||
"description": "Convert a string into a date object.",
|
||||
"arguments": [ {"arg":"string","description":"string representing a date value"}],
|
||||
"examples": [ { "expression":"to_date('2012-05-04')", "returns":"2012-05-04"}
|
||||
"description": "Convert a string into a date object. An optional format string can be provided to parse the string; see <a href='https://doc.qt.io/qt-5/qdate.html#fromString-1'>QDate::fromString</a> for additional documentation on the format.",
|
||||
"arguments": [
|
||||
{"arg":"string","description":"string representing a date value"},
|
||||
{"arg":"format","optional":true,"description":"format used to convert the string into a date"}
|
||||
],
|
||||
"examples": [
|
||||
{ "expression":"to_date('2012-05-04')", "returns":"2012-05-04"},
|
||||
{ "expression":"to_date('June 29, 2019','MMMM d, yyyy')", "returns":"2019-06-29"}
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
{
|
||||
"name": "to_datetime",
|
||||
"type": "function",
|
||||
"description": "Convert a string into a datetime object.",
|
||||
"arguments": [ {"arg":"string","description":"string representing a datetime value"}],
|
||||
"examples": [ { "expression":"to_datetime('2012-05-04 12:50:00')", "returns":"2012-05-04T12:50:00"}
|
||||
"description": "Convert a string into a datetime object. An optional format string can be provided to parse the string; see <a href='https://doc.qt.io/qt-5/qdatetime.html#fromString-1'>QDateTime::fromString</a> for additional documentation on the format.",
|
||||
"arguments": [
|
||||
{"arg":"string","description":"string representing a datetime value"},
|
||||
{"arg":"format","optional":true,"description":"format used to convert the string into a date"}
|
||||
],
|
||||
"examples": [
|
||||
{ "expression":"to_datetime('2012-05-04 12:50:00')", "returns":"2012-05-04T12:50:00"},
|
||||
{ "expression":"to_datetime('June 29, 2019 @ 12:34','MMMM d, yyyy @ HH:mm')", "returns":"2019-06-29T12:34"}
|
||||
]
|
||||
}
|
||||
|
||||
@ -1,8 +1,13 @@
|
||||
{
|
||||
"name": "to_time",
|
||||
"type": "function",
|
||||
"description": "Convert a string into a time object.",
|
||||
"arguments": [ {"arg":"string","description":"string representing a time value"}],
|
||||
"examples": [ { "expression":"to_time('12:30:01')", "returns":"12:30:01"}
|
||||
"description": "Convert a string into a time object. An optional format string can be provided to parse the string; see <a href='https://doc.qt.io/qt-5/qtime.html#fromString-1'>QTime::fromString</a> for additional documentation on the format.",
|
||||
"arguments": [
|
||||
{"arg":"string","description":"string representing a time value"},
|
||||
{"arg":"format","optional":true,"description":"format used to convert the string into a date"}
|
||||
],
|
||||
"examples": [
|
||||
{ "expression":"to_time('12:30:01')", "returns":"12:30:01"},
|
||||
{ "expression":"to_time('12:34','HH:mm')", "returns":"12:34:00"}
|
||||
]
|
||||
}
|
||||
|
||||
@ -1019,7 +1019,18 @@ static QVariant fcnToString( const QVariantList &values, const QgsExpressionCont
|
||||
|
||||
static QVariant fcnToDateTime( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
{
|
||||
return QVariant( QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent ) );
|
||||
QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
|
||||
if ( format.isEmpty() )
|
||||
return QVariant( QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent ) );
|
||||
|
||||
QString datetimestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
|
||||
QDateTime datetime = QDateTime::fromString( datetimestring, format );
|
||||
if ( !datetime.isValid() )
|
||||
{
|
||||
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to DateTime" ).arg( datetimestring ) );
|
||||
datetime = QDateTime();
|
||||
}
|
||||
return QVariant( datetime );
|
||||
}
|
||||
|
||||
static QVariant fcnCoalesce( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * )
|
||||
@ -1721,12 +1732,34 @@ static QVariant fcnNow( const QVariantList &, const QgsExpressionContext *, QgsE
|
||||
|
||||
static QVariant fcnToDate( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
{
|
||||
return QVariant( QgsExpressionUtils::getDateValue( values.at( 0 ), parent ) );
|
||||
QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
|
||||
if ( format.isEmpty() )
|
||||
return QVariant( QgsExpressionUtils::getDateValue( values.at( 0 ), parent ) );
|
||||
|
||||
QString datestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
|
||||
QDate date = QDate::fromString( datestring, format );
|
||||
if ( !date.isValid() )
|
||||
{
|
||||
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to Date" ).arg( datestring ) );
|
||||
date = QDate();
|
||||
}
|
||||
return QVariant( date );
|
||||
}
|
||||
|
||||
static QVariant fcnToTime( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
{
|
||||
return QVariant( QgsExpressionUtils::getTimeValue( values.at( 0 ), parent ) );
|
||||
QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
|
||||
if ( format.isEmpty() )
|
||||
return QVariant( QgsExpressionUtils::getTimeValue( values.at( 0 ), parent ) );
|
||||
|
||||
QString timestring = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
|
||||
QTime time = QTime::fromString( timestring, format );
|
||||
if ( !time.isValid() )
|
||||
{
|
||||
parent->setEvalErrorString( QObject::tr( "Cannot convert '%1' to Time" ).arg( timestring ) );
|
||||
time = QTime();
|
||||
}
|
||||
return QVariant( time );
|
||||
}
|
||||
|
||||
static QVariant fcnToInterval( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
|
||||
@ -4971,9 +5004,9 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_int" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToInt, QStringLiteral( "Conversions" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "toint" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_real" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToReal, QStringLiteral( "Conversions" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "toreal" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_string" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToString, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "String" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "tostring" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_datetime" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToDateTime, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todatetime" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_date" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToDate, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todate" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_time" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToTime, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "totime" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_datetime" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "format" ), true, QVariant() ), fcnToDateTime, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todatetime" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_date" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "format" ), true, QVariant() ), fcnToDate, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todate" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_time" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "format" ), true, QVariant() ), fcnToTime, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "totime" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_interval" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ), fcnToInterval, QStringList() << QStringLiteral( "Conversions" ) << QStringLiteral( "Date and Time" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "tointerval" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_dm" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "axis" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "precision" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "formatting" ), true ), fcnToDegreeMinute, QStringLiteral( "Conversions" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todm" ) )
|
||||
<< new QgsStaticExpressionFunction( QStringLiteral( "to_dms" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "value" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "axis" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "precision" ) ) << QgsExpressionFunction::Parameter( QStringLiteral( "formatting" ), true ), fcnToDegreeMinuteSecond, QStringLiteral( "Conversions" ), QString(), false, QSet<QString>(), false, QStringList() << QStringLiteral( "todms" ) )
|
||||
|
||||
@ -1269,6 +1269,11 @@ 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( "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();
|
||||
QTest::newRow( "datetime from format" ) << "to_datetime('June 29, 2019 @ 11:00','MMMM d, yyyy @ HH:mm')" << false << QVariant( QDateTime( QDate( 2019, 6, 29 ), QTime( 11, 0 ) ) );
|
||||
QTest::newRow( "time from format" ) << "to_time('12:34:56','HH:mm:ss')" << false << QVariant( QTime( 12, 34, 56 ) );
|
||||
|
||||
// Color functions
|
||||
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "254,190,116,255" );
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user