From 943d89c8e8359f768f422ac8e40955f1aaf1a89d Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Thu, 2 Jan 2014 12:11:50 +0100 Subject: [PATCH] Hell with date/times. --- src/core/feedsmodelstandardcategory.cpp | 3 +-- src/core/feedsmodelstandardfeed.cpp | 15 +++++++-------- src/core/messagesmodel.cpp | 7 ++----- src/core/textfactory.cpp | 14 ++++++++++++++ src/core/textfactory.h | 6 ++++++ 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/core/feedsmodelstandardcategory.cpp b/src/core/feedsmodelstandardcategory.cpp index b6003e7f1..0cac6f3f5 100755 --- a/src/core/feedsmodelstandardcategory.cpp +++ b/src/core/feedsmodelstandardcategory.cpp @@ -96,8 +96,7 @@ FeedsModelStandardCategory *FeedsModelStandardCategory::loadFromRecord(const QSq category->setId(record.value(CAT_DB_ID_INDEX).toInt()); category->setTitle(record.value(CAT_DB_TITLE_INDEX).toString()); category->setDescription(record.value(CAT_DB_DESCRIPTION_INDEX).toString()); - category->setCreationDate(QDateTime::fromString(record.value(CAT_DB_DCREATED_INDEX).toString(), - Qt::ISODate)); + category->setCreationDate(TextFactory::parseDateTime(record.value(CAT_DB_DCREATED_INDEX).value()).toLocalTime()); category->setIcon(IconFactory::fromByteArray(record.value(CAT_DB_ICON_INDEX).toByteArray())); return category; diff --git a/src/core/feedsmodelstandardfeed.cpp b/src/core/feedsmodelstandardfeed.cpp index 94e6642cb..a8cfaf473 100755 --- a/src/core/feedsmodelstandardfeed.cpp +++ b/src/core/feedsmodelstandardfeed.cpp @@ -32,8 +32,7 @@ FeedsModelStandardFeed *FeedsModelStandardFeed::loadFromRecord(const QSqlRecord feed->setTitle(record.value(FDS_DB_TITLE_INDEX).toString()); feed->setId(record.value(FDS_DB_ID_INDEX).toInt()); feed->setDescription(record.value(FDS_DB_DESCRIPTION_INDEX).toString()); - feed->setCreationDate(QDateTime::fromString(record.value(FDS_DB_DCREATED_INDEX).toString(), - Qt::ISODate)); + feed->setCreationDate(TextFactory::parseDateTime(record.value(FDS_DB_DCREATED_INDEX).value()).toLocalTime()); feed->setIcon(IconFactory::fromByteArray(record.value(FDS_DB_ICON_INDEX).toByteArray())); feed->setEncoding(record.value(FDS_DB_ENCODING_INDEX).toString()); feed->setUrl(record.value(FDS_DB_URL_INDEX).toString()); @@ -206,7 +205,7 @@ void FeedsModelStandardFeed::update() { void FeedsModelStandardFeed::updateMessages(const QList &messages) { int feed_id = id(), message_id; - QDateTime message_creation_date; + qint64 message_creation_date; QSqlDatabase database = DatabaseFactory::getInstance()->addConnection("FeedsModelStandardFeed"); // Prepare queries. @@ -252,7 +251,7 @@ void FeedsModelStandardFeed::updateMessages(const QList &messages) { if (query_select.next()) { // Message with this title & url probably exists in current feed. message_id = query_select.value(0).toInt(); - message_creation_date = TextFactory::parseDateTime(query_select.value(2).toString()); + message_creation_date = query_select.value(2).value(); } else { message_id = -1; @@ -266,15 +265,15 @@ void FeedsModelStandardFeed::updateMessages(const QList &messages) { query_insert.bindValue(":title", message.m_title); query_insert.bindValue(":url", message.m_url); query_insert.bindValue(":author", message.m_author); - query_insert.bindValue(":date_created", message.m_created.toString(Qt::ISODate)); + query_insert.bindValue(":date_created", message.m_created.toMSecsSinceEpoch()); query_insert.bindValue(":contents", message.m_contents); query_insert.exec(); query_insert.finish(); } else if (message.m_createdFromFeed && - message_creation_date.isValid() && - message_creation_date > message.m_created) { + message_creation_date != 0 && + message_creation_date > message.m_created.toMSecsSinceEpoch()) { qDebug("Message '%s' (id %d) was updated in the feed, updating too.", qPrintable(message.m_title), message_id); @@ -288,7 +287,7 @@ void FeedsModelStandardFeed::updateMessages(const QList &messages) { query_update.bindValue(":title", message.m_title); query_update.bindValue(":url", message.m_url); query_update.bindValue(":author", message.m_author); - query_update.bindValue(":date_created", message.m_created.toString(Qt::ISODate)); + query_update.bindValue(":date_created", message.m_created.toMSecsSinceEpoch()); query_update.bindValue(":contents", message.m_contents); query_update.bindValue(":id", message_id); diff --git a/src/core/messagesmodel.cpp b/src/core/messagesmodel.cpp index 48acb844e..1894159d4 100644 --- a/src/core/messagesmodel.cpp +++ b/src/core/messagesmodel.cpp @@ -141,11 +141,8 @@ QVariant MessagesModel::data(const QModelIndex &index, int role) const { int index_column = index.column(); if (index_column == MSG_DB_DCREATED_INDEX) { - // This column contains QDateTime properly stored in ISO format. - // So that QDateTime::fromString(...) is okay here. - return QDateTime::fromString(QSqlTableModel::data(index, - role).toString(), - Qt::ISODate).toLocalTime().toString(Qt::DefaultLocaleShortDate); + return TextFactory::parseDateTime(QSqlTableModel::data(index, + role).value()).toLocalTime().toString(Qt::DefaultLocaleShortDate); } else if (index_column == MSG_DB_AUTHOR_INDEX) { QString author_name = QSqlTableModel::data(index, role).toString(); diff --git a/src/core/textfactory.cpp b/src/core/textfactory.cpp index 0f4111df8..24eaac815 100644 --- a/src/core/textfactory.cpp +++ b/src/core/textfactory.cpp @@ -40,6 +40,20 @@ QDateTime TextFactory::parseDateTime(const QString &date_time) { return QDateTime(); } +QDateTime TextFactory::parseDateTime(qint64 milis_from_epoch) { + QDateTime converted = QDateTime::fromMSecsSinceEpoch(milis_from_epoch); + + // TODO: tadle funkce nahore fromMSec.. by mela vracet cas v UTC + // tedy timespec Qt::UTC, ale na windows vraci local time. + // overit co to vraci na linuxu a podle toho + // prenastavit zobrazovani datumu v messagesmodelu::data (delani + // nebo nedelani konverze .toLocalTime(). + // mozna taky toString() udela konverzi za me. + // vsude je pouzito prozatim toLocalTime() + //converted.setTimeSpec(Qt::UTC); + return converted; +} + QString TextFactory::shorten(const QString &input, int text_length_limit) { if (input.size() > text_length_limit) { return input.left(text_length_limit - ELLIPSIS_LENGTH) + QString(ELLIPSIS_LENGTH, '.'); diff --git a/src/core/textfactory.h b/src/core/textfactory.h index 5ce2a909f..eefd41c92 100644 --- a/src/core/textfactory.h +++ b/src/core/textfactory.h @@ -14,8 +14,14 @@ class TextFactory { public: // Tries to parse input textual date/time representation. // Returns invalid date/time if processing fails. + // NOTE: This method tries to always return time in UTC+00:00. static QDateTime parseDateTime(const QString &date_time); + // Converts 1970-epoch miliseconds to date/time. + // NOTE: This method returns date/time in UTC+00:00. + // NOTE: On Windows UTC is known to be broken. + static QDateTime parseDateTime(qint64 milis_from_epoch); + // Strips "<....>" (HTML, XML) tags from given text. static QString stripTags(QString text);