Skip to content

Commit

Permalink
QgsDateTimeFieldFormatter::representValue(): convert datetime to UTC …
Browse files Browse the repository at this point in the history
…when format string includes a Z

Fixes #57262
  • Loading branch information
rouault authored and nyalldawson committed May 22, 2024
1 parent ced485a commit ffd8ee1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/core/fieldformatter/qgsdatetimefieldformatter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fi
}
else
{
// Convert to UTC if the format string includes a Z, as QLocale::toString() doesn't do it
if ( displayFormat.indexOf( "Z" ) > 0 )
date = date.toUTC();

result = date.toString( displayFormat );
}
}
Expand Down
5 changes: 5 additions & 0 deletions tests/src/python/test_qgsfieldformatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,11 @@ def test_representValue(self):
QDateTime(QDate(2020, 3, 4), QTime(12, 13, 14), Qt.TimeSpec.OffsetFromUTC, 3600)),
'04/03/2020 12:13:14')

config = {"display_format": "dd/MM/yyyy HH:mm:ssZ"}
self.assertEqual(field_formatter.representValue(layer, 0, config, None,
QDateTime(QDate(2020, 3, 4), QTime(12, 13, 14), Qt.TimeSpec.OffsetFromUTC, 3600)),
'04/03/2020 11:13:14Z')

locale_assertions = {
QLocale(QLocale.Language.English): {
"date_format": 'M/d/yy',
Expand Down

0 comments on commit ffd8ee1

Please sign in to comment.