Skip to content

Commit

Permalink
format_date expression: convert datetime to UTC when format string in…
Browse files Browse the repository at this point in the history
…cludes a Z

Fixes #57262
  • Loading branch information
rouault authored and nyalldawson committed May 22, 2024
1 parent 8ff3a38 commit ced485a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/core/expression/qgsexpressionfunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgsexpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down

0 comments on commit ced485a

Please sign in to comment.