Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Show time zones in expression results preview for date time values
Behind the scenes Qt IS considering time zones for datetime values,
which leads to some extremely subtle issues if users are unaware
that datetime values differ in timezones.

Accordingly we should always show these to users so that they
can be aware when datetimes have different zones.
  • Loading branch information
nyalldawson committed Aug 11, 2021
1 parent 3076c39 commit 29b8d87
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/core/expression/qgsexpression.cpp
Expand Up @@ -1013,18 +1013,18 @@ QString QgsExpression::formatPreviewString( const QVariant &value, const bool ht
}
else if ( value.type() == QVariant::Date )
{
QDate dt = value.toDate();
const QDate dt = value.toDate();
return startToken + tr( "date: %1" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd" ) ) ) + endToken;
}
else if ( value.type() == QVariant::Time )
{
QTime tm = value.toTime();
const QTime tm = value.toTime();
return startToken + tr( "time: %1" ).arg( tm.toString( QStringLiteral( "hh:mm:ss" ) ) ) + endToken;
}
else if ( value.type() == QVariant::DateTime )
{
QDateTime dt = value.toDateTime();
return startToken + tr( "datetime: %1" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd hh:mm:ss" ) ) ) + endToken;
const QDateTime dt = value.toDateTime();
return startToken + tr( "datetime: %1 (%2)" ).arg( dt.toString( QStringLiteral( "yyyy-MM-dd hh:mm:ss" ) ), dt.timeZoneAbbreviation() ) + endToken;
}
else if ( value.type() == QVariant::String )
{
Expand Down
2 changes: 2 additions & 0 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -3991,6 +3991,8 @@ class TestQgsExpression: public QObject
{
QCOMPARE( QgsExpression::formatPreviewString( QVariant( "hello" ) ), QStringLiteral( "'hello'" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( QVariantMap() ) ), QStringLiteral( "{}" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( QDateTime( QDate( 2020, 3, 4 ), QTime( 12, 13, 14 ), Qt::UTC ) ) ), QStringLiteral( "<i>&lt;datetime: 2020-03-04 12:13:14 (UTC)&gt;</i>" ) );
QCOMPARE( QgsExpression::formatPreviewString( QVariant( QDateTime( QDate( 2020, 3, 4 ), QTime( 12, 13, 14 ), Qt::OffsetFromUTC, 3600 ) ) ), QStringLiteral( "<i>&lt;datetime: 2020-03-04 12:13:14 (UTC+01:00)&gt;</i>" ) );

QVariantMap map;
map[QStringLiteral( "1" )] = "One";
Expand Down

0 comments on commit 29b8d87

Please sign in to comment.