Import schedules with dates attached to events correctly.
authorPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 13 Sep 2017 21:20:23 +0000 (23:20 +0200)
committerPhilipp Spitzer <philipp@spitzer.priv.at>
Wed, 13 Sep 2017 21:28:44 +0000 (23:28 +0200)
src/sql/schedulexmlparser.cpp
src/sql/sqlengine.cpp
src/sql/sqlengine.h

index 90c7714..7bf7ed6 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <QDomDocument>
 #include <QHash>
+#include <QTime>
 
 #include "schedulexmlparser.h"
 #include "sqlengine.h"
@@ -50,6 +51,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, i
     if (!scheduleElement.isNull())
     {
         QDomElement conferenceElement = scheduleElement.firstChildElement("conference");
+        QTime conference_day_change;
         if (!conferenceElement.isNull())
         {
             emit(parsingScheduleBegin());
@@ -67,6 +69,7 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, i
             sqlEngine->addConferenceToDB(conference, conferenceId);
             conferenceId = conference["id"].toInt();
             conference_title = conference["title"];
+            conference_day_change = QTime(0, 0).addSecs(conference["day_change"].toInt());
         }
 
         // we need to get count of all events in order to emit 'progressStatus' signal
@@ -108,8 +111,18 @@ void ScheduleXmlParser::parseData(const QByteArray &aData, const QString& url, i
                         QHash<QString,QString> event;
                         event["id"] = eventElement.attribute("id");;
                         event["conference_id"] = QString::number(conferenceId, 10);
-                        event["start"] = eventElement.firstChildElement("start").text(); // time eg. 10:00
-                        event["date"] = dayElement.attribute("date"); // date eg. 2009-02-07
+                        QTime event_start = QTime::fromString(eventElement.firstChildElement("start").text(), sqlEngine->TIME_FORMAT);
+                        event["start"] = event_start.toString(sqlEngine->TIME_FORMAT); // time eg. 10:00
+                        QDate event_date;
+                        QDomElement eventDateElement = eventElement.firstChildElement("date");
+                        if (!eventDateElement.isNull()) {
+                            QString date_str = eventDateElement.text(); // date eg. 2009-02-07T10:00:00+00:00
+                            event_date = QDate::fromString(date_str.left(sqlEngine->DATE_FORMAT.size()), sqlEngine->DATE_FORMAT);
+                        } else {
+                            event_date = QDate::fromString(dayElement.attribute("date"),sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
+                            if (event_start < conference_day_change) event_date = event_date.addDays(1);
+                        }
+                        event["date"] = event_date.toString(sqlEngine->DATE_FORMAT); // date eg. 2009-02-07
                         event["duration"] = eventElement.firstChildElement("duration").text(); // time eg. 00:30
                         event["room_name"] = eventElement.firstChildElement("room").text(); // string eg. "Janson"
                         event["tag"] = eventElement.firstChildElement("tag").text(); // string eg. "welcome"
index 5097082..e062387 100644 (file)
@@ -37,9 +37,6 @@
 
 #include <QDebug>
 
-const QString DATE_FORMAT ("yyyy-MM-dd");
-const QString TIME_FORMAT ("hh:mm");
-
 SqlEngine::SqlEngine(QObject *aParent): QObject(aParent) {
 #if QT_VERSION >= 0x050000
     QDir dbPath(QStandardPaths::writableLocation(QStandardPaths::DataLocation));
@@ -192,8 +189,6 @@ void SqlEngine::addEventToDB(QHash<QString,QString> &aEvent) {
     }
     QDate startDate = QDate::fromString(aEvent["date"], DATE_FORMAT);
     QTime startTime = QTime::fromString(aEvent["start"], TIME_FORMAT);
-    // consider day_change (note that if day_change is e.g. at 04:00 AM, an event starting at 02:00 AM has the previous date in the XML file)
-    if (startTime < conference.dayChangeTime()) startDate = startDate.addDays(1);
     QDateTime startDateTime;
     startDateTime.setTimeSpec(Qt::UTC);
     startDateTime = QDateTime(startDate, startTime, Qt::UTC);
index ae7bf06..4097bdb 100644 (file)
@@ -28,6 +28,9 @@
 class SqlEngine : public QObject {
     Q_OBJECT
     public:
+        const QString DATE_FORMAT = "yyyy-MM-dd";
+        const QString TIME_FORMAT = "hh:mm";
+
         QString dbFilename; ///< database filename including path
         QSqlDatabase db; ///< this may be private one day...