format_date expression: convert datetime to UTC when format string includes a Z

Fixes #57262
This commit is contained in:
Even Rouault 2024-05-19 01:30:33 +02:00 committed by Nyall Dawson
parent 8ff3a38f43
commit ced485ae26
2 changed files with 6 additions and 1 deletions

View File

@ -5807,10 +5807,14 @@ static QVariant fcnFormatNumber( const QVariantList &values, const QgsExpression
static QVariant fcnFormatDate( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
const QDateTime datetime = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
QDateTime datetime = QgsExpressionUtils::getDateTimeValue( values.at( 0 ), parent );
const QString format = QgsExpressionUtils::getStringValue( values.at( 1 ), parent );
const QString language = QgsExpressionUtils::getStringValue( values.at( 2 ), parent );
// Convert to UTC if the format string includes a Z, as QLocale::toString() doesn't do it
if ( format.indexOf( "Z" ) > 0 )
datetime = datetime.toUTC();
QLocale locale = !language.isEmpty() ? QLocale( language ) : QLocale();
return locale.toString( datetime, format );
}

View File

@ -1875,6 +1875,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "time from format and language" ) << "to_time('12:34:56','HH:mm:ss','fr')" << false << QVariant( QTime( 12, 34, 56 ) );
QTest::newRow( "formatted string from date" ) << "format_date('2019-06-29','MMMM d, yyyy')" << false << QVariant( QString( "June 29, 2019" ) );
QTest::newRow( "formatted string from date with language" ) << "format_date('2019-06-29','d MMMM yyyy','fr')" << false << QVariant( QString( "29 juin 2019" ) );
QTest::newRow( "formatted string with Z" ) << "format_date(to_datetime('2019-06-29T13:34:56+01:00'),'yyyy-MM-ddTHH:mm:ssZ')" << false << QVariant( QString( "2019-06-29T12:34:56Z" ) );
// Color functions
QTest::newRow( "ramp color" ) << "ramp_color('Spectral',0.3)" << false << QVariant( "253,190,116,255" );