Skip to content

Commit

Permalink
QgsDateTimeEdit: convert datetime to UTC when format string includes a Z
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault committed May 19, 2024
1 parent eb879e7 commit cf75374
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,9 @@ void QgsDateTimeEdit::setDateTime( const QDateTime &dateTime )
{
// changed emits a signal, so don't allow it to be emitted from setDateTime
mBlockChangedSignal++;
// We need to set the time spec of the set datetime to the widget, otherwise
// the dateTime() getter would loose edit, and return local time.
QDateTimeEdit::setTimeSpec( dateTime.timeSpec() );
QDateTimeEdit::setDateTime( dateTime );
mBlockChangedSignal--;
changed( dateTime );
Expand Down
12 changes: 12 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeeditconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,18 @@ QgsDateTimeEditConfig::QgsDateTimeEditConfig( QgsVectorLayer *vl, int fieldIdx,

void QgsDateTimeEditConfig::updateDemoWidget()
{
// Use a UTC datetime if the format string includes a Z
if ( mDisplayFormatEdit->text().indexOf( "Z" ) > 0 )
{
mDemoDateTimeEdit->setTimeSpec( Qt::UTC );
mDemoDateTimeEdit->setDateTime( QDateTime::currentDateTimeUtc() );
}
else
{
mDemoDateTimeEdit->setTimeSpec( Qt::LocalTime );
mDemoDateTimeEdit->setDateTime( QDateTime::currentDateTime() );
}

mDemoDateTimeEdit->setDisplayFormat( mDisplayFormatEdit->text() );
mDemoDateTimeEdit->setCalendarPopup( mCalendarPopupCheckBox->isChecked() );
}
Expand Down
6 changes: 6 additions & 0 deletions src/gui/editorwidgets/qgsdatetimeeditwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,12 @@ void QgsDateTimeEditWrapper::updateValues( const QVariant &value, const QVariant

if ( mQgsDateTimeEdit )
{
// Convert to UTC if the format string includes a Z
if ( mQgsDateTimeEdit->displayFormat().indexOf( "Z" ) > 0 )
{
dateTime = dateTime.toUTC();
}

mQgsDateTimeEdit->setDateTime( dateTime );
}
else
Expand Down
12 changes: 12 additions & 0 deletions tests/src/python/test_qgsdatetimeedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
start_app()

DATE = QDateTime.fromString('2018-01-01 01:02:03', Qt.DateFormat.ISODate)
DATE_Z = QDateTime.fromString('2018-01-01 01:02:03Z', Qt.DateFormat.ISODate)


class TestQgsDateTimeEdit(QgisTestCase):
Expand All @@ -32,6 +33,17 @@ def testSettersGetters(self):
w.setDateTime(QDateTime())
self.assertEqual(w.dateTime(), DATE)

def testSettersGetters_DATE_Z(self):
""" test widget handling with Z time spec """
w = QgsDateTimeEdit()
w.setAllowNull(False)

w.setDateTime(DATE_Z)
self.assertEqual(w.dateTime(), DATE_Z)
# date should remain when setting an invalid date
w.setDateTime(QDateTime())
self.assertEqual(w.dateTime(), DATE_Z)

def testNullValueHandling(self):
""" test widget handling of null values """
w = QgsDateTimeEdit()
Expand Down

0 comments on commit cf75374

Please sign in to comment.