diff options
| author | David Faure <faure@kde.org> | 2021-03-26 10:05:31 +0100 | 
|---|---|---|
| committer | David Faure <faure@kde.org> | 2021-01-26 10:05:31 +0100 | 
| commit | b132648084df231c196e726c899761d83d0766cc (patch) | |
| tree | 94f43a1d16905aed554d4feafea7642617856b87 | |
| parent | d1c58a842196c229913f908f42e8e3799b0717fc (diff) | |
| download | kconfig-b132648084df231c196e726c899761d83d0766cc.tar.gz kconfig-b132648084df231c196e726c899761d83d0766cc.tar.bz2 | |
KConfig: preserve the milliseconds component of QDateTime
I stored a file's lastModified() into KConfig, to compare with it
again on next start, and the loss of milliseconds made the code think
the timestamps didn't match, even when they did.
| -rw-r--r-- | autotests/kconfigtest.cpp | 8 | ||||
| -rw-r--r-- | src/core/kconfiggroup.cpp | 13 | 
2 files changed, 15 insertions, 6 deletions
| diff --git a/autotests/kconfigtest.cpp b/autotests/kconfigtest.cpp index 6f79f072..6ab50b2f 100644 --- a/autotests/kconfigtest.cpp +++ b/autotests/kconfigtest.cpp @@ -66,6 +66,7 @@ static QString homePath()  #define SIZEENTRY QSize( 10, 20 )  #define RECTENTRY QRect( 10, 23, 5321, 13 )  #define DATETIMEENTRY QDateTime( QDate( 2002, 06, 23 ), QTime( 12, 55, 40 ) ) +#define DATETIMEWITHMSENTRY QDateTime( QDate( 2002, 06, 23 ), QTime( 12, 55, 40, 532 ) )  #define STRINGLISTENTRY (QStringList( "Hello," ) << " World")  #define STRINGLISTEMPTYENTRY QStringList()  #define STRINGLISTJUSTEMPTYELEMENT QStringList(QString()) @@ -154,6 +155,7 @@ void KConfigTest::initTestCase()      cg.writeEntry("sizeEntry", SIZEENTRY);      cg.writeEntry("dateTimeEntry", DATETIMEENTRY);      cg.writeEntry("dateEntry", DATETIMEENTRY.date()); +    cg.writeEntry("dateTimeWithMSEntry", DATETIMEWITHMSENTRY);      KConfigGroup ct = cg;      cg = KConfigGroup(&ct, "Nested Group 1"); @@ -620,10 +622,12 @@ void KConfigTest::testComplex()      QCOMPARE(sc3.readEntry("pointEntry", QPoint()), POINTENTRY);      QCOMPARE(sc3.readEntry("sizeEntry", SIZEENTRY), SIZEENTRY);      QCOMPARE(sc3.readEntry("rectEntry", QRect(1, 2, 3, 4)), RECTENTRY); -    QCOMPARE(sc3.readEntry("dateTimeEntry", QDateTime()).toString(Qt::ISODate), -             DATETIMEENTRY.toString(Qt::ISODate)); +    QCOMPARE(sc3.readEntry("dateTimeEntry", QDateTime()).toString(Qt::ISODateWithMs), +             DATETIMEENTRY.toString(Qt::ISODateWithMs));      QCOMPARE(sc3.readEntry("dateEntry", QDate()).toString(Qt::ISODate),               DATETIMEENTRY.date().toString(Qt::ISODate)); +    QCOMPARE(sc3.readEntry("dateTimeWithMSEntry", QDateTime()).toString(Qt::ISODateWithMs), +             DATETIMEWITHMSENTRY.toString(Qt::ISODateWithMs));      QCOMPARE(sc3.readEntry("dateTimeEntry", QDate()), DATETIMEENTRY.date());  } diff --git a/src/core/kconfiggroup.cpp b/src/core/kconfiggroup.cpp index cc4f8d0c..14cbcab8 100644 --- a/src/core/kconfiggroup.cpp +++ b/src/core/kconfiggroup.cpp @@ -27,6 +27,7 @@  #include <QUrl>  #include <stdlib.h> +#include <math.h>  class KConfigGroupPrivate : public QSharedData  { @@ -325,14 +326,18 @@ QVariant KConfigGroup::convertToQVariant(const char *pKey, const QByteArray &val          return size;      }      case QMetaType::QDateTime: { -        const auto list = asIntList(value); -        if (list.count() != 6) { +        const auto list = asRealList(value); +        if (list.count() < 6) {              qCWarning(KCONFIG_CORE_LOG) << errString(pKey, value, aDefault)                         << formatError(6, list.count());              return aDefault;          }          const QDate date(list.at(0), list.at(1), list.at(2)); -        const QTime time(list.at(3), list.at(4), list.at(5)); +        const qreal totalSeconds = list.at(5); +        qreal seconds; +        const qreal fractional = modf(totalSeconds, &seconds); +        const qreal milliseconds = round(fractional * 1000.0); +        const QTime time(list.at(3), list.at(4), seconds, milliseconds);          const QDateTime dt(date, time);          if (!dt.isValid()) {              qCWarning(KCONFIG_CORE_LOG) << errString(pKey, value, aDefault); @@ -1044,7 +1049,7 @@ void KConfigGroup::writeEntry(const char *key, const QVariant &value,              time.hour(),              time.minute(), -            time.second(), +            time.second() + time.msec()/1000.0,          };          writeEntry(key, list, flags); | 
